Python + CLI¶
Step 1: Create the app¶
The first step is to create a new App. Every app gets its own service principal, a custom OAuth application, and a unique URL.
To create a new app you need to give it a name. The app name must be a valid DNS string, and at most 30 characters long. Example: my-first-app
. Optionally you can provide a nice app description as well for others to know what your app is about.
To create a new app, use the databricks apps create
command.
It only takes a few seconds to create the app. Once ready, the app should be in IDLE
state and have a valid URL.
If you go to the URL you'll see a App Not Available page. This is expected as you have not deployed your app yet!
Step 2: Write code¶
Developing an app for Databricks is no different than developing an app to host elsewhere. There is no magic. You can choose to develop your app in your favorite IDE or use the editor inside Databricks. We support all frameworks. To learn more, visit developing apps
To get started with python, the bare minimum you need is a python file. It can be named whatever you want as long as it is in the root of your app folder. Say app.py
. There are several pre-installed libraries, but if you need to install extra packages, you can add a requirements.txt
. By default, we use the command python {file_name}.py
to run your app. This may work in some cases, but for production and in frameworks like Streamlit, you may need a different command. Also, you may want to pass environment variables to the app to avoid hard-coding references to other Databricks resources such as warehouses and serving endpoints. For all of that, you can define an app.yaml file.
Below is a functioning gradio app copied from their documentation. It is not "production ready", but it will work!
import gradio as gr
def alternatingly_agree(message, history):
if len(history) % 2 == 0:
return f"Yes, I do think that '{message}'"
else:
return "I don't this is quite right"
gr.ChatInterface(alternatingly_agree).launch()
Step 3: Deploy the app¶
Code deployed needs to be in a folder in Databricks. You can use the CLI to upload your files or just create a new app.py
file (the name does not matter) and paste the source code. We recommend using the VS Code extension for Databricks to automatically sync the code.
To deploy, you create a new "deployment". Deployments are the process of preparing and downloading the source code, installing any packages, and running the app. For more details, check the deployment documentation.
You'll need to get the "full path" of the workspace directory you choose. It should look something like /Workspace/my-app
.
The first time you deploy, we provision your app compute, so it may take 2 to 3 minutes. After that, subsequent deployments should only take a few seconds.
After the deployment is complete, you can go to the app URL to access it.
Step 4: Share¶
In the UI (under Compute -> Apps), there is a permissions button. You can share your app with CAN_USE or CAN_MANAGE permissions.
Step 5: Troubleshoot¶
We print any information you'd need to troubleshoot together with the stdout and stderr of the app, navigate to https://<app URL>/logz
and you should see a live stream of logs.
Tip: Check out our talk at Data and AI Summit, where we demo developing, deploying, and troubleshooting an app.