2025-11-18 13:06:15 +02:00
|
|
|
import json
|
2025-11-18 12:51:30 +02:00
|
|
|
import panel
|
2025-11-18 20:40:56 +02:00
|
|
|
from random import randint
|
|
|
|
|
from typing import Any
|
2025-11-18 12:51:30 +02:00
|
|
|
|
|
|
|
|
from watt42_viewlib import attach_w42_state
|
|
|
|
|
|
|
|
|
|
w42_state = panel.rx(None)
|
|
|
|
|
attach_w42_state(rx_var=w42_state, system_id="fb2b91ce-383e-4356-96b3-b6405dacb353")
|
|
|
|
|
|
2025-11-18 13:06:15 +02:00
|
|
|
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)
|
2025-11-18 12:51:30 +02:00
|
|
|
|
|
|
|
|
state_pane = panel.pane.Markdown(state_as_text, sizing_mode='stretch_width')
|
|
|
|
|
|
2025-11-18 20:40:56 +02:00
|
|
|
value = w42_state.rx.value
|
|
|
|
|
|
|
|
|
|
multiplier = panel.widgets.IntSlider(name='Multiplier', start=1, end=10, step=1, value=1)
|
|
|
|
|
|
|
|
|
|
def value_display(state: dict[str, Any], multiplier: int) -> str:
|
|
|
|
|
if state and 'value' in state:
|
|
|
|
|
return f"Current W42 State Value times multiplier: {state['value'] * multiplier}"
|
|
|
|
|
return "Current W42 State Value: N/A"
|
|
|
|
|
|
|
|
|
|
def value_or_zero(state: dict[str, Any]) -> int:
|
|
|
|
|
if state and 'value' in state:
|
|
|
|
|
return state['value']
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
def array_from_value(x: int) -> list[int]:
|
|
|
|
|
return [x * randint(1, 20) for _ in range(25)]
|
|
|
|
|
|
|
|
|
|
def times2(x: int) -> int:
|
|
|
|
|
return x * 2
|
|
|
|
|
|
|
|
|
|
times2_rx = panel.rx(times2)(panel.rx(value_or_zero)(w42_state))
|
|
|
|
|
|
|
|
|
|
markdown_rx = panel.rx(lambda v: f"## Value times 2 is: {v}, array={array_from_value(v)}")(times2_rx)
|
|
|
|
|
|
|
|
|
|
array_rx = panel.rx(array_from_value)(panel.rx(value_or_zero)(w42_state))
|
|
|
|
|
|
|
|
|
|
echart_bar_rx = panel.pane.ECharts(
|
|
|
|
|
panel.rx(lambda arr: {
|
|
|
|
|
'xAxis': {
|
|
|
|
|
'type': 'category',
|
|
|
|
|
'data': [f'Item {i+1}' for i in range(len(arr))]
|
|
|
|
|
},
|
|
|
|
|
'yAxis': {
|
|
|
|
|
'type': 'value'
|
|
|
|
|
},
|
|
|
|
|
'series': [{
|
|
|
|
|
'data': arr,
|
|
|
|
|
'type': 'bar'
|
|
|
|
|
}]
|
|
|
|
|
})(array_rx),
|
|
|
|
|
sizing_mode='stretch_width',
|
|
|
|
|
height=400
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-18 12:51:30 +02:00
|
|
|
_ = panel.template.FastListTemplate(
|
|
|
|
|
title="Sample W42 View App",
|
|
|
|
|
sidebar=[panel.pane.Markdown("This is a sample sidebar.")],
|
2025-11-18 20:40:56 +02:00
|
|
|
main=[
|
|
|
|
|
panel.pane.Markdown("# Welcome to the Main Content Area"),
|
|
|
|
|
echart_bar_rx,
|
|
|
|
|
# state_pane, panel.pane.Markdown(f"Current W42 State Value: {value}"),
|
|
|
|
|
panel.ReactiveExpr(panel.rx(value_display)(w42_state, multiplier), widget_location='top'),
|
|
|
|
|
panel.pane.Markdown(markdown_rx),
|
|
|
|
|
],
|
2025-11-18 12:51:30 +02:00
|
|
|
).servable()
|