from pathlib import Path
import solara
from solara.toestand import Reactive, Ref
from utils import plus
from render_logger import RenderLogger, RenderDisplay
# reactive variables will trigger a component rerender
# when changed.
# When you change the default (now 0), hit the embedded browser
# refresh button to reset the state
global_clicks = solara.reactive(0)
@solara.component
def Counter(value: Reactive | int):
RenderLogger('Counter')
counter = solara.use_reactive(value)
with solara.Row(justify='space-evenly'):
solara.Info(f"Local Counter {counter.value}")
solara.Button(f"Local Counter+: {counter}", on_click=plus(counter))
@solara.component
def SimpleCounter(value: int):
RenderLogger('SimpleCounter')
solara.Info(f"SimpleCounter: {value}")
@solara.component
def Page():
RenderLogger('Page')
RenderDisplay()
solara.lab.theme.dark = True
solara.Style(Path('style.css'))
clicks = solara.use_reactive(0)
solara.Text(f"Global Clicks: {global_clicks.value}")
solara.Text(f"Local Clicks: {clicks.value}")
with solara.Card("Counter"):
solara.Markdown("""
The local `clicks` reactive is passed to
Counter and used there via `use_reactive`
""")
Counter(clicks)
with solara.Card("SimpleCounter"):
SimpleCounter(clicks.value)
with solara.Column(align='start'):
with solara.Row():
solara.Button(f"Clicked: {clicks.value}", on_click=plus(clicks))
solara.Button(f"Global Clicked: {global_clicks}", on_click=plus(global_clicks))
solara.Row()