Publish a Shiny for Python app using Posit Team
What is Shiny for Python?
Shiny is an open-source tool for building web applications with Python code. It enables you to customize the layout and style of your application and dynamically respond to events, such as a button press, or dropdown selection. If you are familiar with Shiny for R, then you’ll feel right at home with Shiny for Python! Check out these examples to get started.
What is Posit Connect
Posit Connect is a professional data science tool for hosting and sharing your data products, including Shiny for Python. It’s one part of Posit Team which also includes Posit Workbench for development work, and Posit Package Manager for managing your R and Python packages.
Publishing a Shiny for Python app to Posit Connect
Video Overview
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 it’s current state through the power of shinylive!
Take the code (copied below) and port it over to VS Code in a new app.py
file in a fresh directory we’ll call shiny4python
.
import matplotlib.pyplot as plt
import numpy as np
from shiny import App, render, ui
= ui.page_fluid(
app_ui
ui.layout_sidebar(
ui.panel_sidebar("n", "N", 0, 100, 20),
ui.input_slider(
),
ui.panel_main("histogram"),
ui.output_plot(
),
),
)
def server(input, output, session):
@output
@render.plot(alt="A histogram")
def histogram():
19680801)
np.random.seed(= 100 + 15 * np.random.randn(437)
x input.n(), density=True)
plt.hist(x,
= App(app_ui, server, debug=True) app
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 app.py
file) and run the following command from the terminal/shell:
shiny run app.py
Step 4: Deploy to Posit Connect
To deploy the application to Posit Connect, there are two things we need to do.
- Inform Posit Workbench which Posit Connect instance we are going to publish to.
- Supply an API key so Posit Connect knows who is publishing.
The rsconnect-python
package allows us add a Posit Connect server and store an API key. Full instructions are here, and the needed command, which 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 to arguments: the server name (-n
) and the name of the directory containing our app.py
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 (package names, package versions, Python version, etc.) is recorded and sent to Posit Connect to be 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!