Python API deployment with RStudio Connect: Flask
This is part one of our three part series
- Part 1: Python API deployment with RStudio Connect: Flask (this post)
- Part 2: Python API deployment with RStudio Connect: FastAPI
- Part 3: Python API deployment with RStudio Connect: Streamlit
RStudio recently announced they are changing to Posit. Their publishing platform RStudio Connect (-> Posit Connect) is well known for providing the ability to deploy and share R applications such as Shiny apps and Plumber APIs as well as plots, models and RMarkdown reports. However, it is not just for R developers. RStudio Connect also supports a growing number of Python applications. Indeed posit.co states they are
embracing multi-lingual data science, creating open source and commercial software for R, Python, and beyond.
One of the Python applications you can deploy to RStudio Connect is Flask. Flask is a WSGI (Web Server Gateway Interface) web application framework and provides a Python interface to enable the building of web APIs. It is useful to data scientists, for example for building interactive web dashboards and visualisations of data, as well as APIs for machine learning models. Deploying a Flask app to a publishing platform such as RStudio Connect means it can then be used from anywhere and can be easily shared with clients.
This blog post focuses on how to deploy a Flask app to RStudio Connect. We will use a simple example but won’t go into detail on how to create Flask apps. If you are getting started in Flask you may find this tutorial useful.
Creating your first Flask app
First things first, we will need to create a project directory and install Flask in a virtual environment.
# Create a project directory
mkdir flask-deploy-demo && cd flask-deploy-demo
# Create a virtual environment
python -m venv .venv
source .venv/bin/activate
# Install flask
pip install flask
For this example we will just use a basic hello world app. Create a file for the app with:
touch flask_demo.py
and copy in the code below.
# flask_demo.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
We can check then our app is working by running it locally. By default Flask looks for an application called app.py
or wsgi.py
in the current directory. As we have called our app something different we will need to first set a FLASK_APP
environment variable so that Flask knows where to look. You can set this variable in a .env
or .flaskenv
file and source it with,
echo 'export FLASK_APP="flask_demo"' >> .env
source .env
Now we can deploy our app locally with
flask run
and check it is working by following the output link http://127.0.0.1:5000.
Deploying to RStudio Connect
Adding a server
Now we have our Flask app we want to deploy it to a server such as RStudio Connect. To do this we will use the CLI (Command Line Interface) deployment tool rsconnect-python
. This can be installed with,
pip install rsconnect-python
We will then need to add the server which we want to deploy to. However, before we do this we will need to first create an API key. Log into RStudio Connect and click on your user icon in the top left corner, navigate to “API Keys” and add a new API key.
Remember to save the API key somewhere as it will only be shown to you once!
It is also useful to set an API key environment variable in our .env
file. This can be done by running
echo 'export CONNECT_API_KEY=<your_api_key>' >> .env
source .env
If you wish you could also add an environment variable for the server you are using,
CONNECT_SERVER=<your server url>
Note the server url will be the part of the url that comes before connect/
and must include a trailing slash.
Now we can add the server with,
rsconnect add --server $CONNECT_SERVER --name <server nickname> --api-key $CONNECT_API_KEY
You can check the server has been added and view its details with
rsconnect list
Deploying your Flask App
Before we deploy our app, there is one more thing to watch out for. Unless you have a requirements.txt
file in the same directory as your app, RStudio Connect will freeze your current environment. Therefore, make sure you run the deploy command from the virtual environment that you created your Flask app in and wish it to run in on the server.
Aside When writing this blog I found there is a bug in pip/ubuntu which adds pkg-resources==0.0.0
when freezing the environment. This causes an error when trying to deploy. To get around this you can create a requirements.txt
file for your current environment and exclude pkg-resources==0.0.0
with
pip freeze | grep -v "pkg-resources" > requirements.txt
We are now ready to deploy our app by running,
rsconnect deploy api -n <server nickname> . --entrypoint flask_demo:app
from the flask-deploy-demo
directory.
Here we have used the flag --entrypoint
. This tells RStudio Connect where our app is located. It is of the form module name:object name. By default RStudio Connect will look for an entrypoint of the form app:app so if you have used these names then you won’t need to specify the entrypoint flag.
And just like that our app is deployed and ready to share!
You can check your deployment was successful by following the “Dashboard content URL” which will take you to your published API on RStudio Connect.
Further reading
We hope you found this post useful!
If you wish to learn more about Flask or deploying applications to RStudio Connect you may be interested in the following links:
- Flask quickstart
- Publishing to RStudio Connect
- Recreating a Shiny app with Flask
- Introduction to RStudio Connect course