Build a Python app¶
Build a small FastAPI app that displays an HTML page, then deploy it with the Snowflake CLI. Python apps run on serverless compute (Public Preview). For the Python version, dependency installation, and environment variables, see Python.
Before you begin¶
- Snowflake CLI with a configured connection.
- Serverless compute enabled for your account, as described in Create a serverless app.
- A role with the privileges described in Deploy with the CLI.
- To share the app, ask an account administrator to complete account setup first. Otherwise you deploy to your personal database.
Create the project¶
-
Create a project directory and open it:
-
Create
requirements.txtwith these dependencies:
Write the app code¶
Create app.py. The app imports dependencies from .python_packages and
listens on 0.0.0.0 at the port Snowflake provides in PORT:
Configure app.yml¶
Generate deployment settings. Setup resolves the database, schema, and warehouse from its options, then account defaults, then your personal database. It doesn’t fill the database from the active connection. See Create the app.
Keep the name, database, schema, warehouse, and ignore entries that setup
writes. Add compute_resource, runtime, install, and run
as shown below, and add .venv, .python_packages, and output to ignore.
The complete app.yml looks like this. Replace the placeholders with the
database, schema, and warehouse values from your generated file.
Make sure compute_resource: SERVERLESS and runtime: python are set; Python
apps run only on serverless compute.
ignore keeps your local .python_packages out of the source upload; the
remote build reinstalls dependencies into .python_packages and includes them
in the deployed package. The run command starts app.py.
If setup warns that HELLO_PYTHON already exists, choose a different app
name before deploying so you don’t replace an existing app.
Run locally¶
Install the dependencies into .python_packages with the same command the
remote build runs, then start the app:
Without PORT set, the app listens on port 8080. Open http://localhost:8080
to see the page.
Deploy and open the app¶
Deploy the app, then open it:
Sign in to Snowflake when prompted. The page displays Hello from Python and This FastAPI app runs on Snowflake App Runtime.
The first serverless deploy in an account can stop while Snowflake issues the TLS certificate. See Serverless deploy stops while the TLS certificate is issued.
Next steps¶
- Query your data: Query Snowflake from a Snowflake App Runtime app.
- Learn how serverless compute starts and stops: Serverless compute for Snowflake App Runtime.
- Share the app with other roles: Share a Snowflake App Runtime app.