DataTable
#| standalone: true
#| components: [viewer]
#| viewerHeight: 300
from palmerpenguins import load_penguins
from shiny import App, render, ui
penguins = load_penguins()
app_ui = ui.page_fluid(
ui.h2("Palmer Penguins"),
ui.output_data_frame("penguins_df"),
)
def server(input, output, session):
@render.data_frame
def penguins_df():
return render.DataTable(penguins)
app = App(app_ui, server)
from palmerpenguins import load_penguins
from shiny import App, render, ui
penguins = load_penguins()
app_ui = ui.page_fluid(
ui.h2("Palmer Penguins"),
ui.output_data_frame("penguins_df"),
)
def server(input, output, session):
@render.data_frame
def penguins_df():
return render.DataTable(penguins)
app = App(app_ui, server)
Relevant Functions
-
ui.output_data_frame
ui.output_data_frame(id)
-
@render.data_frame
render.data_frame(fn=None)
-
render.DataTable
render.DataTable(self, data, *, width='fit-content', height='500px', summary=True, filters=False, row_selection_mode='none')
Details
A DataTable presents tabular data in a figure-like view with a minimum of grid lines.
To make a reactive Data Table, follow three steps:
Call
ui.output_data_frame()
in the UI of your app to create a div in which to display the table. Where you call this function within the UI functions will determine where the table will appear within the layout of the app. Set the id argument ofui.output_data_frame()
to a unique value.Within the server function, define a new function whose name matches the id used above. The function should assemble the table to display and then return the table wrapped in
render.DataTable()
. Shiny will rerun this function whenever it needs to build or update the output that has the matching id.Decorate the function with
@render.data_frame
.
A Data Table can also collect input from the user. To allow this, set render.DataTable(selection_mode="row")
or render.DataTable(selection_mode="rows")
to allow the user to select one or more rows of the Data Table.
The indices of the selected rows will be accessible within the server function as a reactive variable returned by <name>.input_cell_selection()["rows"]
, where @render.data_frame
.
The value returned will be an empty tuple if no rows are selected, or a tuple of integers representing the indices of the selected rows. To filter a pandas data frame down to the selected rows, use df.iloc[list(<name>.input_cell_selection()["rows"])]
.
For more information about interacting with data frames, see the API documentation for Express or Core syntax.
If your table is a data frame that uses the pandas styler, replace ui.output_data_frame()
with ui.output_table()
and @render.data_frame
with @render.table
.
See also Data Grids
Variations
Select Rows
Set selection_mode
in render.DataTable()
to "row"
to allow the user to select one row at a time. Set it to "rows"
to allow the user to select multiple rows at a time. Access the selection(s) as <id>.input_cell_selection()["rows"]
.
#| standalone: true
#| components: [viewer]
#| viewerHeight: 350
from palmerpenguins import load_penguins
from shiny import App, render, ui
penguins = load_penguins()
app_ui = ui.page_fluid(
ui.h2("Palmer Penguins"),
ui.output_ui("rows"),
ui.output_data_frame("penguins_df"),
)
def server(input, output, session):
@render.data_frame
def penguins_df():
return render.DataTable(penguins, selection_mode="rows")
@render.ui
def rows():
rows = penguins_df.input_cell_selection()["rows"]
selected = ", ".join(str(i) for i in sorted(rows)) if rows else "None"
return f"Rows selected: {selected}"
app = App(app_ui, server)
from palmerpenguins import load_penguins
from shiny import render
from shiny.express import input, ui
penguins = load_penguins()
ui.h2("Palmer Penguins")
@render.ui
def rows():
rows = penguins_df.input_cell_selection()["rows"]
selected = ", ".join(str(i) for i in sorted(rows)) if rows else "None"
return f"Rows selected: {selected}"
@render.data_frame
def penguins_df():
return render.DataTable(penguins, selection_mode="rows")
from palmerpenguins import load_penguins
from shiny import App, render, ui
penguins = load_penguins()
app_ui = ui.page_fluid(
ui.h2("Palmer Penguins"),
ui.output_ui("rows"),
ui.output_data_frame("penguins_df"),
)
def server(input, output, session):
@render.data_frame
def penguins_df():
return render.DataTable(penguins, selection_mode="rows")
@render.ui
def rows():
rows = penguins_df.input_cell_selection()["rows"]
selected = ", ".join(str(i) for i in sorted(rows)) if rows else "None"
return f"Rows selected: {selected}"
app = App(app_ui, server)
Filterable Table
Set render.DataTable(filters=True)
to add a row of filter options to the header row. Users can interact with these options to filter the table.
#| standalone: true
#| components: [viewer]
#| viewerHeight: 350
from palmerpenguins import load_penguins
from shiny import App, render, ui
penguins = load_penguins()
app_ui = ui.page_fluid(
ui.h2("Palmer Penguins"),
ui.output_data_frame("penguins_df"),
)
def server(input, output, session):
@render.data_frame
def penguins_df():
return render.DataTable(penguins, filters=True)
app = App(app_ui, server)
from palmerpenguins import load_penguins
from shiny import App, render, ui
penguins = load_penguins()
app_ui = ui.page_fluid(
ui.h2("Palmer Penguins"),
ui.output_data_frame("penguins_df"),
)
def server(input, output, session):
@render.data_frame
def penguins_df():
return render.DataTable(penguins, filters=True)
app = App(app_ui, server)
Set Table Size
Set the height
and width
parameters of render.DataTable()
to constrain the output size of the table.
#| standalone: true
#| components: [viewer]
#| viewerHeight: 350
from palmerpenguins import load_penguins
from shiny import App, render, ui
penguins = load_penguins()
app_ui = ui.page_fluid(
ui.h2("Palmer Penguins"),
ui.output_data_frame("penguins_df"),
)
def server(input, output, session):
@render.data_frame
def penguins_df():
return render.DataTable(penguins, width="300px", height="250px")
app = App(app_ui, server)
from palmerpenguins import load_penguins
from shiny import App, render, ui
penguins = load_penguins()
app_ui = ui.page_fluid(
ui.h2("Palmer Penguins"),
ui.output_data_frame("penguins_df"),
)
def server(input, output, session):
@render.data_frame
def penguins_df():
return render.DataTable(penguins, width="300px", height="250px")
app = App(app_ui, server)
Customize Summary Statement
Set summary
in render.DataGrid()
to False
to remove the statement “Viewing rows 1 through 10 of 20”. Set it to a string template containing {start}
, {end}
, and {total}
tokens, to customize the message.
#| standalone: true
#| components: [viewer]
#| viewerHeight: 350
from palmerpenguins import load_penguins
from shiny import App, render, ui
penguins = load_penguins()
app_ui = ui.page_fluid(
ui.h2("Palmer Penguins"),
ui.output_data_frame("penguins_df"),
)
def server(input, output, session):
@render.data_frame
def penguins_df():
return render.DataTable(
penguins,
summary="Viendo filas {start} a {end} de {total}",
)
app = App(app_ui, server)
from palmerpenguins import load_penguins
from shiny import App, render, ui
penguins = load_penguins()
app_ui = ui.page_fluid(
ui.h2("Palmer Penguins"),
ui.output_data_frame("penguins_df"),
)
def server(input, output, session):
@render.data_frame
def penguins_df():
return render.DataTable(
penguins,
summary="Viendo filas {start} a {end} de {total}",
)
app = App(app_ui, server)