Skip to content

Commit 936332c

Browse files
authored
Tweak example app so Plotly plots fill their cards (#574)
1 parent a6e5881 commit 936332c

File tree

2 files changed

+124
-86
lines changed

2 files changed

+124
-86
lines changed

shiny/examples/data_frame/app.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from shiny import App
66
from shiny import experimental as x
7-
from shiny import reactive, render, req, ui
7+
from shiny import reactive, render, req, session, ui
88

99
# Load the Gapminder dataset
1010
df = px.data.gapminder()
@@ -78,7 +78,14 @@ def country_detail_pop():
7878
color="country",
7979
title="Population Over Time",
8080
)
81-
return go.FigureWidget(fig)
81+
widget = go.FigureWidget(fig)
82+
83+
@synchronize_size("country_detail_pop")
84+
def on_size_changed(width, height):
85+
widget.layout.width = width
86+
widget.layout.height = height
87+
88+
return widget
8289

8390
@output
8491
@render_widget
@@ -91,7 +98,37 @@ def country_detail_percap():
9198
color="country",
9299
title="GDP per Capita Over Time",
93100
)
94-
return go.FigureWidget(fig)
101+
widget = go.FigureWidget(fig)
102+
103+
@synchronize_size("country_detail_percap")
104+
def on_size_changed(width, height):
105+
widget.layout.width = width
106+
widget.layout.height = height
107+
108+
return widget
109+
110+
111+
# This is a hacky workaround to help Plotly plots automatically
112+
# resize to fit their container. In the future we'll have a
113+
# built-in solution for this.
114+
def synchronize_size(output_id):
115+
def wrapper(func):
116+
input = session.get_current_session().input
117+
118+
@reactive.Effect
119+
def size_updater():
120+
func(
121+
input[f".clientdata_output_{output_id}_width"](),
122+
input[f".clientdata_output_{output_id}_height"](),
123+
)
124+
125+
# When the output that we're synchronizing to is invalidated,
126+
# clean up the size_updater Effect.
127+
reactive.get_current_context().on_invalidate(size_updater.destroy)
128+
129+
return size_updater
130+
131+
return wrapper
95132

96133

97134
app = App(app_ui, server)

shiny/render/_dataframe.py

Lines changed: 84 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,48 @@ def to_payload(self) -> object:
3030

3131

3232
class DataGrid(AbstractTabularData):
33+
"""
34+
Holds the data and options for a ``shiny.render.data_frame`` output, for a
35+
spreadsheet-like view.
36+
37+
Parameters
38+
----------
39+
data
40+
A pandas `DataFrame` object, or any object that has a `.to_pandas()` method
41+
(e.g., a Polars data frame or Arrow table).
42+
width
43+
A _maximum_ amount of vertical space for the data grid to occupy, in CSS units
44+
(e.g. `"400px"`) or as a number, which will be interpreted as pixels. The
45+
default is `fit-content`, which sets the grid's width according to its contents.
46+
Set this to `100%` to use the maximum available horizontal space.
47+
height
48+
A _maximum_ amount of vertical space for the data grid to occupy, in CSS units
49+
(e.g. `"400px"`) or as a number, which will be interpreted as pixels. If there
50+
are more rows than can fit in this space, the grid will scroll. Set the height
51+
to `None` to allow the grid to grow to fit all of the rows (this is not
52+
recommended for large data sets, as it may crash the browser).
53+
summary
54+
If `True` (the default), shows a message like "Viewing rows 1 through 10 of 20"
55+
below the grid when not all of the rows are being shown. If `False`, the message
56+
is not displayed. You can also specify a string template to customize the
57+
message, containing `{start}`, `{end}`, and `{total}` tokens. For example:
58+
`"Viendo filas {start} a {end} de {total}"`.
59+
row_selection_mode
60+
Use `"none"` to disable row selection, `"single"` to allow a single row to be
61+
selected at a time, and `"multiple"` to allow multiple rows to be selected by
62+
clicking on them individually.
63+
64+
Returns
65+
-------
66+
:
67+
An object suitable for being returned from a `@render.data_frame`-decorated
68+
output function.
69+
70+
See Also
71+
--------
72+
~shiny.ui.output_data_frame ~shiny.render.data_frame
73+
"""
74+
3375
def __init__(
3476
self,
3577
data: object,
@@ -39,47 +81,6 @@ def __init__(
3981
summary: Union[bool, str] = True,
4082
row_selection_mode: Literal["none", "single", "multiple"] = "none",
4183
):
42-
"""
43-
Holds the data and options for a ``shiny.render.data_frame`` output, for a
44-
spreadsheet-like view.
45-
46-
Parameters
47-
----------
48-
data
49-
A pandas `DataFrame` object, or any object that has a `.to_pandas()` method
50-
(e.g., a Polars data frame or Arrow table).
51-
width
52-
A _maximum_ amount of vertical space for the data grid to occupy, in CSS
53-
units (e.g. `"400px"`) or as a number, which will be interpreted as pixels.
54-
The default is `fit-content`, which sets the grid's width according to its
55-
contents. Set this to `100%` to use the maximum available horizontal space.
56-
height
57-
A _maximum_ amount of vertical space for the data grid to occupy, in CSS
58-
units (e.g. `"400px"`) or as a number, which will be interpreted as pixels.
59-
If there are more rows than can fit in this space, the grid will scroll. Set
60-
the height to `None` to allow the grid to grow to fit all of the rows (this
61-
is not recommended for large data sets, as it may crash the browser).
62-
summary
63-
If `True` (the default), shows a message like "Viewing rows 1 through 10 of
64-
20" below the grid when not all of the rows are being shown. If `False`, the
65-
message is not displayed. You can also specify a string template to
66-
customize the message, containing `{start}`, `{end}`, and `{total}` tokens.
67-
For example: `"Viendo filas {start} a {end} de {total}"`.
68-
row_selection_mode
69-
Use `"none"` to disable row selection, `"single"` to allow a single row to
70-
be selected at a time, and `"multiple"` to allow multiple rows to be
71-
selected by clicking on them individually.
72-
73-
Returns
74-
-------
75-
:
76-
An object suitable for being returned from a `@render.data_frame`-decorated
77-
output function.
78-
79-
See Also
80-
--------
81-
~shiny.ui.output_data_frame ~shiny.render.data_frame
82-
"""
8384
import pandas as pd
8485

8586
self.data: pd.DataFrame = cast(
@@ -113,6 +114,48 @@ def to_payload(self) -> object:
113114

114115

115116
class DataTable(AbstractTabularData):
117+
"""
118+
Holds the data and options for a ``shiny.render.data_frame`` output, for a
119+
spreadsheet-like view.
120+
121+
Parameters
122+
----------
123+
data
124+
A pandas `DataFrame` object, or any object that has a `.to_pandas()` method
125+
(e.g., a Polars data frame or Arrow table).
126+
width
127+
A _maximum_ amount of vertical space for the data table to occupy, in CSS units
128+
(e.g. `"400px"`) or as a number, which will be interpreted as pixels. The
129+
default is `fit-content`, which sets the table's width according to its
130+
contents. Set this to `100%` to use the maximum available horizontal space.
131+
height
132+
A _maximum_ amount of vertical space for the data table to occupy, in CSS units
133+
(e.g. `"400px"`) or as a number, which will be interpreted as pixels. If there
134+
are more rows than can fit in this space, the table body will scroll. Set the
135+
height to `None` to allow the table to grow to fit all of the rows (this is not
136+
recommended for large data sets, as it may crash the browser).
137+
summary
138+
If `True` (the default), shows a message like "Viewing rows 1 through 10 of 20"
139+
below the grid when not all of the rows are being shown. If `False`, the message
140+
is not displayed. You can also specify a string template to customize the
141+
message, containing `{start}`, `{end}`, and `{total}` tokens. For example:
142+
`"Viendo filas {start} a {end} de {total}"`.
143+
row_selection_mode
144+
Use `"none"` to disable row selection, `"single"` to allow a single row to be
145+
selected at a time, and `"multiple"` to allow multiple rows to be selected by
146+
clicking on them individually.
147+
148+
Returns
149+
-------
150+
:
151+
An object suitable for being returned from a `@render.data_frame`-decorated
152+
output function.
153+
154+
See Also
155+
--------
156+
~shiny.ui.output_data_frame ~shiny.render.data_frame
157+
"""
158+
116159
def __init__(
117160
self,
118161
data: object,
@@ -124,48 +167,6 @@ def __init__(
124167
Literal["none"], Literal["single"], Literal["multiple"]
125168
] = "none",
126169
):
127-
"""
128-
Holds the data and options for a ``shiny.render.data_frame`` output, for a
129-
spreadsheet-like view.
130-
131-
Parameters
132-
----------
133-
data
134-
A pandas `DataFrame` object, or any object that has a `.to_pandas()` method
135-
(e.g., a Polars data frame or Arrow table).
136-
width
137-
A _maximum_ amount of vertical space for the data table to occupy, in CSS
138-
units (e.g. `"400px"`) or as a number, which will be interpreted as pixels.
139-
The default is `fit-content`, which sets the table's width according to its
140-
contents. Set this to `100%` to use the maximum available horizontal space.
141-
height
142-
A _maximum_ amount of vertical space for the data table to occupy, in CSS
143-
units (e.g. `"400px"`) or as a number, which will be interpreted as pixels.
144-
If there are more rows than can fit in this space, the table body will
145-
scroll. Set the height to `None` to allow the table to grow to fit all of
146-
the rows (this is not recommended for large data sets, as it may crash the
147-
browser).
148-
summary
149-
If `True` (the default), shows a message like "Viewing rows 1 through 10 of
150-
20" below the grid when not all of the rows are being shown. If `False`, the
151-
message is not displayed. You can also specify a string template to
152-
customize the message, containing `{start}`, `{end}`, and `{total}` tokens.
153-
For example: `"Viendo filas {start} a {end} de {total}"`.
154-
row_selection_mode
155-
Use `"none"` to disable row selection, `"single"` to allow a single row to
156-
be selected at a time, and `"multiple"` to allow multiple rows to be
157-
selected by clicking on them individually.
158-
159-
Returns
160-
-------
161-
:
162-
An object suitable for being returned from a `@render.data_frame`-decorated
163-
output function.
164-
165-
See Also
166-
--------
167-
~shiny.ui.output_data_frame ~shiny.render.data_frame
168-
"""
169170
import pandas as pd
170171

171172
self.data: pd.DataFrame = cast(

0 commit comments

Comments
 (0)