I am building a python shiny app that will use a row value selected from a table in a given module to filter a second table that will be rendered in another module.
# file1.py
python
from shiny import req, ui, module, render, reactive
import shiny.experimental as x
import pandas as pd
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
'num_wings': [2, 0, 0, 0],
'num_specimen_seen': [10, 2, 1, 8]},
index=['falcon', 'dog', 'spider', 'fish'])
@module.ui
def table_view_ui(label: str = "Table"):
return x.ui.card(
{"style": "color:#000"},
x.ui.card_header("This is " + label),
ui.output_data_frame("grided"),
ui.output_text("select_row")
)
@module.server
def table_server(input, output, session):
# Render Table
@output
@render.data_frame
def grided():
{"style": "color:#000"},
width="100%"
height="100%"
return render.DataGrid(
df.reset_index(),
row_selection_mode="single",
width=width,
filters=True,
height=height)
@output
@render.text
def select_row():
if (input.grided_selected_rows() is not None
and len(input.grided_selected_rows()) > 0
):
selected_idx = list(req(input.grided_selected_rows()))
selected_row= df.iloc[selected_idx]
value_selected = selected_row["num_legs"].values[0]
return value_selected
on another python file (file 2), the goal is to use extract the value selected (value_selected) and filter another table. At this stage, I can render the table on file2.py but I've not been able to get the value returned.
# file2.py
from shiny import req, ui, module, render, reactive
from .file1 import table_view_ui, table_server
df2 = pd.DataFrame({'num_legs': [2, 4, 8, 0],
'num_eyes': [2, 0, 0, 0],
'num_specimen_seen': [4, 0, 2, 1]},
index=['falcon', 'dog', 'spider', 'fish'])
@module.ui
def second_table_ui():
return ui.tags.div(
table_view("table_viewer")
@module.server
def second_server(input, output, session):
table_server("table_viewer")
If I can capture the row value selected in file1.py from df, I will be able to filter another dataframe (df2) in file2.py.
# file3.py
from shiny import req, ui, module, render, reactive, App
from .file2 import second_table_ui, second_server
app_ui = ui.page_fluid(
second_table_ui("second_table_viewer")
def server(input, output, session):
second_server("second_table_viewer")
app = App(app_ui, server)