Case studies and industry use cases

Create and Deploy a Python Shiny Express Application to Connect Cloud

Written by Dan Chen
2024-11-07

Our latest video shows you how to create a dynamic Shiny for Python application in Positron and deploy it for free on Connect Cloud.

We built a simple app that lets users explore the Palmer Penguins dataset. Specifically, it generates a histogram of bill lengths, highlighting the distribution for a user-selected species.

Here’s the accompanying video if you want to follow along:

The Shiny Express Code

Shiny Express simplifies app development by allowing you to mix UI components and reactive logic directly within a single Python script. The resulting file should be named app.py.

#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [editor, viewer]
#| viewerHeight: 500
#| layout: vertical
from palmerpenguins import load_penguins
from plotnine import aes, geom_histogram, ggplot, theme_minimal
from shiny.express import input, render, ui

dat = load_penguins().dropna()
species = dat["species"].unique().tolist()
ui.input_radio_buttons("species", "Species", species, inline=True)
 
@render.plot
def plot():
    sel = dat[dat["species"] == input.species()]
    return (
        ggplot(aes(x="bill_length_mm"))
        + geom_histogram(dat, fill="#C2C2C4", binwidth=1)
        + geom_histogram(sel, fill="#447099", binwidth=1)
        + theme_minimal()
    )

The full code for this example is available on GitHub.

Free Deployment with Posit Connect Cloud

We use Posit Connect Cloud because it offers a free tier for hosting and sharing your applications. Deploying can be done in a few ways:

  1. GitHub Integration (Shown in Video):
    1. Place your app.py file in a GitHub repository.
    2. Connect Posit Connect Cloud to that repository.
    3. Connect Cloud will automatically discover the app and deploy it.

This method enables automatic redeployment! Any future updates you push to GitHub will be instantly reflected in your live application.

  1. Deploy from your IDE: Use the Posit Publisher Extension within Positron or VS Code to publish directly.

For detailed instructions, consult the official Posit Connect Cloud documentation on deploying Shiny applications.

Learn more

We’re excited to see what you create with Shiny Express!