Sharing the value of a selected row in a table to another module

I am building a python shiny app that will use a row value selected from one table in a given module to filter a second table that will be rendered in another module. Basically, the goal is to filter the second table based on the column value of a row selected in the first table. Here is the first file:

####  file1.py
from shiny import req, ui, module, render
import shiny.experimental as x
import pandas as pd 

df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
                   'name': ['A', 'B', 'C', 'D'],
                   '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["name"].values[0]
            return value_selected

on another python file (file 2.py), the goal is to use extract the value selected above (value_selected) and filter a second table. In this case, I can click on the table and extract the name from the row selected by the user (e.g. B), and my goal is to use this name B and query another data-frame (e.g. df2 shown below) so that I can extract matching values for it.

Currently, 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
import pandas as pd 
from .file1 import table_view_ui, table_server

df2 = pd.DataFrame({'num_wings': [2, 4, 8, 0],
                   'name': ['B', 'E', 'F', 'D'],
                   'num_specimen_seen': [4, 0, 2, 1]},
                  index=['cat', 'bird', 'beast', 'pet'])

@module.ui
def main_app_ui():
   
    return ui.tags.div(
    table_view_ui("table_viewer"),
     {"style": "color:#000"},
    ui.output_data_frame("grided"),
    )

@module.server
def main_server(input, output, session):
    table_server("table_viewer")

    @output
    @render.data_frame
    def grided():
        {"style": "color:#000"},
        width="100%"
        height="100%"
        
        return render.DataGrid(
            df2,
            width=width,
            height=height)

In short, how can I get the value selected from df in file1.py (e.g. B), to filter the selected df2 in file2.py?

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.