Deploying a Shiny for Python application with Posit Connect

Text: What can you do with Posit Connect?

This is part 3 in our series of blog posts, “What can you do with Posit Connect?”

Posit Connect is a publishing platform for the authenticated sharing of R and Python data products in one convenient place that brings the power of data science to your entire organization. On the last Wednesday of every month, we host a Posit Team demo and Q&A session that is open to all. You can use this link to add the event to your calendar.

Last week’s blog post announced that Shiny for Python is now generally available and Python programmers can feel confident deploying Shiny for Python apps in production. This post will serve as a guide to deploying your applications so that users can interact with your apps and leverage their robust functionality using Posit Team.


What is Posit Team?

Posit Team is the bundle of our most popular products including Workbench (for development work), Connect (for hosting and sharing your data products), and Package Manager (for managing your R and Python packages). Together, this bundle delivers an end-to-end toolchain for data science teams committed to R and Python, improving every step of a data scientist’s workflow, from developing insights to deploying data products, to managing environments.


Follow along


Step 1: Load packages

We will use VS Code within Posit Workbench for this demo. In order to create a Shiny for Python application, we need to install shiny. Full instructions are here. We also need to install the rsconnect-python package which will allow us to publish our application to Posit Connect. Finally, there are two popular data science packages used by the shiny application that need to be installed, matplotlib and numpy. We’ll use pip for our installation tool.


pip install shiny rsconnect-python matplotlib numpy


Step 2: Create the app

For this example, we’ll use the example application found here. You can interact with this application in its current state through the power of shinylive! Take the code (copied below) and port it over to VS Code in a new file in a fresh directory we’ll call shiny4python.


import matplotlib.pyplot as plt
import numpy as np
from shiny import App, render, ui

app_ui = ui.page_fluid(
            ui.input_slider("n", "N", 0, 100, 20),

def server(input, output, session):
    @render.plot(alt="A histogram")
    def histogram():
        x = 100 + 15 * np.random.randn(437)
        plt.hist(x, input.n(), density=True)

app = App(app_ui, server, debug=True)


Step 3: Run the Application Locally

We can run the application within VS Code first to make sure it behaves as expected. To do this, make sure you are within the shiny4python directory (which contains the file) and run the following command from the terminal/shell:

shiny run


Step 4: Deploy to Posit Connect

To deploy the application to Posit Connect, there are two things we need to do.

  1. Inform Posit Workbench which Posit Connect instance we are going to publish to.
  2. Supply an API key so Posit Connect knows who is publishing.

The rsconnect-python package allows us to add a Posit Connect server and store an API key. Full instructions are here, and the needed command with placeholder values is copied below.


rsconnect add \
    --api-key api-key-goes-here \
    --server connect-server-url-goes-here \
    --name choose-a-name

Once added, you can view your saved Posit Connect instances by running rsconnect list. Now that we’ve added our API key and the Posit Connect URL, we can publish our app to Posit Connect! We need to supply two arguments: the server name (-n) and the name of the directory containing our file (shiny4python). If you are already within the shiny4python directory, you can use the ./ syntax to denote your current working directory.

rsconnect deploy shiny -n server-name ./

Once you run this command, you will see a few checks that take place (e.g., Validating server, Making bundle, etc.) followed by some logs. During this process, your environment (packages, package versions, Python version, etc.) is recorded and sent to Posit Connect and replicated before deploying your application. Once the stage is set, the application is deployed and you’ll see links at the bottom to view your application!


Learn more

We’d love to have you join us at one of our monthly Posit Team Demos where we walk through a different end-to-end data science workflow each month. They are on the last Wednesday of every month at 11 am ET on YouTube. You can add the event to your calendar here:

If you have any questions and would like to talk with our team, you can schedule a time to chat.