Skip to main content
AI in Production 2026 is now open for talk proposals.
Share insights that help teams build, scale, and maintain stronger AI systems.
items
Menu
  • About
    • Overview 
    • Join Us  
    • Community 
    • Contact 
  • Training
    • Overview 
    • Course Catalogue 
    • Public Courses 
  • Posit
    • Overview 
    • License Resale 
    • Managed Services 
    • Health Check 
  • Data Science
    • Overview 
    • Visualisation & Dashboards 
    • Open-source Data Science 
    • Data Science as a Service 
    • Gallery 
  • Engineering
    • Overview 
    • Cloud Solutions 
    • Enterprise Applications 
  • Our Work
    • Blog 
    • Case Studies 
    • R Package Validation 
    • diffify  

Python API deployment with RStudio Connect: Flask

Author: Parisa Gregg

Published: August 25, 2022

tags: python, rstudio, r, 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.

Do you use Professional Posit Products? If so, check out our managed Posit services

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.

user menu icon

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

Jumping Rivers Logo

Recent Posts

  • Start 2026 Ahead of the Curve: Boost Your Career with Jumping Rivers Training 
  • Should I Use Figma Design for Dashboard Prototyping? 
  • Announcing AI in Production 2026: A New Conference for AI and ML Practitioners 
  • Elevate Your Skills and Boost Your Career – Free Jumping Rivers Webinar on 20th November! 
  • Get Involved in the Data Science Community at our Free Meetups 
  • Polars and Pandas - Working with the Data-Frame 
  • Highlights from Shiny in Production (2025) 
  • Elevate Your Data Skills with Jumping Rivers Training 
  • Creating a Python Package with Poetry for Beginners Part2 
  • What's new for Python in 2025? 

Top Tags

  • R (236) 
  • Rbloggers (182) 
  • Pybloggers (89) 
  • Python (89) 
  • Shiny (63) 
  • Events (26) 
  • Training (23) 
  • Machine Learning (22) 
  • Conferences (20) 
  • Tidyverse (17) 
  • Statistics (14) 
  • Packages (13) 

Authors

  • Amieroh Abrahams 
  • Shane Halloran 
  • Russ Hyde 
  • Myles Mitchell 
  • Tim Brock 
  • Theo Roe 
  • Colin Gillespie 
  • Aida Gjoka 
  • Gigi Kenneth 
  • Osheen MacOscar 
  • Sebastian Mellor 
  • Keith Newman 
  • Pedro Silva 

Keep Updated

Like data science? R? Python? Stan? Then you’ll love the Jumping Rivers newsletter. The perks of being part of the Jumping Rivers family are:

  • Be the first to know about our latest courses and conferences.
  • Get discounts on the latest courses.
  • Read news on the latest techniques with the Jumping Rivers blog.

We keep your data secure and will never share your details. By subscribing, you agree to our privacy policy.

Follow Us

  • GitHub
  • Bluesky
  • LinkedIn
  • YouTube
  • Eventbrite

Find Us

The Catalyst Newcastle Helix Newcastle, NE4 5TG
Get directions

Contact Us

  • hello@jumpingrivers.com
  • + 44(0) 191 432 4340

Newsletter

Sign up

Events

  • North East Data Scientists Meetup
  • Leeds Data Science Meetup
  • Shiny in Production
British Assessment Bureau, UKAS Certified logo for ISO 9001 - Quality management British Assessment Bureau, UKAS Certified logo for ISO 27001 - Information security management Cyber Essentials Certified Plus badge
  • Privacy Notice
  • |
  • Booking Terms

©2016 - present. Jumping Rivers Ltd