diff --git a/sample2.py b/sample2.py index fc25d90..96b4463 100644 --- a/sample2.py +++ b/sample2.py @@ -14,7 +14,7 @@ API_TOKEN = "d0vA6CsrY69N3JAOGtuZMEb9QpbJWPcoxhxRyBXZn8SIisB3weLKjMZwQRo8c2k9BRD w42_state = panel.rx(None) # TODO: must pass token, otherwise subscription should fail -attach_w42_state(rx_var=w42_state, system_id=SYSTEM_ID) +attach_w42_state(rx_var=w42_state, system_id=SYSTEM_ID, token=API_TOKEN) state_as_text = panel.bind(lambda s: f"W42 State:\n\n```\n{json.dumps(s, indent=2)}\n```\n\nReplace this with some awesome visuals", w42_state) @@ -39,6 +39,9 @@ class Configuration(BaseModel): location_lat: float = 0.0 location_lon: float = 0.0 +class Stats(BaseModel): + grid_cost_total: float = 0.0 + class SystemState(BaseModel): load_forecast: LoadForecast = LoadForecast() pv_forecast: PvForecast = PvForecast() @@ -47,6 +50,7 @@ class SystemState(BaseModel): buy_prices: list[float] = [] output: Output | None = None config: Configuration = Configuration() + stats: Stats = Stats() state: SystemState = panel.rx(lambda s: SystemState.model_validate(s) if s else SystemState())(w42_state) @@ -178,11 +182,16 @@ async def process_config_change(_btn): config_submit = panel.widgets.Button(name="Update Configuration", button_type="primary", width=200) config_submit.on_click(process_config_change) +def summary(state: SystemState) -> str: + total_cost = state.stats.grid_cost_total + return f"## Summary\n\nGrid {'cost' if total_cost > 0 else 'income'} total: {abs(state.stats.grid_cost_total):.2f} ZAR" + _ = panel.template.FastListTemplate( title="Sample W42 App", sidebar=[panel.pane.Markdown("This is a sample sidebar.")], main=[ # panel.pane.Markdown(state_as_text, sizing_mode='stretch_width'), + panel.pane.Markdown(panel.rx(summary)(state), sizing_mode='stretch_width'), panel.pane.ECharts( load_fc_chart_rx, sizing_mode='stretch_width',