-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add plots as inputs section to plotly component
- Loading branch information
Showing
3 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
components/outputs/plot-plotly/app-variation-plot-as-input-core.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import plotly.express as px | ||
import plotly.graph_objects as go | ||
from plotly.callbacks import Points | ||
import plotly.express as px | ||
from palmerpenguins import load_penguins | ||
from shiny import App, ui, render, reactive | ||
from shinywidgets import output_widget, render_widget | ||
|
||
penguins = load_penguins() | ||
|
||
app_ui = ui.page_fluid( | ||
output_widget("plot"), | ||
"Click info", | ||
ui.output_text_verbatim("click_info", placeholder=True), | ||
"Hover info", | ||
ui.output_text_verbatim("hover_info", placeholder=True), | ||
"Selection info (use box or lasso select)", | ||
ui.output_text_verbatim("selection_info", placeholder=True) | ||
) | ||
|
||
|
||
def server(input, output, session): | ||
|
||
click_reactive = reactive.value() # << | ||
hover_reactive = reactive.value() # << | ||
selection_reactive = reactive.value() # << | ||
|
||
@render_widget # << | ||
def plot(): # << | ||
fig = px.scatter( | ||
data_frame=penguins, x="body_mass_g", y="bill_length_mm" | ||
).update_layout( | ||
yaxis_title="Bill Length (mm)", | ||
xaxis_title="Body Mass (g)", | ||
) | ||
w = go.FigureWidget(fig.data, fig.layout) # << | ||
w.data[0].on_click(on_point_click) # << | ||
w.data[0].on_hover(on_point_hover) # << | ||
w.data[0].on_selection(on_point_selection) # << | ||
return w # << | ||
|
||
|
||
def on_point_click(trace, points, state): # << | ||
click_reactive.set(points) # << | ||
|
||
def on_point_hover(trace, points, state): # << | ||
hover_reactive.set(points) # << | ||
|
||
def on_point_selection(trace, points, state): # << | ||
selection_reactive.set(points) # << | ||
|
||
@render.text | ||
def click_info(): | ||
return click_reactive.get() | ||
|
||
@render.text | ||
def hover_info(): | ||
return hover_reactive.get() | ||
|
||
@render.text | ||
def selection_info(): | ||
return selection_reactive.get() | ||
|
||
|
||
app = App(app_ui, server) |
57 changes: 57 additions & 0 deletions
57
components/outputs/plot-plotly/app-variation-plot-as-input-express.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import plotly.express as px | ||
import plotly.graph_objects as go | ||
from plotly.callbacks import Points | ||
from shinywidgets import render_plotly | ||
from palmerpenguins import load_penguins | ||
from shiny import render, reactive | ||
from shiny.express import input, ui | ||
from shiny.ui import output_code, output_plot | ||
|
||
penguins = load_penguins() | ||
|
||
|
||
@render_plotly # << | ||
def plot(): # << | ||
fig = px.scatter( | ||
data_frame=penguins, x="body_mass_g", y="bill_length_mm" | ||
).update_layout( | ||
yaxis_title="Bill Length (mm)", | ||
xaxis_title="Body Mass (g)", | ||
) | ||
# Need to create a FigureWidget() for on_click to work | ||
w = go.FigureWidget(fig.data, fig.layout) # << | ||
w.data[0].on_click(on_point_click) # << | ||
w.data[0].on_hover(on_point_hover) # << | ||
w.data[0].on_selection(on_point_selection) # << | ||
return w # << | ||
|
||
|
||
# Capture the clicked point in a reactive value | ||
click_reactive = reactive.value() # << | ||
hover_reactive = reactive.value() # << | ||
selection_reactive = reactive.value() # << | ||
|
||
def on_point_click(trace, points, state): # << | ||
click_reactive.set(points) # << | ||
|
||
def on_point_hover(trace, points, state): # << | ||
hover_reactive.set(points) # << | ||
|
||
def on_point_selection(trace, points, state): # << | ||
selection_reactive.set(points) # << | ||
|
||
|
||
"Click info" | ||
@render.code | ||
def click_info(): | ||
return str(click_reactive.get()) | ||
|
||
"Hover info" | ||
@render.code | ||
def hover_info(): | ||
return str(hover_reactive.get()) | ||
|
||
"Selection info (use box or lasso select)" | ||
@render.code | ||
def selection_info(): | ||
return str(selection_reactive.get()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters