### Install Shiny for Python Source: https://shiny.posit.co/py/index Installs the Shiny package for Python using pip. This is the first step to getting started with building reactive applications. ```bash pip install shiny ``` -------------------------------- ### Install Shiny using uv Source: https://shiny.posit.co/py/docs/install-create-run Installs the Shiny package into a virtual environment using the 'uv' package manager. ```Shell uv pip install shiny ``` -------------------------------- ### Install Pytest Source: https://shiny.posit.co/py/docs/unit-testing Installs the pytest framework using pip, the Python package installer. This is the first step to start writing and running tests. ```shell pip install pytest ``` -------------------------------- ### Install Shiny using uv pip Source: https://shiny.posit.co/py/get-started/install Installs the Shiny package into a virtual environment using the 'uv pip' command. ```shell uv pip install shiny ``` -------------------------------- ### Upgrade Pip and Install Wheel (Python) Source: https://shiny.posit.co/py/get-started/install Before installing Shiny, it's recommended to upgrade pip and install the wheel package for faster and more reliable installations. This ensures you have the latest tools for package management. ```bash pip install --upgrade pip wheel ``` -------------------------------- ### Upgrade Pip and Install Wheel (Python) Source: https://shiny.posit.co/py/docs/install-create-run Before installing Shiny, it's recommended to upgrade pip and install the wheel package for faster and more reliable installations. This ensures you have the latest tools for package management. ```bash pip install --upgrade pip wheel ``` -------------------------------- ### Install Shiny with Pip (Python) Source: https://shiny.posit.co/py/docs/comp-r-shiny This snippet shows how to create a virtual environment and install the Shiny package using pip in Python. It's recommended to use virtual environments for Python projects to manage dependencies effectively. ```bash python3 -m venv venv source venv/bin/activate pip install shiny ``` -------------------------------- ### Install Development Versions of Shiny and Htmltools (Python) Source: https://shiny.posit.co/py/docs/install-create-run This command installs the development versions of py-htmltools and py-shiny directly from their GitHub tarball archives. This is useful for testing the latest unreleased features or contributing to the projects. ```bash pip install https://github.com/posit-dev/py-htmltools/tarball/main pip install https://github.com/posit-dev/py-shiny/tarball/main ``` -------------------------------- ### Install Development Versions of Shiny and Htmltools (Python) Source: https://shiny.posit.co/py/get-started/install This command installs the development versions of py-htmltools and py-shiny directly from their GitHub tarball archives. This is useful for testing the latest unreleased features or contributing to the projects. ```bash pip install https://github.com/posit-dev/py-htmltools/tarball/main pip install https://github.com/posit-dev/py-shiny/tarball/main ``` -------------------------------- ### Install Shiny Package (Python - Pip) Source: https://shiny.posit.co/py/get-started/install This command installs the Shiny package from PyPI using pip. It's the standard way to get the latest stable release of Shiny for your Python projects. ```bash pip install shiny ``` -------------------------------- ### Install Shiny Package (Python - uv) Source: https://shiny.posit.co/py/docs/install-create-run This command installs the Shiny package using the 'uv pip' command. 'uv' is a fast Python package installer and resolver, offering an alternative to pip. ```bash uv pip install shiny ``` -------------------------------- ### Install Shiny Package (Python - Pip) Source: https://shiny.posit.co/py/docs/install-create-run This command installs the Shiny package from PyPI using pip. It's the standard way to get the latest stable release of Shiny for your Python projects. ```bash pip install shiny ``` -------------------------------- ### Install and Export Shinylive Application Source: https://shiny.posit.co/py/get-started/shinylive This section provides shell commands for installing the `shinylive` package, creating a new Shiny application directory, exporting the application for static hosting, and previewing it locally. The export process generates a `site` directory containing all necessary files for deployment. ```Shell pip install shinylive ``` ```Shell shiny create --dir myapp ``` ```Shell shinylive export myapp site ``` ```Shell python3 -m http.server --directory site 8008 ``` ```Shell shinylive export myapp1 site --subdir app1 shinylive export myapp2 site --subdir app2 ``` ```Shell pip install shinylive --upgrade shinylive assets remove # Remove old cached shinylive files ``` -------------------------------- ### Install Shiny Package (Python - uv) Source: https://shiny.posit.co/py/get-started/install This command installs the Shiny package using the 'uv pip' command. 'uv' is a fast Python package installer and resolver, offering an alternative to pip. ```bash uv pip install shiny ``` -------------------------------- ### Create and Activate uv Virtual Environment (Python) Source: https://shiny.posit.co/py/get-started/install This section shows how to create a virtual environment using 'uv venv' within your project directory. It covers creating the environment (which defaults to '.venv') and activating it for use. ```bash # Create and move into your shiny application directory mkdir myapp cd myapp # Create a virtual environment, defaults to .venv uv venv # Activate the virtual environment source .venv/bin/activate # .venv\Scripts\Activate.ps1 # on Windows (PowerShell) ``` -------------------------------- ### Basic Shiny App Input/Output Example (Python) Source: https://shiny.posit.co/py/docs/overview Demonstrates the fundamental mechanics of a Shiny app, including creating input components using ui.input_*() and rendering reactive outputs with @render.* decorators. This example uses shiny.express for reduced code. ```Python from shiny import ui, render, App app_ui = ui.page_fluid( ui.input_slider("count", "Count", 0, 100, 50), ui.output_text_verbatim("value") ) def server(input, output, session): @output @render.text def value(): return f"The current value is {input.count()}" app = App(app_ui, server) ``` -------------------------------- ### Per-File Pyright Configuration for Shiny Source: https://shiny.posit.co/py/docs/install-create-run Allows overriding Pyright/Pylance settings on a per-file basis within a Python project. This example sets strict type checking and disables unused function reporting for a specific file. ```Python # pyright: strict # pyright: reportUnusedFunction=false ``` -------------------------------- ### Create and Activate uv Virtual Environment (Python) Source: https://shiny.posit.co/py/docs/install-create-run This section shows how to create a virtual environment using 'uv venv' within your project directory. It covers creating the environment (which defaults to '.venv') and activating it for use. ```bash # Create and move into your shiny application directory mkdir myapp cd myapp # Create a virtual environment, defaults to .venv uv venv # Activate the virtual environment source .venv/bin/activate # .venv\Scripts\Activate.ps1 # on Windows (PowerShell) ``` -------------------------------- ### Create and Activate Conda Virtual Environment (Python) Source: https://shiny.posit.co/py/get-started/install This demonstrates how to create a new conda environment named 'shiny' and install the Shiny package along with the latest Python version from the 'conda-forge' channel. It also shows how to activate the newly created environment. ```bash # Create a conda environment named 'shiny' install the shiny package with latest python conda create --name shiny -c conda-forge shiny python # Activate the virtual environment conda activate shiny # Your prompt will be pre-pended with shiny ``` -------------------------------- ### Create and Activate Conda Virtual Environment (Python) Source: https://shiny.posit.co/py/docs/install-create-run This demonstrates how to create a new conda environment named 'shiny' and install the Shiny package along with the latest Python version from the 'conda-forge' channel. It also shows how to activate the newly created environment. ```bash # Create a conda environment named 'shiny' install the shiny package with latest python conda create --name shiny -c conda-forge shiny python # Activate the virtual environment conda activate shiny # Your prompt will be pre-pended with shiny ``` -------------------------------- ### Create and Activate Python Virtual Environment (Python) Source: https://shiny.posit.co/py/get-started/install This section demonstrates how to create a virtual environment named '.venv' for your Shiny application using Python's built-in 'venv' module. It includes steps to create the directory, initialize the environment, and activate it. ```bash # Create and move into your shiny application directory mkdir myapp cd myapp # Create a virtual environment in the .venv subdirectory python3 -m venv .venv # Activate the virtual environment source .venv/bin/activate # Your prompt will change to have the .venv prefix # Now install Shiny in your environment pip install shiny ``` -------------------------------- ### Run Shiny App with Uvicorn Source: https://shiny.posit.co/py/get-started/deploy-on-prem This command starts a Shiny application named 'app' using the Uvicorn server. It's configured to listen on all network interfaces (0.0.0.0) and port 80. This setup is similar to how FastAPI applications are served, but Shiny has specific requirements. ```Shell uvicorn app:app --host 0.0.0.0 --port 80 ``` -------------------------------- ### Create and Activate Python Virtual Environment (Python) Source: https://shiny.posit.co/py/docs/install-create-run This section demonstrates how to create a virtual environment named '.venv' for your Shiny application using Python's built-in 'venv' module. It includes steps to create the directory, initialize the environment, and activate it. ```bash # Create and move into your shiny application directory mkdir myapp cd myapp # Create a virtual environment in the .venv subdirectory python3 -m venv .venv # Activate the virtual environment source .venv/bin/activate # Your prompt will change to have the .venv prefix # Now install Shiny in your environment pip install shiny ``` -------------------------------- ### Python: Basic Shiny App with Slider and Text Output Source: https://shiny.posit.co/py/docs/comp-r-shiny Demonstrates a simple Shiny application in Python using `ui.page_fluid` for layout, `ui.input_slider` for user input, and `ui.output_text_verbatim` to display server-side rendered text. The server logic uses `@render.text` to process the slider input. ```Python from shiny import ui, render, App app_ui = ui.page_fluid( ui.input_slider("n", "N", 0, 100, 40), ui.output_text_verbatim("txt"), ) def server(input, output, session): @render.text def txt(): return f"n*2 is {input.n() * 2}" app = App(app_ui, server) ``` -------------------------------- ### Basic Shiny App Structure in R Source: https://shiny.posit.co/py/docs/comp-r-shiny This is a standard Shiny application structure in R, defining a user interface with a slider input and a text output, and a server function to render the text based on the input. It uses `renderText` for output and `shinyApp` to run the application. ```R library(shiny) ui <- fluidPage( sliderInput("n", "N", 0, 100, 40), verbatimTextOutput("txt") ) server <- function(input, output, session) { output$txt <- renderText({ paste0("n*2 is ", input$n, " * 2") }) } shinyApp(ui, server) ``` -------------------------------- ### Shiny App with render.text Example Source: https://shiny.posit.co/py/api/render A complete Shiny application example demonstrating the use of render.text. It includes UI elements for text input and output, and server logic to update the text outputs reactively based on user input. This example showcases rendering text in different output types like ui.output_text and ui.output_text_verbatim. ```python from shiny import App, Inputs, Outputs, Session, render, ui app_ui = ui.page_fluid( ui.input_text("txt", "Enter the text to display below:", "delete me"), ui.row( ui.column(6, ui.code("ui.output_text()"), ui.output_text("text")), ui.column( 6, ui.code("ui.output_text_verbatim(placeholder=True)"), ui.output_text_verbatim("verb", placeholder=True), ), ), ui.row( ui.column(6), ui.column( 6, ui.code("ui.output_text_verbatim(placeholder=False)"), ui.output_text_verbatim("verb_no_placeholder", placeholder=False), ), ), ) def server(input: Inputs, output: Outputs, session: Session): @render.text def text(): return input.txt() @render.text def verb(): return input.txt() @render.text def verb_no_placeholder(): return input.txt() app = App(app_ui, server) ``` -------------------------------- ### R: Basic Shiny App with Slider and Text Output Source: https://shiny.posit.co/py/docs/comp-r-shiny Illustrates a basic Shiny application in R, utilizing `fluidPage` for layout, `sliderInput` for user interaction, and `verbatimTextOutput` to display computed results. The server function uses `renderText` to generate the output based on the slider's value. ```R library(shiny) ui <- fluidPage( sliderInput("n", "N", 0, 100, 40), verbatimTextOutput("txt") ) server <- function(input, output) { output$txt <- renderText({ paste0("n*2 is ", input$n," * 2") }) } shinyApp(ui, server) ``` -------------------------------- ### Create Shiny Dashboard Template Source: https://shiny.posit.co/py/docs/overview Demonstrates how to use the Shiny CLI to create a new dashboard project using a starter template. This is a common starting point for building Shiny applications. ```Shell shiny create --template dashboard ``` -------------------------------- ### Create and Run Shiny Applications (Python) Source: https://shiny.posit.co/py/api/core/Inputs Learn how to create and run Shiny applications using the `shiny` library. This includes setting up the application structure with inputs and outputs, and managing the session. ```Python from shiny import App, render, ui app_ui = ui.page_fluid( ui.h2("My First Shiny App"), ui.input_slider("n", "Number of bins", 0, 100, 50), ) def server(input, output, session): @output @render.text def txt(): return f"You have selected {input.n()}!" app = App(app_ui, server) # To run the app, save this code as app.py and run 'shiny run app.py' in your terminal. ``` -------------------------------- ### Install Shiny with Conda (Python) Source: https://shiny.posit.co/py/docs/comp-r-shiny This snippet demonstrates how to create and activate a conda environment, then install the Shiny package from the conda-forge channel. Conda environments help manage package dependencies, especially in data science workflows. ```bash conda create --name myenv conda activate myenv conda install -c conda-forge shiny ``` -------------------------------- ### Shinylive: Static Hosting with WebAssembly Source: https://shiny.posit.co/py/get-started/deploy Convert Shiny apps to static web pages (HTML, CSS, JavaScript) by running Python logic in the browser via WebAssembly. This simplifies hosting, reduces costs, and offers high scalability. ```Python # Example of a Shiny app that can be converted to Shinylive from shiny import App, render, ui app_ui = ui.page_fluid( ui.h2("Shinylive Example"), ui.input_slider("n", "Number of bins", 0, 100, 20), ui.output_plot("distPlot") ) def server(input, output, session): @output @render.plot def distPlot(): import matplotlib.pyplot as plt import numpy as np x = np.random.randn(1000) plt.hist(x, bins=input.n()) return plt.gcf() app = App(app_ui, server) ``` -------------------------------- ### Install Package with Micropip in Shinylive Source: https://shiny.posit.co/py/get-started/shinylive Demonstrates how to install a Python package from PyPI or a direct URL using Pyodide's `micropip` within a Shinylive environment. This is useful for adding packages not included in the default Shinylive distribution. ```Python import micropip await micropip.install("mypackage") import mypackage ``` ```Python import micropip await micropip.install("https://example.com/mypackage-1.0-py3-none-any.whl") ``` -------------------------------- ### Install rsconnect-python package Source: https://shiny.posit.co/py/get-started/deploy-cloud Installs the rsconnect-python package, which is required for deploying Shiny applications to shinyapps.io. Ensure you have version 1.22.0 or later for Shiny Express applications. ```Shell pip install rsconnect-python ``` -------------------------------- ### Shiny Counter Example: Incrementing/Decrementing Values (R vs. Python) Source: https://shiny.posit.co/py/docs/comp-r-shiny Provides a practical example of using reactive values to implement a counter with increment and decrement buttons in Shiny for both R and Python. It demonstrates event handling for button clicks and updating a displayed value. ```R library(shiny) ui <- fluidPage( actionButton("minus", "-1"), actionButton("plus", "+1"), br(), textOutput("value") ) server <- function(input, output, session) { value <- reactiveVal(0) observeEvent(input$minus, { newValue <- value() - 1 value(newValue) }) observeEvent(input$plus, { newValue <- value() + 1 value(newValue) }) output$value <- renderText({ value() }) } shinyApp(ui, server) ``` ```Python from shiny import * app_ui = ui.page_fluid( ui.input_action_button("minus", "-1"), ui.input_action_button("plus", "+1"), ui.br(), ui.output_text("value"), ) def server(input: Inputs, output: Outputs, session: Session): val = reactive.value(0) @reactive.effect @reactive.event(input.minus) def _(): newVal = val() - 1 val.set(newVal) @reactive.effect @reactive.event(input.plus) def _(): newVal = val() + 1 val.set(newVal) @render.text def value(): return str(val()) app = App(app_ui, server) ``` -------------------------------- ### Create a New Shiny Dashboard Project Source: https://shiny.posit.co/py/index Creates a new Shiny project using the 'dashboard-tips' template. This command-line interface command helps bootstrap a new application with a pre-defined structure. ```bash shiny create --template dashboard-tips ``` -------------------------------- ### Deploy Shiny Application via Command Line Source: https://shiny.posit.co/py/get-started/deploy-on-prem Deploys a Shiny application to Posit Connect using the configured server nickname. This command initiates the deployment process from your local machine or CI/CD system. ```Shell rsconnect deploy shiny -n my-connect ``` -------------------------------- ### Run a Shiny Application from Command Line Source: https://shiny.posit.co/py/get-started/create-run Execute Shiny applications from the command line using `shiny run`. Specify the entry point file (e.g., `app.py`) and use flags like `--reload` for automatic updates and `--launch-browser` to open in a web browser. ```Shell shiny run --reload --launch-browser app_dir/app.py ``` -------------------------------- ### Python: Accessing Reactive Values with Function Calls Source: https://shiny.posit.co/py/docs/comp-r-shiny Shows how to access reactive values in Shiny for Python by treating them as function calls. This example defines a reactive calculation `n` using `@reactive.calc` and then uses it within a text rendering function `@render.text`. ```Python from shiny import ui, render, reactive, App app_ui = ui.page_fluid( ui.input_slider("n", "N", 0, 100, 40), ui.output_text_verbatim("txt"), ) def server(input, output, session): @reactive.calc def n(): return input.n() @render.text def txt(): return f"n*2 is {n() * 2}" app = App(app_ui, server) ``` -------------------------------- ### Shiny for Python: Application Creation and Execution Source: https://shiny.posit.co/py/api/core/module Covers the core components for creating and running Shiny for Python applications. This includes functions for starting applications and classes for defining application inputs, outputs, and sessions. ```Python run_app App Inputs Outputs Session ``` -------------------------------- ### Run Shiny App with FastAPI Stack Source: https://shiny.posit.co/py/get-started/deploy-on-prem This command demonstrates how to run a Shiny application using the same stack as FastAPI, which includes Starlette, ASGI, and Uvicorn. This highlights the interoperability and shared foundation between Shiny for Python and FastAPI. ```bash uvicorn main:app --reload ``` -------------------------------- ### Shiny for Python: Create and Run Applications Source: https://shiny.posit.co/py/api/core/Renderer This snippet lists the key components for creating and running Shiny for Python applications. It includes functions for starting applications and defining core application elements like inputs, outputs, and sessions. ```Python run_app App Inputs Outputs Session ``` -------------------------------- ### Shiny for Python: Application Creation and Execution Source: https://shiny.posit.co/py/api/core/reactive Covers the core components for creating and running Shiny applications, including the `run_app` function, `App` class, and concepts of Inputs, Outputs, and Session. ```Python from shiny import App, reactive, ui # Basic Shiny App structure app_ui = ui.page_fluid( ui.h1("My Shiny App"), ui.input_slider("n", "N", min=0, max=100, value=50), ui.output_text_verbatim("output_text") ) # Server logic def server(input, output, session): @output @render.text def output_text(): return f"You selected {input.n()}" # Create the App instance app = App(app_ui, server) # To run the app, you would typically use: # from shiny import run_app # run_app(app) # Concepts: # Inputs: User-provided values (e.g., input.n()) # Outputs: Values rendered to the UI (e.g., output.output_text()) # Session: Represents a client connection, provides access to session-specific info ``` -------------------------------- ### R: Accessing Reactive Values with Input Syntax Source: https://shiny.posit.co/py/docs/comp-r-shiny Demonstrates the R syntax for accessing reactive values and expressions. Reactive values like `input$n` are accessed directly, while reactive expressions are called as functions. This example uses `reactive({...})` to create a reactive expression `n`. ```R library(shiny) ui <- fluidPage( sliderInput("n", "N", 0, 100, 40), verbatimTextOutput("txt") ) server <- function(input, output) { n <- reactive({input$n}) output$txt <- renderText({ paste0("n*2 is ", n()," * 2") }) } shinyApp(ui, server) ``` -------------------------------- ### Shiny Gallery: CPU Usage Monitor App Source: https://shiny.posit.co/py/gallery Presents the CPU usage monitor application, a Shiny app in Python. Includes links to view the live app and its source code. ```Python # View app # View source ``` -------------------------------- ### Install Shiny Package (Python - Conda) Source: https://shiny.posit.co/py/get-started/install This command installs the Shiny package from the 'conda-forge' channel using the conda package manager. Conda is often used for managing complex dependencies, especially in data science. ```bash conda install -c conda-forge shiny ``` -------------------------------- ### Create Image Describer App Template Source: https://shiny.posit.co/py/docs/genai-inspiration This command initiates the creation of a Shiny application from a template designed to describe images. The application takes an image URL as input, uses an LLM to generate a description and related tags, and streams the content to `MarkdownStream()` and potentially other components. ```Shell shiny create --github jonkeane/shinyImages ``` -------------------------------- ### Install Shiny Package (Python - Conda) Source: https://shiny.posit.co/py/docs/install-create-run This command installs the Shiny package from the 'conda-forge' channel using the conda package manager. Conda is often used for managing complex dependencies, especially in data science. ```bash conda install -c conda-forge shiny ``` -------------------------------- ### Shiny Gallery: Restaurant Tips Dashboard App Source: https://shiny.posit.co/py/gallery Features the Restaurant tips dashboard, a Shiny application for Python. Includes links to access the live app and its underlying source code. ```Python # View app # View source ``` -------------------------------- ### Upgrade Shiny and Htmltools (Python - Pip) Source: https://shiny.posit.co/py/get-started/install If you have Shiny already installed, this command upgrades both the Shiny and htmltools packages to their latest versions. This is useful for accessing new features or bug fixes. ```bash pip install --upgrade shiny htmltools ``` -------------------------------- ### Upgrade Shiny and Htmltools (Python - Pip) Source: https://shiny.posit.co/py/docs/install-create-run If you have Shiny already installed, this command upgrades both the Shiny and htmltools packages to their latest versions. This is useful for accessing new features or bug fixes. ```bash pip install --upgrade shiny htmltools ``` -------------------------------- ### Shiny Core Dashboard Example Source: https://shiny.posit.co/py/docs/express-or-core This snippet showcases a Shiny Core implementation of a dashboard application. It is provided as a reference to compare with a Shiny Express version, illustrating the structural differences and maintainability advantages of Shiny Core for larger projects. ```Python # Example of a Shiny Core dashboard app (conceptual, actual code not provided in source text) # from shiny import App, render, ui # app_ui = ui.page_fluid( # ui.h2("Shiny Core Dashboard"), # ui.input_slider("n", "N", min=0, max=100, value=50), # ui.output_text_verbatim("txt") # ) # def server(input, output, session): # @output # @render.text # def txt(): # return f"You have selected {input.n()}" # app = App(app_ui, server) ``` -------------------------------- ### Update Shiny Package (Python - Conda) Source: https://shiny.posit.co/py/get-started/install If you already have Shiny installed via conda, this command updates it to the latest version available on the 'conda-forge' channel. This ensures you are using the most current stable release. ```bash conda update -c conda-forge shiny ``` -------------------------------- ### Update Shiny Package (Python - Conda) Source: https://shiny.posit.co/py/docs/install-create-run If you already have Shiny installed via conda, this command updates it to the latest version available on the 'conda-forge' channel. This ensures you are using the most current stable release. ```bash conda update -c conda-forge shiny ``` -------------------------------- ### Get Initial URL Hash Source: https://shiny.posit.co/py/api/Session Retrieves the initial hash part of the URL when the session started. ```python session.ClientData.url_hash_initial() ``` -------------------------------- ### Create Shiny app from template using command line Source: https://shiny.posit.co/py/get-started/what-is-shiny Uses the shiny create command to generate a new Shiny application from a pre-built template. This allows for quick setup of common application types like dashboards. ```shell shiny create --template dashboard ``` -------------------------------- ### Create Workout Plan App Template Source: https://shiny.posit.co/py/docs/genai-inspiration This command creates a Shiny application using the 'workout-plan' template, which generates personalized workout plans based on user input. The application utilizes an LLM to create the plans and streams the output using `MarkdownStream()`. ```Shell shiny create --template workout-plan --github posit-dev/py-shiny-templates/gen-ai ``` -------------------------------- ### Create a Shiny Application Source: https://shiny.posit.co/py/get-started/create-run Use the `shiny create` command to generate a new Shiny application. This command prompts for details and provides boilerplate code for a working app, with options for starter templates. ```Shell shiny create ``` -------------------------------- ### Pyright Configuration for Shiny Type Checking (pyrightconfig.json) Source: https://shiny.posit.co/py/get-started/install Configures Pyright type checking mode to 'basic' and disables 'reportUnusedFunction' warnings in the pyrightconfig.json file. ```json { "typeCheckingMode": "basic", "reportUnusedFunction": "none" } ``` -------------------------------- ### Strict Type Checking in VS Code Source: https://shiny.posit.co/py/get-started/install Sets the Python analysis type checking mode to 'strict' in VS Code settings for more comprehensive type checking. ```json { "python.analysis.typeCheckingMode": "strict" } ``` -------------------------------- ### Shiny Gallery: Superzip Explorer App Source: https://shiny.posit.co/py/gallery Demonstrates the Superzip explorer application, a Shiny app for Python. Provides links to view the live application and its source code. ```Python # View app # View source ``` -------------------------------- ### Get NavSetArg Value Source: https://shiny.posit.co/py/api/core/MiscTypes Provides an example of the get_value method for NavSetArg, which retrieves the value of a navigation item. This value helps determine the default selected item in a navigation container. ```Python types.NavSetArg.get_value() ``` -------------------------------- ### Shiny for Python: Application Creation and Execution Source: https://shiny.posit.co/py/api/core Functions and classes for building and running Shiny applications. `run_app` starts the application, while `App` creates a Shiny app instance. `Inputs`, `Outputs`, and `Session` provide interfaces for managing application inputs, outputs, and session-related information respectively. ```python run_app() App() Inputs Outputs Session ``` -------------------------------- ### Per-file Type Checking Configuration for Shiny Source: https://shiny.posit.co/py/get-started/install Applies specific Pyright type checking settings directly within a Python file using comments, overriding global or project-level configurations. ```python # pyright: strict # pyright: reportUnusedFunction=false ``` -------------------------------- ### Async Computation with Progress Updates Source: https://shiny.posit.co/py/components/display-messages/progress-bar Demonstrates updating a Shiny progress bar within an asynchronous computation. It shows setting initial and final messages to indicate the start and completion of the task. ```Python async def compute(): with ui.Progress(min=1, max=15) as p: p.set(1, message="Here we go") # computation() p.set(15, message="Finished!") ``` -------------------------------- ### Create and Run Shiny Applications Source: https://shiny.posit.co/py/api/core/Outputs Learn how to create and run Shiny applications using Python. This section covers the basic structure of a Shiny app, including inputs, outputs, and session management. ```Python from shiny import App, Inputs, Outputs, Session app = App(ui=..., server=...) def server(input: Inputs, output: Outputs, session: Session): pass # To run the app: # app.run() ``` -------------------------------- ### Upgrade Shiny and Htmltools (Python - uv) Source: https://shiny.posit.co/py/get-started/install This command upgrades both the Shiny and htmltools packages to their latest versions using 'uv pip'. It's an efficient way to update your project's dependencies. ```bash uv pip install --upgrade shiny htmltools ``` -------------------------------- ### Upgrade Shiny and Htmltools (Python - uv) Source: https://shiny.posit.co/py/docs/install-create-run This command upgrades both the Shiny and htmltools packages to their latest versions using 'uv pip'. It's an efficient way to update your project's dependencies. ```bash uv pip install --upgrade shiny htmltools ``` -------------------------------- ### Create and Run Shiny Applications in Python Source: https://shiny.posit.co/py/api/core/Session Learn how to create and run interactive web applications using Shiny for Python. This section covers the fundamental steps and components involved in building a Shiny app, including inputs, outputs, and session management. ```Python from shiny import App, render, ui app_ui = ui.page_fluid( ui.h2("My First Shiny App"), ui.input_slider("n", "Number of observations", 0, 100, 20), ) def server(input, output, session): @output @render.text def txt(): return f"You have selected {input.n()}!"; app = App(app_ui, server) # To run the app, save this code as app.py and run 'shiny run app.py' in your terminal. ``` -------------------------------- ### VS Code Settings for Shiny Type Checking (settings.json) Source: https://shiny.posit.co/py/get-started/install Configures Python type checking mode to 'basic' and disables 'reportUnusedFunction' warnings in VS Code's settings.json file for a smoother Shiny development experience. ```json { "python.analysis.typeCheckingMode": "basic", "python.analysis.diagnosticSeverityOverrides": { "reportUnusedFunction": "none" } } ``` -------------------------------- ### Shiny Gallery: Orbit Simulation App Source: https://shiny.posit.co/py/gallery Showcases the Orbit simulation application, a Shiny app for Python. Users can view the live app and its source code through provided links. ```Python # View app # View source ``` -------------------------------- ### Strict Type Checking in VS Code for Shiny Source: https://shiny.posit.co/py/docs/install-create-run Enables 'strict' type checking mode in VS Code for Python projects using Shiny, providing more rigorous code analysis. This setting can be applied globally or per-file. ```JSON { "python.analysis.typeCheckingMode": "strict" } ``` -------------------------------- ### Shiny Core: Single Selectize Input Source: https://shiny.posit.co/py/components/inputs/selectize-single Provides an example of implementing a single-selectize input in a standard Shiny Core application. It includes the UI setup with `ui.page_fluid` and `ui.input_selectize`, and the server logic to render the selected value. ```python from shiny import App, render, ui app_ui = ui.page_fluid( ui.input_selectize( "selectize", "Select an option below:", {"1A": "Choice 1A", "1B": "Choice 1B", "1C": "Choice 1C"}, ), ui.output_text("value"), ) def server(input, output, session): @render.text def value(): return f"{input.selectize()}" ``` -------------------------------- ### Shiny Gallery: Shiny Wordle App Source: https://shiny.posit.co/py/gallery Highlights the Shiny Wordle application, a Python-based Shiny app. Offers links to view the live application and its source code. ```Python # View app # View source ``` -------------------------------- ### Pyright Configuration for Shiny Type Checking Source: https://shiny.posit.co/py/docs/install-create-run Configures Pyright settings, often used in `pyrightconfig.json`, for Shiny development. It enables 'basic' type checking and suppresses 'reportUnusedFunction' warnings, aligning with Shiny's idiomatic use of functions. ```JSON { "typeCheckingMode": "basic", "reportUnusedFunction": "none" } ``` -------------------------------- ### Shiny for Python: Application Creation and Execution Source: https://shiny.posit.co/py/api/Session Demonstrates how to create and run Shiny for Python applications, including defining inputs, outputs, and sessions. ```Python from shiny import App, Inputs, Outputs, Session, ui, reactive # Define UI app_ui = ui.page_fluid( ui.h1("My Shiny App"), ui.input_slider("n", "Number of bins", min=5, max=50, value=30), ui.output_plot("distPlot") ) # Define server logic def server(input: Inputs, output: Outputs, session: Session): @output @render.plot def distPlot(): # plot code here pass # Create the Shiny app app = App(app_ui, server) # To run the app, you would typically use: # app.run() ``` -------------------------------- ### Python Shiny: Running an App Source: https://shiny.posit.co/py/api/render Shows the basic structure for creating and running a Shiny application using `App`. This includes defining the UI and server components. ```Python from shiny import ui, render, App app_ui = ui.page_fluid( "Hello, Shiny!" ) def server(input, output, session): pass app = App(app_ui, server) # To run the app, save this code as app.py and run 'shiny run app.py' in your terminal. ``` -------------------------------- ### Create Shiny App (Express Mode) Source: https://shiny.posit.co/py/templates/basic-navigation Creates a new Shiny for Python application using the 'basic-navigation' template in Express mode. This command initializes a project structure suitable for demonstrating multi-panel navigation. ```shell shiny create --template basic-navigation --mode express --github posit-dev/py-shiny-templates ``` -------------------------------- ### Update Row Selection in Shiny App Source: https://shiny.posit.co/py/components/outputs/data-table Provides an example of updating row selection in a Shiny data table using the `.update_cell_selection()` method within a standard Shiny application structure. It covers UI setup and server logic for managing row selections. ```python from palmerpenguins import load_penguins from shiny import App, reactive, render, ui penguins = load_penguins() app_ui = ui.page_fluid( ui.h2("Palmer Penguins"), ui.input_action_button("update_selection", "Update selection"), ui.input_action_button("reset_selection", "Reset selection"), ui.h5("Current selection: ", {"class": "pt-2"}), ui.output_code("penguins_selection"), ui.output_data_frame("penguins_df"), ) def server(input, output, session): @render.code def penguins_selection(): return penguins_df.cell_selection()["rows"] @render.data_frame def penguins_df(): return render.DataTable(penguins, selection_mode="rows") @reactive.effect @reactive.event(input.update_selection) async def _(): await penguins_df.update_cell_selection({"type": "row", "rows": [1, 2, 8]}) @reactive.effect @reactive.event(input.reset_selection) async def _(): await penguins_df.update_cell_selection({"type": "row", "rows": []}) app = App(app_ui, server) ``` -------------------------------- ### Shiny Reactivity: Reactive Expressions and Observers (R vs. Python) Source: https://shiny.posit.co/py/docs/comp-r-shiny Demonstrates the equivalent functionality of reactive expressions and observers in Shiny for R and Python. It shows how to create a reactive value that updates based on a slider input and how an observer resets the slider. ```R library(shiny) ui <- fluidPage( sliderInput("n", "N", 0, 100, 40), verbatimTextOutput("txt"), actionButton("reset", "Reset") ) server <- function(input, output, session) { val <- reactive({input$n}) observe({ input$reset updateSliderInput(session, "n", value = 40) }) output$txt <- renderText({ paste0("n*2 is ", val()," * 2") }) } shinyApp(ui, server) ``` ```Python from shiny import App, reactive, render, ui app_ui = ui.page_fluid( ui.input_slider("n", "N", 0, 100, 40), ui.output_text_verbatim("txt"), ui.input_action_button("reset", "Reset"), ) def server(input, output, session): @reactive.calc def val(): return input.n() @reactive.effect def _(): input.reset() ui.update_slider("n", value=40) @render.text def txt(): return f"n*2 is {val() * 2}" app = App(app_ui, server) ``` -------------------------------- ### Creating and Running Shiny for Python Applications Source: https://shiny.posit.co/py/api/core/render Provides guidance on how to create and run Shiny for Python applications. This includes defining the application's UI and server logic, and using the `run_app` function. ```Python from shiny import App, ui, render app_ui = ui.page_fluid( ui.h1("My Shiny App"), ui.input_slider("n", "Number of points", min=1, max=100, value=50), ui.output_text_verbatim("txt") ) def server(input, output, session): @output @render.text def txt(): return f"You have selected {input.n()} points." app = App(app_ui, server) # To run the app, save this code as app.py and run 'shiny run app.py' in your terminal. ``` -------------------------------- ### Shiny Reactive Values: Standalone vs. reactiveValues (R vs. Python) Source: https://shiny.posit.co/py/docs/comp-r-shiny Illustrates how to manage reactive values in Shiny for R and Python. It shows the R approach using `reactiveVal` and `reactiveValues`, and the Python approach using `reactive.value`, including how to retrieve and set values. ```R vals <- reactiveValues(x = 1, y = 2) z <- reactiveVal(3) # Retrieve values print(vals$x) print(z()) ``` ```Python from shiny import reactive vals = { "x": reactive.value(1), "y": reactive.value(2), } z = reactive.value(3) # Retrieve values print(vals.x()) print(z()) ``` -------------------------------- ### Shiny for Python: Application Creation and Execution Source: https://shiny.posit.co/py/api/reactive Documents the fundamental functions and classes for creating and running Shiny for Python applications. This includes `run_app`, `App`, `Inputs`, `Outputs`, and `Session`. ```Python run_app App Inputs Outputs Session ``` -------------------------------- ### VS Code Settings for Shiny Type Checking Source: https://shiny.posit.co/py/docs/install-create-run Configures Python language server settings in VS Code for enhanced type checking with Shiny. It sets the type checking mode to 'basic' and disables reporting for unused functions, which is common in Shiny's reactive programming model. ```JSON { "python.analysis.typeCheckingMode": "basic", "python.analysis.diagnosticSeverityOverrides": { "reportUnusedFunction": "none" } } ``` -------------------------------- ### Shiny Core App Structure Source: https://shiny.posit.co/py/docs/express-to-core This snippet demonstrates the basic structure of a Shiny Core application. It includes the definition of the user interface (`app_ui`) and the server logic (`server`), culminating in the creation of the `App` instance. ```Python from shiny import App, ui app_ui = ui.page_fixed( # static UI here ) def server(input, output, session): # render/reactive logic here ... app = App(app_ui, server) ``` -------------------------------- ### Customize DataGrid Summary Statement in Shiny Express Source: https://shiny.posit.co/py/components/outputs/data-table This code snippet demonstrates how to customize the summary statement in a Shiny Express DataGrid. By setting the 'summary' parameter in render.DataTable to a custom string with tokens like {start}, {end}, and {total}, you can control the displayed message. This example uses the 'palmerpenguins' dataset. ```python from palmerpenguins import load_penguins from shiny.express import render, ui penguins = load_penguins() ui.h2("Palmer Penguins") @render.data_frame def penguins_df(): return render.DataTable( penguins, summary="Viendo filas {start} a {end} de {total}", ) ```