What is the correct way to load a dataframe in datatable? #2273
rohitsathish
announced in
Q&A
Replies: 1 comment
-
@rohitsathish Thanks so much for bringing this up - a recent release may have introduced a bug that we are not registering the Pandas data frame serializer. Try this code for now: import reflex as rx
a = rx.data_table
import asyncio
import pandas as pd
i = 0
async def long_running_task():
global i
i += 1
return pd.DataFrame({"col1": [i, 2], "col2": [3, 4]})
class State(rx.State):
progress: int = 0
df: pd.DataFrame = pd.DataFrame({"col1": [i, 2], "col2": [3, 4]})
@rx.background
async def run_long_task(self):
async with self:
self.df = await long_running_task()
@rx.page("/")
def index():
return rx.vstack(
rx.button(
"Start Long Task",
on_click=State.run_long_task,
),
rx.data_table(data=State.df),
)
app = rx.App()
app.compile() Your approach of setting the data frame as a state var is correct, but in a recent PR we made the component loading lazy. So that doing I'll file an issue for this and make sure this is fixed in an upcoming release. #2277 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm still experiencing the issue outlined in #279, so I'm unable to setup a dataframe with
rx.State
likedf: pd.DataFrame
. Would state even be the right place to keep a dataframe that needs to be loaded in the frontend?I would like to know the right approach if I have a structure like the one shown below.
Also some other allied questions:
data.py (sync with state)
data.py (just return df)
state.py
ui.py
Beta Was this translation helpful? Give feedback.
All reactions