Python-bloggers

Shiny Express: Blending the Best of Shiny and Streamlit for Dashboard Development

This article was first published on Tag: python - Appsilon | Enterprise R Shiny Dashboards , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

Shiny has long stood as a pillar of functionality and flexibility for R-based web applications. Let’s explore Shiny Express — an emerging Python-centric framework in development that combines the ease of Streamlit with the robustness of Shiny.

This article explores the nuances of Shiny Express and what differentiates it from Classic Shiny and Streamlit.

Table of Contents

Ready to Enhance Your Shiny App Prototyping Skills? Explore {ShinyUiEditor}.

Brief Overview of Shiny Express

Shiny Express is a new, simplified way to write Shiny app prototypes, offering an easier and quicker development process compared to traditional Shiny. It’s akin to Streamlit in user-friendliness but with greater potential.

Shiny Express includes integrated UI and server code, simplified layout components, and automatic setting of top-level page containers, making it more accessible, especially for beginners. Just like Streamlit, it won’t suit all use cases, but on the contrary will be much easier to refactor into Classic Shiny, with clear UI and server separation, for complex applications.

Key Features of Shiny Express

Here are some key features of Shiny Express:

Installation

Install Shiny and htmltools:

Shiny Express requires the installation of development shiny and htmltools versions from GitHub. In your terminal, execute the following commands:

pip install git+https://github.com/posit-dev/py-htmltools
pip install git+https://github.com/posit-dev/py-shiny

Example

In this Shiny Express example, let’s create an interactive app featuring a dynamic histogram plot with an adjustable slider:

from shiny import ui, render, reactive
from shiny.express import input
import numpy as np
import matplotlib.pyplot as plt

# Setting the title of the panel
ui.panel_title("PyShiny App")

# Creating a slider input for number of samples
ui.input_slider("n_samples", "N Samples", 10, 100, 50)

# Creating a dropdown select input for choosing a color
ui.input_select('selected_color', "Color", ['red', 'green', 'blue'])

# Reactive function to generate random numbers
# It updates when the slider value changes
@reactive.Calc
def x():
    return np.random.randn(input.n_samples())
    
# Function to render the histogram plot
# It updates when either the slider value or the selected color changes
@render.plot
def plot():
    fig, ax = plt.subplots()
    # Histogram of the random numbers with the selected color
    ax.hist(x(), color=input.selected_color())
    return fig

PyShiny App

This example showcases a Shiny Express app that integrates a slider for adjusting sample size and a dropdown for colour selection, dynamically updating a histogram plot in response to user inputs. This illustrates the power and flexibility of Shiny Express in creating interactive and responsive web application prototypes with minimal and intuitive coding.

Comparing Shiny Express with Streamlit

Streamlit App

This Streamlit app mirrors the above Shiny Express example.

import streamlit as st
import numpy as np
import matplotlib.pyplot as plt

# Displaying the title of the Streamlit app
st.title("Streamlit App")

# Creating a slider for number of samples
n_samples = st.slider("N samples", 10, 100, 50)

# Creating a dropdown select box for choosing a color
selected_color = st.selectbox("Color", ["red", "green", "blue"])

# Generating random numbers based on the selected number of samples
x = np.random.randn(n_samples)

# Creating a matplotlib figure and axis
fig, ax = plt.subplots()
# Plotting a histogram of the random numbers with the selected color
ax.hist(x, color=selected_color)
# Displaying the plot in the Streamlit app
st.pyplot(fig)

PyShiny vs Streamlit

In summary, Shiny Express offers a more flexible and capable alternative to Streamlit, especially for users looking to create more complex web applications while still benefiting from a simplified development process.

Discover whether Streamlit or Shiny best suits your data science needs in our comprehensive comparison, Choosing the Right Data Dashboard Tool: The Unique Strengths of Streamlit and Shiny.

Potential Challenges

Conclusion

Shiny Express offers a more accessible entry point for newcomers to Shiny, simplifying the learning curve with its streamlined syntax and integrated UI and server code. Experienced users can benefit from its quick prototyping capabilities while retaining the option to smoothly switch to traditional Shiny for more complex tasks.

The choice between Shiny Express, Classic Shiny, and Streamlit depends on the application’s complexity and specific requirements. Streamlit suits rapid, simple app development, Shiny Express is ideal for intermediate complexity with greater flexibility, and Classic Shiny is best for intricate, highly customised applications. Users can evaluate their needs and expertise level to select the most appropriate framework.

Enjoying our content? Then you’ll love our upcoming event – Shiny Gatherings #9: R Shiny Trivia Night! Whether you’re a solo player or teaming up with friends and colleagues, this is your chance to showcase your R Shiny knowledge. Get ready for an evening of competition, prizes, and fun. Secure your spot today and be part of the excitement.

 

The post appeared first on appsilon.com/blog/.

To leave a comment for the author, please follow the link and comment on their blog: Tag: python - Appsilon | Enterprise R Shiny Dashboards .

Want to share your content on python-bloggers? click here.
Exit mobile version