Skip to content

[Feature]: Elaborate "native" support for Plotly charts in Shiny Express (@render.express) #2007

Open
@pfalcon

Description

@pfalcon

Category

Integrations

Scope

Minor Enhancement

Problem

As a new user, reading documentation on how to render Plotly charts, it suggests to use additional and separate shinywidgets package: https://shiny.posit.co/py/components/outputs/plot-plotly/ . However, it is a matter of fact that Plotly charts can be rendered without additional dependencies, and in the most natural way for the Shiny Express dialect: by simply "mentioning" the figure object:

# This is going to be text output
"Description of chart"

fig = px.line(...)
# This is going to be chart output
fig

However, how that works currently is flaky at best. But again, due to utter naturalness of syntax like above (for Shiny Express), I would kindly suggest that it's worth effort to make it work reliably and present to user among (the first) choices of rendering Plotly diagrams (would be naturally extended to other types charting packages, but I didn't try them yet and don't know the situation there).

This ticker is intended to serve as an "umbrella" issue, a statement of direction (make it all work out of the box), with specific issue to tackle linked against it.

Solution

I would like to suggest that the following code should work robustly:

import plotly.express as px
from shiny.express import input, ui


ui.input_slider("n", "Number of sth", 1, 100, 20)

fig = px.line(x=[1, 2, 3], y=[4, 1, 2])
fig

Likewise, I'd like to suggest that the following code also should work reliably:

import plotly.express as px
from shiny.express import input, ui, render


ui.input_slider("n", "Number of sth", 1, 100, 20)

@render.express
def render_func():
    _ = input.n()
    ui.h1("Heading1")
    fig = px.line(x=[1, 2, 3], y=[4, 1, 2])
    fig

Alternatives (Optional)

An alternative is of course to use shinywidgets, but how it all started for me is apparent inability to do following with it:

@render.express
def my_render():
    ui.h2("Chart 1")
    fig1 = px.line(...)
    # Wanna chart to be rendered here    

    ui.h2("Chart 2")
    fig2 = px.line(...)
    # Wanna chart to be rendered here    

Example (Optional)

Impact (Optional)

Would make Shiny Express mode much more natural. I'm a Streamlit refugee who would like to write dashboard apps in a similarly easy manner (but without straitjacket Streamlit enforces).

Contribution? (Optional)

Yes, I can implement (or help).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions