Py‐Shiny - WFRCAnalytics/Resources GitHub Wiki
Py-Shiny is a framework for building interactive web applications in pure Python using the Shiny library by Posit.
Py-Shiny is a Python port of R's popular Shiny framework. You create web-based dashboards and applications without needing to write JavaScript or HTML/CSS.
If you know Python, you can build powerful, reactive, and interactive apps in minutes.
-
Reactive Programming Model: Build apps using reactive expressions and UI bindings.
-
Native Python: No need for HTML or JS (though you can integrate if you want to).
-
Data-Focused: Seamlessly integrate with Pandas, Plotly, Matplotlib, Altair, and other Python data tools.
-
Mapping Support: Create maps with ipyleaflet that can respond to user interactions.
-
Easy Deployment: Run locally, on servers, or deploy to WFRC's shiny app webspace.
Reactive programming connects inputs (like widgets) and outputs (like charts, text, or maps) so that when users interact with widgets (e.g., sliders, dropdowns, maps), the underlying data updates, and any connected UI elements update automatically (e.g., graphs refresh, tables re-calculate). No need for manual callbacks or refresh logic...it's all reactive.
To install Py-Shiny:
pip install shinyA minimal app.py:
from shiny import App, ui, render
app_ui = ui.page_fluid(
ui.input_slider("n", "Number of bins", 10, 100, 20),
ui.output_text_verbatim("txt")
)
def server(input, output, session):
@output
@render.text
def txt():
return f"Selected: {input.n()} bins"
app = App(app_ui, server)Run your first app:
shiny run --reload app.pymy-shiny-app/
│
├── app.py
├── data/
│ └── sample.csv
├── static/
│ └── style.css
└── utils/
└── helpers.py
-
Display Output: Use
@render.plot,@render.text,@render.table, etc. to show plots, text, tables, and more in your app. -
React to Changes: Use reactive.value() to store values that can change, and reactive.effect() to run code when those values update.
-
Organize Your Code: Keep things clean by splitting your app into sections:
- ui for layout
- server for logic
- optional helper functions for reusability
1. Inside python environment, install rsconnect-python and shiny libraries:
pip install rsconnect-python
pip install shiny
2. Get Your Account Info from ShinyApps.io
- Go to https://www.shinyapps.io/
- Log in to your account (shared wfrc account, see Chris for login information)
- Click your username → "Tokens"
- Click "Show" next to your token and secret
- Click the "With Python" tab
- Click Copy to clipboard
The token should look something like this:
rsconnect add --account <your-account-name> \
--name <a-nickname-for-your-account> \
--token <your-token> \
--secret <your-secret>
3. Set Up the Account in Python
In your terminal paste and run the connect script from previous step. This creates a local config file storing your credentials securely.
4. Deploy Your App
Run within app subfolder. The app name is the subfolder name: rsconnect deploy shiny -n wfrc .
Options:
--name: the app's unique ID on ShinyApps.io (in URL)
--title: display title of the app
You can add --python app.py if the file name is something different.
Your app should now be live at: https://wfrc.shinyapps.io/<your-app-name>
In Visual Studio Code, you can install the [Shiny extension] (https://marketplace.visualstudio.com/items?itemName=Posit.shiny)
- Run & Preview Apps: Launch Shiny apps directly in VS Code with built-in preview panel.
- Hot Reloading: Automatically reloads the app on code changes.
-
Intellisense Support: Autocompletion and tooltips for Shiny functions (
input,output,render, etc.). - Easy Debugging: One-click run/debug for Shiny apps from the editor.
-
App Detection: Identifies files containing Shiny
App()instances and adds Run buttons. - Environment Help: Prompts to install missing Shiny dependencies.
- Markdown & HTML Support: Syntax highlighting for UI code that mixes Python with HTML/Markdown.