Get started with Databricks Apps
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 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 Databricks workspace.
- Viewing the overview 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.
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 Develop Databricks apps.
Prerequisites
Before you complete this tutorial, make sure that your Databricks workspace and local development environment are configured correctly. See Set up your Databricks Apps workspace and development environment.
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 gradio-hello-world
cd gradio-hello-world
gradio-hello-world
is the local directory for your app files.
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 named
app.py
with the following code and save it to the directory you created:Pythonimport 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 YAML file named
app.yaml
with the following contents in the directory you created:YAMLcommand: ['python', 'app.py']
Step 3: Test your app locally
-
To test your app locally, open a terminal and run
python app.py
:Bashpython app.py
Running on local URL: http://127.0.0.1:7860
... -
To view the app:
-
Navigate to
http://127.0.0.1:7860
in a browser window. -
Alternatively, use the databricks apps run-local command to run and debug the app:
Bashdatabricks apps run-local --prepare-environment --debug
This installs all dependencies and prepares the virtual environment, then starts the app and the debugger on port 5678. Navigate to
http://localhost:8001
to view the app. To be able to set breakpoints, in Visual Studio Code you can install the Python debugger, then Run > Start Debugging and Remote Attach.
-
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.
-
Run the following command to create the app in your Databricks workspace.
Bashdatabricks apps create gradio-hello-world
note- You can’t change an app name after you create it.
- All users with access to the Databricks workspace can view app names and deployment history.
- Avoid including sensitive information in app names.
- Each app name must be unique within the workspace and can only contain lowercase letters, numbers, and hyphens.
-
To view the app in your workspace when the
create
command completes, in the sidebar, clickCompute, go to the Apps tab, and click the link to your app in the Name column.
-
Copy the command under Sync source files into Databricks.
-
In a terminal, navigate to the directory that contains your app files and run the copied
sync
command.Bashdatabricks sync --watch . /Workspace/Users/my-email@org.com/gradio-hello-world
...
Initial Sync Complete -
To view the synced files in your workspace when the
sync
command completes, clickWorkspace in the sidebar and go to the directory created for your app.
-
To deploy the app, run the following command in a terminal. Replace the source code path with the workspace path to your app files.
databricks apps deploy gradio-hello-world \
--source-code-path /Workspace/Users/my-email@org.com/gradio-hello-world -
To view the deployment status, go to the overview page for the app.
To view the deployed app's output, click the app link under the app name on the overview page.
Next steps
To learn how to create apps in the Databricks Apps UI, see Configure a Databricks app.