Skip to main content

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.
Bash
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

  1. 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:

    Python
    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()
  2. In a text editor or an IDE, create a YAML file named app.yaml with the following contents in the directory you created:

    YAML
    command: ['python', 'app.py']

Step 3: Test your app locally

  1. To test your app locally, open a terminal and run python app.py:

    Bash
    python app.py
    Running on local URL: http://127.0.0.1:7860
    ...
  2. To view the app:

    • Navigate to http://127.0.0.1:7860 in a browser window.

      View the output of the hello world app locally

    • Alternatively, use the databricks apps run-local command to run and debug the app:

      Bash
      databricks 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.

  1. Run the following command to create the app in your Databricks workspace.

    Bash
    databricks 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.
  2. To view the app in your workspace when the create command completes, in the sidebar, click compute icon Compute, go to the Apps tab, and click the link to your app in the Name column.

    View the app details page after app creation

  3. Copy the command under Sync source files into Databricks.

  4. In a terminal, navigate to the directory that contains your app files and run the copied sync command.

    Bash
    databricks sync --watch . /Workspace/Users/my-email@org.com/gradio-hello-world
    ...
    Initial Sync Complete
  5. To view the synced files in your workspace when the sync command completes, click Workspace Icon Workspace in the sidebar and go to the directory created for your app.

  6. 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
  7. To view the deployment status, go to the overview page for the app.

    View the app details page after app deployment

    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.