Py.Cafe

rob.on.lists/

solara-inventory-editor-only-works-first-time

Updates show first time, but 2nd update does not show in output frame

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# show map of messages, display map in Page() after a change

import solara
import pandas

store={'books':'3','shelves':'2','racks':'1'} # inventory
rstore = solara.reactive(store) # make inventory changes show up in frame

@solara.component
def MarkdownEditor(fld:str):
    md, set_md = solara.use_state(store[fld])
    def store_updater(newval):
        set_md(newval)
        store[fld]=newval  # update map
        # rstore.set(None)  # extra hard forcing change to reactive variable 
        rstore.set(store)
        print('updated',store)  # proof that on_value was triggered
    with solara.VBox() as main:
        solara.InputText('value '+fld,value=md, on_value=store_updater)
        solara.Markdown(md)
    return main

@solara.component
def Page():
    for fld in store.keys():
        MarkdownEditor(fld)
    # redisplay DataFrame after each change in store
    print('showing as frame',rstore.value)
    #solara.DataFrame(pandas.DataFrame([rstore.value],index=['value']).T.reset_index().rename({'index':'item'},axis=1))
    solara.DataFrame(pandas.DataFrame([[k,v] for k,v in rstore.value.items()],columns=['item','value']))