-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reactive data for ipydatagrid #511
Comments
Hi Florian, lets start by saying that I am impressed with what you managed to figure out, and your creative solutions.
Yes, this is the preferred solution. I always planned to provide an example for ipydatagrid because of this issue, it's a nice example of how to deal with a library that has a constructor with arguments named differently from the traits, and where the updates are done via a differently named property (not even a trait). If ipydatagrid would be more symmetric (e.g. its .data property would be named .dataframe - as mentioned in the docs) everything would be fine). The solution in your last example is creative, but I can imagine that in the future this will trigger warnings (data is not an argument of the DataGrid ctor). Maybe we can ask the ipydatagrid maintainers to add a In the meantime, this is an elegant solution, what do you think?: from typing import Callable, Dict, List, cast
import solara
import solara as sl
import pandas as pd
from ipydatagrid import DataGrid as DataGridWidget
meta = pd.DataFrame(columns=['name', 'region', "source"],
data=[["forecast", "de", "ws"], ["actual", "de", "ws"], ["intraday", "de", "ws"]])
selected_meta = sl.reactive(pd.DataFrame(columns=meta.columns))
selection_text = sl.reactive("")
@solara.component
def DataGrid(dataframe, selection_mode: str, on_selections: Callable[[List[Dict]], None] = None, **kwargs):
el = DataGridWidget.element(dataframe=dataframe, selection_mode=selection_mode, on_selections=on_selections, **kwargs)
def update():
widget = cast(DataGridWidget, solara.get_widget(el))
widget.data = dataframe
solara.use_effect(update, dependencies=[dataframe])
@sl.component
def Page():
def handle_select(sel):
print(sel)
selected_meta.value = pd.concat([selected_meta.value, meta[meta.index == sel[0]["r1"]]])
DataGrid(dataframe=meta, selection_mode="row", on_selections=handle_select)
DataGrid(dataframe=selected_meta.value, selection_mode="row") Also, do you think we should add this to https://solara.dev/docs/howto/ipywidget-libraries or maybe add an example at https://solara.dev/examples or both? Regards, Maarten |
Looks great thanks! |
Hi, we were trying to update the table contents of ipydatagrid via a reactive variable. Our initial approach did not work:
our initial fix was to use effect:
which does work, but out of curiosity we also tried the following
which works as well.
Since pydatagrid is mentioned in the docs we were a bit surprised that such a workaround was needed in the first place.
Is any of the two solutions preferable?
What would be the best way to support this in pydatagrid?
Best,
Florian
The text was updated successfully, but these errors were encountered: