Using a Pandas dataframe with Foreach #1931
-
I'm trying to use a dataframe in a foreach loop inside a responsive grid. I'm following the docs example as close as possible and calling the row data passed to create_card like a row from the pandas function iterrows. Running the app throws a type error relating to the type for row. Does anyone know how to correct what I'm doing or point me in the right direction?
Traceback:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Reflex does not currently support import pandas as pd
import reflex as rx
class UserState(rx.State):
@rx.cached_var
def get_users(self) -> pd.DataFrame:
# Usually call database for this
df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4],
'col3': [5, 6]})
return df
@rx.cached_var
def user_rows(self) -> list[dict]:
return [row[1].to_dict() for row in self.get_users.iterrows()]
def create_card(row, index):
return rx.card(
rx.text(row['col2']),
header=rx.heading(row['col1'], size="lg"),
footer=rx.heading(row['col3'], size="sm"),
)
def user_grid() -> rx.Component:
return rx.fragment(rx.vstack(
rx.heading("Sample"),
rx.responsive_grid(
rx.foreach(UserState.user_rows, lambda row, index: create_card(row, index)),
columns=[1, 2]
)
)
)
app = rx.App()
app.add_page(user_grid(), route="/")
app.compile() |
Beta Was this translation helpful? Give feedback.
card_link
is "ComputedVar" because it is decorated withrx.var
orrx.cached_var
, these functions act likeproperty
, they cannot take parameters and are computed based on the current state.My suggestion was to use a normal event handler, so remove these decorators and try again.