Share
Embed
Transcript
22 slides
-
Joe Cheng (@jcheng) Posit PBC PyData Seattle 2023 Shiny
for Python Data-centric web applications in Python -
About Shiny • Create interactive apps and dashboards in Python
• Designed primarily with data scientists in mind • No HTML, CSS, or JavaScript skills required • Easy-to-use “reactive programming” approach to interactivity • But also designed with me in mind • Fully leverage HTML, CSS, and JavaScript skills if you have them • Reactive programming is powerful, fl exible, and deep—you’ll never outgrow it -
from shiny import App, render, ui import numpy as np
import matplotlib.pyplot as plt app_ui = ui.page_f i xed( ui.input_slider("n", "N", 0, 100, 20), ui.output_plot("plot"), ) def server(input, output, session) : @output @render.plot(alt="A histogram") def plot() : x = 100 + 15 * np.random.randn(input.n()) plt.hist(x, bins=7, density=True) app = App(app_ui, server) Anatomy of a Shiny app User interface Generates HTML to send to the browser Server logic Runs on the server, providing interactivity App object UI + server + options -
app_ui = ui.page_f i xed( ui.tags.h3("Air mass calculator"), ui.div( ui.markdown(
"""This Shiny app uses [Astropy](https: / /w ww .astropy.org/) to calculate t altitude (degrees above the horizon) and airmass (the amount of atmospher air along your line of sight to an object) of one or more astronomical objects, over a given evening, at a given geographic location. """ ), class_="mb-5", ), ui.row( ui.column( 8, ui.output_ui("timeinfo"), ui.output_plot("plot", height="800px"), class_="order-2 order - sm-1", ), ui.column( 4, -
class_="order-2 order - sm-1", ), ui.column( 4, ui.panel_well( ui.input_date("date", "Date"),
class_="pb-1 mb-3", ), ui.panel_well( ui.input_text_area( "objects", "Target object(s)", "M1, NGC35, PLX299", rows=3 ), class_="pb-1 mb-3", ), ui.panel_well( location_ui("location"), class_="mb-3", ), class_="order-1 order - sm-2", ), ), ) -
app_ui = ui.page_f i xed( ui.tags.h3("Air mass calculator"), ui.div( ui.markdown(
"""This Shiny app uses [Astropy](https: / /w ww .astropy.org/) to calculate the altitude (degrees above the horizon) and airmass (the amount of atmospheric air along your line of sight to an object) of one or more astronomical objects, over a given evening, at a given geographic location. """ ), class_="mb-5", ), ui.row( ui.column( 8, ui.output_ui("timeinfo"), ui.output_plot("plot", height="800px"), class_="order-2 order - sm-1", ), ui.column( 4, ui.panel_well( ui.input_date("date", "Date"), class_="pb-1 mb-3", ), ui.panel_well( ui.input_text_area( "objects", "Target object(s)", "M1, NGC35, PLX299", rows=3 ), class_="pb-1 mb-3", ), ui.panel_well( location_ui("location"), class_="mb-3", ), class_="order-1 order - sm-2", ), ), ) -
“Just so easy” script.py Streamlit st.slider() st.write() st.pyplot() Shiny app_ui
= def server() : app = App() -
Streamlit documentation, “Main concepts” “Streamlit’s architecture allows you to write
apps the same way you write plain Python scripts. To unlock this, Streamlit apps have a unique data flow: any time something must be updated on the screen, Streamlit reruns your entire Python script from top to bottom.” -
Streamlit is easy, until it’s not • Simple Streamlit apps:
top-to-bottom execution model • Yes, it really is that easy! • Nice output; hard to create bad-looking apps • Slow performance and limited interactivity • Less-simple Streamlit apps: top-to-bottom execution model plus caching, callbacks, and Session State • Danger zone: tricky to get right, leads to subtle bugs and complexity explosion -
Down the reactive programming rabbit hole Ten years in, we’re
still fi nding new techniques that “fall out” of reactive programming • Inputs/outputs • Selective suppression of reactive signals (isolate()) • Event handlers (@event) • Time-based reactivity (invalidate_later) • Dynamic dependency graphs • Pause/resume outputs and actions • Streaming inputs • File monitoring/database polling • Reactives as monads • Higher-order reactives • Reduce (over time, not space) • Throttle/debounce • Suppressing duplicate values -
Shinylive: Shiny + WebAssembly • Full stack Python web apps,
powered entirely within the browser • Demo: https://shinylive.io/py/examples/#cpu-info • Share via shinylive.io, or deploy on any static web host • Can be easily embedded within Quarto documents • Also powers all of our documentation -
Thank you! Learn more at https://shiny.posit.co/py/ Examples Articles API docs
Code samples at https://github.com/jcheng5/PyDataSeattle-2023