posit-dev / posit-dev/py-shiny

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

Open
#2,007 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement needs-triage
Dominant language
Python
Stars
1.8k
Forks
135
Avg merge
2d 18h
Merged PRs (30d)
21

Description

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).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two Plotly examples in the issue: a figure mentioned directly in Shiny Express and a figure inside @render.express. Read the existing Shiny Express rendering behavior around these entry points and compare it with the documented shinywidgets approach. Done means both examples render reliably without an additional shinywidgets dependency.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
frontend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.