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.
Requirements
- A working installation of Python, Flask, and required dependencies on the machine used for development. This includes the installation and configuration of
rsconnect-python
using an RStudio Connect API key. - A running instance of RStudio Connect 1.8.2+ configured for use with Python.
rsconnect-python
If needed, rsconnect-python
is available on PyPI. To install with pip
, run the following command:
pip install rsconnect-python
Getting Started
app.py
as shown below..
└─ app.py
/
) and return the text Hello, World!# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
app
.rsconnect-python
will use a default entrypoint of app:app
. If you put your code in app.py
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
Following the Flask documentation, identify the target application by setting the Flask environment variable:
export FLASK_APP=app.py
python -m flask run
* Serving Flask app "app.py"
* 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 http://127.0.0.1:5000/ (Press CTRL+C to quit)
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
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
.
├─ app.py
+ └─ requirements.txt
requirements.txt
, a similar listing should be present as below.click==7.1.1
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.11.1
MarkupSafe==1.1.1
Werkzeug==1.0.0
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> .
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.
.
+ ├─ rsconnect-python
+ │ └─ <application-title>.json
├─ app.py
├─ .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 /rsconnect-python
Viewing the application in RStudio Connect
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
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.
Comments