Get started with Databricks Apps

Preview

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

  1. 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()
    
  2. 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

  1. 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
    ...
    
  2. To view the app’s output, open http://127.0.0.1:7860 in a browser window.

    View the output of the hello world app locally

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. Create the app in your Databricks workspace.

    Note

    • The name assigned to a Databricks app cannot be changed after creating the app, and any user with access to a 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 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.

  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. Sync the files from your local environment to your 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 Copy Icon to copy this command.

  4. 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 the sync command will skip that directory when transferring files.

    databricks sync --watch . /Workspace/Users/user@databricks.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, replacing <app-path> with the workspace path to your app files.

    databricks apps deploy gradio-hello-world --source-code-path <app-path>

  7. To view the deployment status, go to the details 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 details page.

    Link to the deployed app

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