Getting Started with Flask and RStudio Connect


Beginning in RStudio Connect 1.8.2+, it's possible to deploy and manage runtime settings for Flask applications. 

In this example, we'll deploy the getting started example provided by the Flask documentation to RStudio Connect.

For detailed user documentation, please review the RStudio Connect Flask user guide.


For this example to work, prerequisite configurations and setup include:


If needed, rsconnect-python is available on PyPI.  To install with pip, run the following command:

pip install rsconnect-python


Getting Started

Create a new application directory and add as shown below.
In this file, paste the code snippet below. This is a minimal application that will accept requests to the base endpoint route (/) and return the text Hello, World!

from flask import Flask

app = Flask(__name__)

def hello_world():
    return 'Hello, World!'
In this example, the application instance is denoted by app.

rsconnect-python will use a default entrypoint of app:app. If you put your code in and your application object is a variable named app, the default entrypoint will work. The route decorator references may not always be @app if Flask blueprints or other extensions are used.

It's important to identify, test, and validate the entrypoint as it relates to the rsconnect-python manifest.

Testing locally

It's recommended to test the application locally before deploying to RStudio Connect. This will help mitigate programmatic errors and also allows for a quicker application debugging experience.

Following the Flask documentation, identify the target application by setting the Flask environment variable:

Start the application locally:
python -m flask run
The output will resemble:
 * Serving Flask app ""
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on (Press CTRL+C to quit)
The application is ready to be deployed if it is running successfully in the local development environment.

Note, there are many configuration options for Flask.  It is important to review these options and customize if needed.  Always be mindful of deploying applications for a public audience with DEBUG=True.

As per the Flask Debug mode documentation:

Even though the interactive debugger does not work in forking environments (which makes it nearly impossible to use on production servers), it still allows the execution of arbitrary code. This makes it a major security risk and therefore it must never be used on production machines.


Configuring rsconnect-python

rsconnect-python must be present in the Python environment that the application is run in.

rsconnect-python will identify the Python version used in local machine based on the active Python environment (virtual or system). It's important to make sure that rsconnect-python correctly identifies the Python version that is being used to develop and run the active application.

Additionally, RStudio Connect must be configured for use with Python and will attempt to infer the correct version to use if the exact version match does not exist between the local environment and Connect server. 


Deploying with rsconnect-python

RStudio Connect requires a requirements.txt file to be present in the working application directory. The package information located here allows RStudio Connect to create an isolated environment on the server that mirrors the local development environment. Remember to always update the requirements.txt file if the application dependencies change.

To create this file using pip, run:

pip freeze > requirements.txt
The application directory will now show:
+ └─ requirements.txt
In requirements.txt, a similar listing should be present as below.
Note, the version numbers may be different. Also, other dependencies will be listed here if installed into the active Python environment.

 It's now possible to deploy the application from within the working directory using the command below once the requirements.txt is created:

rsconnect deploy api -n <server-name> .
The api argument instructs RStudio Connect to deploy this asset bundle as a Flask application.

The server name placeholder is a reference to a server alias created when managing server information in rsconnect-python on a per API key basis.

After deployment, the working directory will contain the following files and directory.
+ ├─ rsconnect-python
+   └─ <application-title>.json
  ├─ .gitignore
  └─ requirements.txt

If needed, the rsconnect-directory can safely be committed to the Git repository. The directory can be git ignored if it's not needed.

To ignore, add /rsconnect-python to the .gitignore file.

# .gitignore


Viewing the application in RStudio Connect

After deployment, the application will be available at the location shown in the terminal output. The application is now ready to be viewed on RStudio Connect. Access control, environment variables, and runtime settings can be configured similarly to the other publishable artifact types.

Note, that the application title will be that of the directory name that the application is deployed from. If a different title is desired, then exercise the --title argument available in rsconnect-python as part of the deployment command.

Publishing updates

It is possible to re-deploy the application if changes are made locally to the project files. Make sure to pip freeze the environment again if additional dependencies have been added since the last deployment.

To update, run the deploy command again:

rsconnect deploy api .


Managing the deployment process through Git or continuous integration

If needed, it is possible to leverage git, rsconnect-python, and Git backed deploy to automate this process. This is possible by deploying the Flask Application to RStudio Connect with Git and rsconnect-python


See also

Additional examples and walkthroughs of getting started with Flask application deployment on RStudio Connect: