Posit's Shiny for Python has reached maturity, bringing the beloved reactive framework to Python developers. Now you can build interactive dashboards using the same powerful paradigm in both R and Python—or even combine them in a single application.
Why Shiny for Python?
Shiny's reactive programming model makes building interactive applications intuitive:
- No JavaScript required for interactivity
- Automatic UI updates when data changes
- Familiar syntax for R Shiny users
- Full Python ecosystem integration
Your First Shiny Python App
from shiny import App, render, ui
app_ui = ui.page_fluid(
ui.input_slider("n", "Sample size", 10, 1000, 100),
ui.output_plot("histogram")
)
def server(input, output, session):
@output
@render.plot
def histogram():
import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(input.n())
plt.hist(data, bins=30, color='#667eea')
plt.title(f"Histogram of {input.n()} samples")
app = App(app_ui, server)
Comparing R and Python Shiny
R Version
library(shiny)
ui <- fluidPage(
sliderInput("n", "Sample size", 10, 1000, 100),
plotOutput("histogram")
)
server <- function(input, output, session) {
output$histogram <- renderPlot({
hist(rnorm(input$n), col = "#667eea",
main = paste("Histogram of", input$n, "samples"))
})
}
shinyApp(ui, server)
Python Version
The syntax is remarkably similar, making it easy for R users to transition:
# Python uses decorators instead of render functions
@output
@render.plot
def histogram():
# Python plotting code here
pass
Advanced Features
Reactive Values
from shiny import reactive
@reactive.Calc
def filtered_data():
df = load_data()
return df[df['category'] == input.category()]
@output
@render.table
def data_table():
return filtered_data()
Modules for Reusability
from shiny import module
@module.ui
def chart_ui(id):
return ui.card(
ui.output_plot(id)
)
@module.server
def chart_server(input, output, session, data):
@output
@render.plot
def plot():
return create_chart(data())
Mixing R and Python
With Shiny for Python and reticulate, you can use both languages:
# In R Shiny, call Python code
library(reticulate)
output$ml_prediction <- renderText({
py_model <- import("sklearn.ensemble")
model <- py_model$RandomForestClassifier()
# Use Python ML with R Shiny UI
})
Deployment Options
- Posit Connect — Enterprise deployment
- shinyapps.io — Cloud hosting
- Docker — Container deployment
- Hugging Face Spaces — Free hosting for demos
Rflow for Shiny Development
library(rflow)
# Generate Shiny apps from descriptions
rflow_ask("Create a Shiny dashboard with a sidebar
for filtering and three chart panels")
# Debug reactive issues
rflow_ask("Why isn't my reactive expression updating
when the input changes?")
# Convert between R and Python Shiny
rflow_ask("Convert this R Shiny app to Python Shiny")
Conclusion
Shiny for Python opens new possibilities for data scientists who work in both languages. Whether you're building a quick prototype or a production dashboard, Shiny's reactive model makes interactive data apps accessible to everyone.
Get started: pip install shiny
