Get started with Databricks Apps

Preview

Databricks Apps is in Public Preview.

This article explains how to set up your local development environment to develop apps and demonstrates how to build and deploy your first app with a step-by-step example.

Requirements

To deploy and run apps in your Databricks workspace, you must ensure that your firewall does not block the domain *.databricksapps.com.

To create Databricks Apps locally and deploy those apps to your Databricks workspace, your development environment must meet the following requirements:

  • Python 3.11 or above.

  • The Databricks CLI, version 0.229.0 or above, configured to access your Databricks workspace. To install or update and configure the Databricks CLI, see Install or update the Databricks CLI and Authentication for the Databricks CLI.

  • The Databricks SDK for Python. You can install the SDK with pip3:

    pip3 install databricks-sdk

    See Databricks SDK for Python.

  • (Optional) If your app needs to access Databricks SQL, install the Databricks SQL Connector for Python. You can install the connector with pip3:

    pip3 install databricks-sql-connector

  • Databricks recommends using a Python virtual environment when developing apps. The example in this article uses pipenv to create a virtual environment. See Python virtual environment.

Write and deploy your first Databricks app

The following steps walk you through creating a simple app in your local development environment and deploying 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.

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 use sensitive information when naming your Databricks apps.

    • Because the name of a Databricks app is used to construct the link to the deployed app, the name must use only characters that are valid in a URL.

    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.

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