python-examples icon indicating copy to clipboard operation
python-examples copied to clipboard

Examples of using Python with Posit Connect

Posit Connect & Python

Posit Connect is a publishing platform for the work your team creates in R and Python. This repository contains examples of Python content you can deploy to Connect, including:

Interactive apps

  • Streamlit
  • Dash
  • Flask
  • Voila

Web APIs

  • Flask
  • FastAPI

Documents

  • Jupyter Notebooks
  • Quarto Documents

Reticulate

Reticulate allows you to call Python from within an R session. This enables you to use models built in Python to power Shiny apps, visualize pandas dataframes with ggplot2, and much more.

Interactive apps

  • Serving Sentiment Analysis with Plumber and spaCy
  • Image Classification with PyTorch and Shiny

Documents

  • Visualizing pandas dataframes with ggplot2

Getting Started

You can deploy examples from this repo to your Connect server via git-backed deployment, or clone the repository and deploy examples from their manifests with the rsconnect CLI.

If you want to explore an example more closely before deploying it:

  • Clone this repository
  • create a virtual environment in the folder you want to work in
  • restore the needed packages into the virtual environment
$ cd flask-sentiment-analysis-api
$ python -m venv .venv
$ source .venv/bin/activate
$ python -m pip install -U pip setuptools wheel
$ python -m pip install -r requirements.txt

For reticulated content, set the RETICULATE_PYTHON environment variable to point to your virtual environment, by placing an .Renviron file in the folder containing the following:

RETICULATE_PYTHON=.venv/bin/python

Publishing basics

Overview:

  • Create and activate a virtual environment
  • Run the examples locally
  • Acquire an API key
  • Publish the examples with the rsconnect cli
  • Save the environment and deployment details for future git-backed publishing
rsconnect add \
    --api-key <MY-API-KEY> \
    --server <https://connect.example.org:3939> \
    --name <SERVER-NICKNAME>
rsconnect deploy api . -n <SERVER-NICKNAME>