Skip the tutorial and deploy the example now
Deploy a Shiny for Python App to Connect Cloud
This end-to-end example creates a Shiny for Python application and deploys it to Posit Connect Cloud. Code for the application is available on GitHub.
1. Build the application
Begin with a new project in your preferred development environment. In the terminal, create a virtual environment named venv
and activate it. This will isolate the specific packages we need for the the application.
-m venv venv
python /bin/activate source venv
Now we want to install the required packages for this application in the virtual environment. Create a requirements.txt
file, adding each package on a new line. This file helps install everything the application needs to run locally, as well as ultimately tell Connect Cloud what it needs to deploy the application.
faicons
shiny
shinywidgets
plotly
pandas ridgeplot
Once you have saved requirements.txt
, run the following command in the terminal to install all the packages in your virtual environment.
-r requirements.txt pip install
Create the primary file
Next, create a file named app.py
and add the following code for the Shiny application.
import faicons as fa
import plotly.express as px
import pandas as pd
# Load data and compute static values
from shiny import reactive, render
from shiny.express import input, ui
from shinywidgets import render_plotly
= pd.read_csv("data/tips.csv")
tips "styles.css")
ui.include_css(
= (min(tips.total_bill), max(tips.total_bill))
bill_rng
# Add page title and sidebar
="Restaurant tipping", fillable=True)
ui.page_opts(title
with ui.sidebar(open="desktop"):
ui.input_slider("total_bill",
"Bill amount",
min=bill_rng[0],
max=bill_rng[1],
=bill_rng,
value="$",
pre
)
ui.input_checkbox_group("time",
"Food service",
"Lunch", "Dinner"],
[=["Lunch", "Dinner"],
selected=True,
inline
)"reset", "Reset filter")
ui.input_action_button(
# Add main content
= {
ICONS "user": fa.icon_svg("user", "regular"),
"wallet": fa.icon_svg("wallet"),
"currency-dollar": fa.icon_svg("dollar-sign"),
"ellipsis": fa.icon_svg("ellipsis"),
}
with ui.layout_columns(fill=False):
with ui.value_box(showcase=ICONS["user"]):
"Total tippers"
@render.express
def total_tippers():
0]
tips_data().shape[
with ui.value_box(showcase=ICONS["wallet"]):
"Average tip"
@render.express
def average_tip():
= tips_data()
d if d.shape[0] > 0:
= d.tip / d.total_bill
perc f"{perc.mean():.1%}"
with ui.value_box(showcase=ICONS["currency-dollar"]):
"Average bill"
@render.express
def average_bill():
= tips_data()
d if d.shape[0] > 0:
= d.total_bill.mean()
bill f"${bill:.2f}"
with ui.layout_columns(col_widths=[6, 6, 12]):
with ui.card(full_screen=True):
"Tips data")
ui.card_header(
@render.data_frame
def table():
return render.DataGrid(tips_data())
with ui.card(full_screen=True):
with ui.card_header(class_="d-flex justify-content-between align-items-center"):
"Total bill vs tip"
with ui.popover(title="Add a color variable", placement="top"):
"ellipsis"]
ICONS[
ui.input_radio_buttons("scatter_color",
None,
"none", "sex", "smoker", "day", "time"],
[=True,
inline
)
@render_plotly
def scatterplot():
= input.scatter_color()
color return px.scatter(
tips_data(),="total_bill",
x="tip",
y=None if color == "none" else color,
color="lowess",
trendline
)
with ui.card(full_screen=True):
with ui.card_header(class_="d-flex justify-content-between align-items-center"):
"Tip percentages"
with ui.popover(title="Add a color variable"):
"ellipsis"]
ICONS[
ui.input_radio_buttons("tip_perc_y",
"Split by:",
"sex", "smoker", "day", "time"],
[="day",
selected=True,
inline
)
@render_plotly
def tip_perc():
from ridgeplot import ridgeplot
= tips_data()
dat "percent"] = dat.tip / dat.total_bill
dat[= input.tip_perc_y()
yvar = dat[yvar].unique()
uvals
= [[dat.percent[dat[yvar] == val]] for val in uvals]
samples
= ridgeplot(
plt =samples,
samples=uvals,
labels=0.01,
bandwidth="viridis",
colorscale="row-index",
colormode
)
plt.update_layout(=dict(
legend="h", yanchor="bottom", y=1.02, xanchor="center", x=0.5
orientation
)
)
return plt
# --------------------------------------------------------
# Reactive calculations and effects
# --------------------------------------------------------
@reactive.calc
def tips_data():
= input.total_bill()
bill = tips.total_bill.between(bill[0], bill[1])
idx1 = tips.time.isin(input.time())
idx2 return tips[idx1 & idx2]
@reactive.effect
@reactive.event(input.reset)
def _():
"total_bill", value=bill_rng)
ui.update_slider("time", selected=["Lunch", "Dinner"]) ui.update_checkbox_group(
Code in app.py
relies on two other files. A datafile named tips.csv
and a style sheet named styles.css
.
Create the data file
Let’s add a blank csv file to a new directory named data
.
mkdir data/tips.csv touch data
Open tips.csv
in your project and paste in this raw data.
Create the style file
Now create the new styles file.
touch styles.css
Open the file and copy and paste the code below.
:root {--bslib-sidebar-main-bg: #f8f8f8;
}
.popover {--bs-popover-header-bg: #222;
--bs-popover-header-color: #fff;
}
-close {
.popover .btnfilter: var(--bs-btn-close-white-filter);
}
Make sure that all your new files are saved.
Test the application
Now you have everything you need to test the application. Run the following code from the terminal.
shiny run
Your application should load locally.
Now that everything looks good and we’ve created a file to help reproduce your local environment, it is time to get the code on GitHub.
2. Push to GitHub
Sign in to GitHub and create a new, public repository.
Before syncing your project with the new repository, create a .gitignore
file so that you don’t add the virtual environment to the repo.
touch .gitignore
Add the virtual environment folder to the .gitignore file.
/ venv
You are now ready to sync the project with your new repo. Run the following lines in the terminal, replacing https://github.com/account-name/repo-name.git with your repository’s link.
git init .-m "first commit"
git commit -M main
git branch //github.com/account-name/repo-name.git
git remote add origin https:-u origin main git push
Finally, refresh your GitHub repository page. You should see everything that was in your local directory with the exception of the virtual environment folder, which was excluded in the .gitignore
file.
3. Deploy to Posit Connect Cloud
Follow the steps below to deploy your project to Connect Cloud.
Sign in to Connect Cloud.
Click the Publish icon button on the top of your Profile page
Select Shiny
Select the public repository that you created in this tutorial
Confirm the branch
Select app.py as the primary file
Click Publish
Publishing will display status updates during the deployment process. You will also find build logs streaming on the lower part of the screen.
Congratulations! You successfully deployed to Connect Cloud and are now able to share the link with others.
4. Republish the application
If you update the code to your application or the underlying data source, commit and push the changes to your GitHub repository.
Once the repository has the updated code, you can republish the application on Connect Cloud by going to your Content List and clicking the republish icon.