Get started with Databricks Apps
Important
Databricks Apps is in Public Preview.
This article helps you get started with Databricks Apps using a step-by-step example to create a simple app in your local development environment and deploy the app to your Azure Databricks workspace. This example walks you through:
- Creating and testing the app locally.
- After testing locally, using the Databricks CLI to add the app to your Azure Databricks workspace.
- Viewing the details page for the app in your workspace.
- Copying the source code and artifacts for the app to your workspace.
- Viewing the output of the app in your workspace.
Before stepping through the example, ensure that your Azure Databricks workspace and local development environment meet the requirements.
Databricks recommends using a Python virtual environment when developing apps. The example in this article uses pipenv to create a virtual environment. To learn more, see Python Virtual Environments: A Primer.
This example is also available in the Databricks Apps template library. See How do I create an app in the Databricks Apps UI?.
Step 1: Set up your local environment
Open a terminal and run the following commands to:
- Create and start a Python virtual environment.
- Install the Python libraries required by the example app.
- Create a local directory for the source and configuration files for your app.
pipenv --python 3.11
pipenv shell
pip install gradio
pip install pandas
mkdir <app-dir-name>
cd <app-dir-name>
Replace <app-dir-name>
with the name of a local directory for your app files, for example, gradio-hello-world
.
Step 2: Add the source and configuration for your app
In a text editor or your favorite integrated development environment (IDE), create a new Python file with the following code and save it to the directory you created. This example uses the filename
app.py
for the Python file:import gradio as gr import pandas as pd data = pd.DataFrame({'x': [x for x in range(30)], 'y': [2 ** x for x in range(30)]}) # Display the data with Gradio with gr.Blocks(css='footer {visibility: hidden}') as gradio_app: with gr.Row(): with gr.Column(scale=3): gr.Markdown('# Hello world!') gr.ScatterPlot(value=data, height=400, width=700, container=False, x='x', y='y', y_title='Fun with data', x_title='Apps') if __name__ == '__main__': gradio_app.launch()
In a text editor or an IDE, create a new YAML file with the following contents and save it to a file named
app.yaml
in the directory you created:command: [ "python", "<app-name.py>" ]
Replace
<app-name.py>
with the name of the Python file containing the code for the app. For example,app.py
.
Step 3: Test your app locally
To test your app locally, open a terminal and run
python <app-name.py>
, replacing<app-name.py>
with the name of the file containing the code for the app.python app.py Running on local URL: http://127.0.0.1:7860 ...
To view the app’s output, open
http://127.0.0.1:7860
in a browser window.
Step 4: Deploy the app to your workspace
To create a new app in your workspace and deploy the code from your local environment to the workspace, open a terminal and complete the following steps.
Create the app in your Azure Databricks workspace.
Note
- The name assigned to a Databricks app cannot be changed after creating the app, and any user with access to a Azure Databricks workspace can see the names and deployment history of all Databricks apps in the workspace. Additionally, the app name is included in records written to system tables. Because of this visibility, you should not include sensitive information when naming your Databricks apps.
- The name must be unique in the Azure Databricks workspace that hosts the app and must contain only lowercase letters, numbers, and hyphens.
databricks apps create <app-name>
Replace
<app-name>
with a name for your app. For example,gradio-hello-world
.To view the app in your workspace when the
create
command completes, in the sidebar, click Compute, go to the Apps tab, and click the link to your app in the Name column.Sync the files from your local environment to your Azure Databricks workspace. The command to sync the files from your local environment to your workspace, including the workspace path for the files, is under Sync source files into Databricks. Click to copy this command.
In a terminal, switch to the directory containing your app files and run the copied
sync
command.Note
If there are specific files or directories in your local app directory that you do not want transferred by the
databricks sync
command, add those files or directories to a.gitignore
file in the local app directory. For example, if you have a Python virtual environment directory in the same directory as your app, add the directory’s name to the.gitignore
file, and thesync
command will skip that directory when transferring files.databricks sync --watch . /Workspace/Users/user@databricks.com/gradio-hello-world ... Initial Sync Complete
To view the synced files in your workspace when the
sync
command completes, click Workspace in the sidebar and go to the directory created for your app.To deploy the app, run the following command in a terminal, replacing
<app-path>
with the workspace path to your app files.databricks apps deploy gradio-hello-world --source-code-path <app-path>
To view the deployment status, go to the details page for the app.
To view the deployed app’s output, click the app link under the app name on the details page.
Next steps
To learn how to create apps in the Databricks Apps UI, see How do I create an app in the Databricks Apps UI?.