Python Apps and APIs

API/Application deployment options

You can deploy a variety of APIs and applications using sub-commands of the rsconnect deploy command.

  • api: WSGI-compliant APIs such as Flask and packages based on Flask
  • fastapi: ASGI-compliant APIs (FastAPI, Quart, Sanic, and Falcon)
  • shiny: Python Shiny apps
  • dash: Python Dash apps
  • streamlit: Streamlit apps
  • bokeh: Bokeh server apps

All options below apply equally to the api, fastapi, shiny, dash, streamlit, and bokeh sub-commands.

Including extra files

You can include extra files in the deployment bundle to make them available when your API or application is run by the Posit Connect server. Just specify them on the command line after the API or application directory:

rsconnect deploy api flask-api/ data.csv

Since deploying an API or application starts at a directory level, there will be times when some files under that directory subtree should not be included in the deployment or manifest. Use the --exclude option to specify files or directories to exclude.

rsconnect deploy dash \
    --exclude dash-app-venv \
    --exclude TODO.txt \
    dash-app/

You can exclude a directory by naming it:

rsconnect deploy dash \
    --exclude dash-app-venv \
    --exclude output/ \
    dash-app/

The --exclude option may be repeated, and may include a glob pattern. You should always quote a glob pattern so that it passes to rsconnect as-is instead of letting the shell expand it. If a file is specifically listed as an extra file that also matches an exclusion pattern, the file is still included in the deployment (i.e., extra files take precedence).

rsconnect deploy dash \
    --exclude dash-app-venv \
    --exclude "*.txt" \
    dash-app/

The following shows an example of an extra file taking precedence:

rsconnect deploy dash \
    --exclude "*.csv" \
    dash-app/ \
    important_data.csv

Some directories are excluded by default, to prevent bundling and uploading files that are not needed or might interfere with the deployment process:

.Rproj.user
.env
.git
.svn
.venv
__pycache__
env
packrat
renv
rsconnect-python
rsconnect
venv

Understanding entrypoints

APIs, Shiny apps, and Dash apps

When deploying a Python API, Shiny app, or Dash app, Connect needs to locate the application object. The location of the application object is called the entrypoint and is specified in two parts, separated by a colon: the module name and the object name (optional). For example, app:app if both are specified, or simply app if only the module name is specified.

The module name is the name of a Python module that contains the application. For example, if your application code is in a file named example.py in the application directory, then the module name would be example.

The object name is the name of the application object in your Python code. In a Flask API, for example, the application is typically created by a line of code that looks like:

# example.py
# ...
app = Flask(__name__)

In this case, the application object is named app, and the entrypoint would be example:app.

If the object_name is omitted, Connect will attempt to find a default application in the following order:

  • app
  • application
  • create_app
  • make_app
Note

create_app and make_app are expected to be factory functions, which should take no arguments and should return a valid application object.

If you are using rsconnect-python, the default entrypoint is app. If you put your code in app.py and your application object is one of the defaults listed above, the default entrypoint will work.

Otherwise, specify the entrypoint on the command line using the --entrypoint option. For example, if your code is in the file example.py and your application object is named myapp, use:

For Flask:

rsconnect deploy api \
    -n <saved server name> \
    --entrypoint example:myapp ...

For FastAPI:

rsconnect deploy fastapi \
    -n <saved server name> \
    --entrypoint example:myapp ...

Shiny for Python:

rsconnect deploy shiny 
    -n <saved server name> \
    --entrypoint example:myapp ...

If you are creating a manifest.json file for later deployment, you can use:

For Flask:

rsconnect write-manifest api --entrypoint example:myapp ...

For FastAPI:

rsconnect write-manifest fastapi --entrypoint example:myapp ...

Shiny for Python:

rsconnect write-manifest shiny --entrypoint example:myapp ...

Streamlit and Bokeh apps

For Streamlit and Bokeh apps, the entrypoint is the name of the Python file containing your app. For example, if your streamlit app’s source file is named main.py, use:

rsconnect deploy streamlit \
    -n <saved server name> \
    --entrypoint main.py ...

Creating a manifest for future deployment

You can create a manifest.json file for an API or application, then use that manifest in a later deployment. Use the write-manifest command to do this.

The write-manifest command also creates a requirements.txt file if it does not already exist or the --force-generate option is specified. It contains the package dependencies from the current Python environment, or from an alternative Python executable specified in the --python option, or via the RETICULATE_PYTHON environment variable.

Here is an example of the write-manifest command:

rsconnect write-manifest api my-api/