import solara
from great_tables import GT
from great_tables.data import sp500
# we still wrap it in a reactive, so each user can work on its own copy
df = solara.reactive(sp500)
# this is a proxy to detect changes to the dataframe, since we will mutate df.value
df_version = solara.reactive(0)
# however, since the initial value is shared, we do not want to mutate
# it for each user
# NOTE: this may change in the future https://github.com/widgetti/solara/pull/595
def copy_once():
df.value = df.value.copy()
solara.lab.on_kernel_start(copy_once)
# Define the start and end dates for the data range
start_date = "2010-06-07"
end_date = "2010-06-14"
@solara.component
def Page():
df_version.value # just read it to make this component depend on the proxy
print("Render: ", df_version.value)
def multiply():
# we mutate the dataframe, Python/solara cannot detect that
df.value['volume'] = df.value['volume'] * 2
# but we use this proxy to signal we changed it
df_version.value += 1
# Filter sp500 using Pandas to dates between `start_date` and `end_date`
dff = df.value
dff = dff[(dff["date"] >= start_date) & (dff["date"] <= end_date)]
# Create a display table based on the `sp500_mini` table data
with solara.Column(align="center"):
display(
GT(dff)
.tab_header(title="S&P 500", subtitle=f"{start_date} to {end_date}")
.fmt_currency(columns=["open", "high", "low", "close"])
.fmt_date(columns="date", date_style="wd_m_day_year")
.fmt_number(columns="volume", compact=True)
.cols_hide(columns="adj_close")
)
with solara.Row(margin=8):
solara.Button("Multiply", on_click=multiply, outlined=True, color="primary")