Beginning in RStudio Connect 1.8.2+ it is possible to deploy and manage runtime settings for Flask applications.
The rsconnect-python
package provides a command-line interface (CLI) based workflow that enables publishing Flask applications from the command line or continuous integration workflows. The package requires an API key associated with an operational installation of RStudio Connect.
This workflow provides a Python-only user the ability to create, publish, and update Flask applications running in RStudio Connect. Flask is a popular Python framework typically used to create standalone web applications or REST-based HTTP APIs.
In this example, we'll deploy the getting started example provided by the Flask documentation to RStudio Connect.
A pure Python approach to deploying Flask applications to RStudio Connect is possible by leveraging Git and the environment manifest (manifest.json
) created by rsconnect-python
.
Requirements
- A working installation of Git, 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
Git backed deployment with rsconnect-python
In addition to deploying from the command line, rsconnect-python
can generate a manifest.json
file that RStudio Connect can recognize to deploy directly from a Git repository.
This is beneficial for hardened deployment workflows and those that may require deployment as a successor to a continuous integration pipeline or testing suite.
The workflow is as follows:
- Create an application structure
- Initialize Git
- Generate
manifest.json
usingrsconnect-python
- Commit changes
- Import from Git in RStudio Connect
- Regenerate and commit
manifest.json
, if necessary - Trigger deploy in RStudio Connect or set to Check for updates periodically
If a virtual environment is used then it is encouraged to continue to do so. Integrate the above steps into your already functional workflow.
A requirements.txt
file along with a manifest.json
are mandatory for RStudio Connect to deploy content from a git repository. It is best practice to create and validate the requirements.txt
although rsconnect-python
will create one if not present.
Initialize git
To leverage Git backed deployment, a git
repository must be initialized within the working directory of the project. This will provide a mechanism for tracking changes to the code and also allow the repository to be pushed to a remote git server, if necessary.
In the working directory, run:
git init
Also, add a .gitignore
file into the directory.
. + └─ .gitignore
Create the Flask application structure
The application structure below is the minimal base structure required for deployment.
. + ├─ app.py + ├─ requirements.txt └─ .gitignore
Typically, a requirements.txt
file is generated using pip
or another package manager.
If pip
is used, then the following command will create the correct file:
pip freeze > requirements.txt
It is necessary to write a new manifest each time the application dependencies change. This instructs RStudio Connect to update the environment with the modified requirements.
Generate application manifest
Generate the manifest.json
by running the following command within the active environment that is configured for use with rsconnect-python
:
rsconnect write-manifest api .
This will create a manifest.json
file in the working directory. Note, there are additional options available in rsconnect-python
for specifying custom application entrypoint and directory locations.
Once executed, the working directory will contain the following files and directory.
. ├─ app.py + ├─ manifest.json ├─ .gitignore └─ requirements.txt
Inspecting manifest.json
The JSON content in the new manifest file will be similar to the below snippet:
{ "version": 1, "metadata": { "appmode": "python-api", "entrypoint": "app:app" }, "locale": "en_US.UTF-8", "python": { "version": "3.7.4", "package_manager": { "name": "pip", "version": "19.0.3", "package_file": "requirements.txt" } }, "files": { "requirements.txt": { "checksum": "f3993fe6b978850a694d8d64e83b233b" }, "app.py": { "checksum": "75e08453fa46fb6ee7835e323ce0860e" } } }
Note: The versions and hashes represented above may be different in your file.
metadata
"metadata": {
"appmode": "python-api",
"entrypoint": "app:app"
},
...
appmode
This instructs RStudio Connect that the application associated with this manifest is a Flask application. This is derived from the api
argument in the rsconnect write-manifest api
command.
entrypoint
The module:app reference that is executed on deployment. This is consistent with the naming convention used in the minimal application.
A file named app.py
that contains a Flask application instance named app
.
# app.py ... app = Flask(__name__) ...
The route decorator references may not always be @app
if Flask blueprints or other extensions are used. It is important to identify, test, and validate the entrypoint as it relates to the rsconnect-python
manifest.
python
The python entry identifies version and package manager information.
"python": { "version": "3.7.4", "package_manager": { "name": "pip", "version": "19.0.3", "package_file": "requirements.txt" } }, ...
It is important to validate that the correct Python version is recognized by rsconnect-python
. This is the version that RStudio Connect will try to match.
Commit and push
Commit the new files once the manifest is generated and the application is running correctly in the local development environment.
git add .
git commit -m 'initial structure with rstudio connect manifest'
Push these commits to the server hosting the remote origin of the Git repository.
Import from Git in RStudio Connect
Once pushed, the Flask application will be available to import into RStudio Connect following the documentation outlined here.
Update and redeploy
The process to update the deployed application is as follows:
- Commit updated code changes
- Regenerate and commit
manifest.json
, if necessary - Push code changes
- Trigger deploy in RStudio Connect or allow for automatic deployment triggered by the RStudio Connect Git update interval
Comments