posit-dev / py-shiny

Shiny for Python
https://shiny.posit.co/py/
MIT License
1.27k stars 76 forks source link

DataGrid not showing using ui.output_data_frame #764

Open remylegoff opened 1 year ago

remylegoff commented 1 year ago

I'm using DataGrid and render.data_frame to be able to do row selection but the data frame is not displaying. When debugging, the returned object is a DataGrid shiny object. If I'm using basic pd.DataFrame everything is right.

Here, the server part :

@output
@render.data_frame
def feature_selection():
  meth = input.meth_graph()
  results = tranformed_data().dict_results[meth][2]
  to_return =  pd.DataFrame([raw_data().features,results]).T
  return render.DataGrid(
    to_return,
    row_selection_mode = 'multiple')

Here the ui part :

ui.nav(
  ui.row(
    ui.output_data_frame('feature_selection')
  )
)
gshotwell commented 1 year ago

I'm not able to replicate this, can you post a more complete example?

This code works:

from shiny import module, ui, render, reactive, event, App, Inputs, Outputs, Session
import pandas as pd
import numpy as np

df = pd.DataFrame(
    {
        "A": np.random.rand(5),
        "B": np.random.rand(5),
        "C": np.random.rand(5),
        "D": np.random.rand(5),
        "E": np.random.rand(5),
        "F": np.random.rand(5),
    }
)

app_ui = ui.page_fluid(
    ui.output_data_frame("my_df"),
)

def server(input: Inputs, output: Outputs, session: Session):
    @output
    @render.data_frame
    def my_df():
        return render.DataGrid(df, row_selection_mode="multiple")

app = App(app_ui, server)
remylegoff commented 1 year ago

here is a bit more of context from the ui part:

ui.navset_tab(
                                ui.nav('Données complètes', ui.output_data_frame('data_display')),
                                ui.nav(
                                    'Statistiques Descriptives',
                                    ui.page_fluid(
                                        ui.row(
                                            'Statistiques descriptives en retirant les 0',
                                            ui.output_data_frame('stats_descriptives')
                                            ),
                                        ui.row(
                                            'Visualisation graphiques',
                                            ui.input_text('col_boxplot','Colonnes à visualiser', placeholder = 'Séparées par ;'),
                                            ui.output_plot('boxplot')
                                            ),
                                        ui.row('Fréquence des différentes classes',
                                               ui.output_plot('frequence_classe'))
                                        )
                                    ),
                                ui.nav('Selection de variable',
                                       ui.output_data_frame('feature_selection'))
)

So it's in a big app with lot of tabs, sub tabs and it's the only thing not working. The server part that is related to the display more than the render function is a bit too complex to put it here. But I don't think it comes from the server because the return object is a DataGrid

remylegoff commented 1 year ago

I'm using shiny 0.5.1, shinywidgets 0.2.1, pandas 2.0.3, python 3.11.4 on windows with VScode

rh-atx commented 1 year ago

Hey @gshotwell I tried running your example, and unfortunately nothing rendered to the screen. I am also using shiny 0.5.1 and am experiencing this issue with render.DataGrid. I've noticed this only occurs when I'm using Safari, though. Thanks!

gshotwell commented 1 year ago

@rh-atx I'm not able to reproduce that on Safari, is anything else different? Which version of Python are you on?

remylegoff commented 11 months ago

Another forum post on the discord answer the question. The column names must be string and not index to work. I don't know if it will help improve the functions but when column names are transform to string, it works well.