Passing data from one to another ui.page #3203
-
I have a ui.page that receives data from an external device. I now need to send this data to another ui.page to process and show it. I tried writing the data into a global dictionary and using .bind_text() to bind it to a label. That worked but I not only visualise it but process it. Has anybody have an idea how to call a function on the second ui.page when receiving data on the first ui.page? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
One option, not particularly elegant, is to save a reference to the components you want to update. The basic idea is something like: saved = { }
def firstpage () :
saved ['name1'] = ui.label ()
saved ['name2'] = ui.card ()
def secondpage () :
...
newdata = receive_external ()
updatefirstpage (newdata)
def updatefirstpage (newdata) :
label = saved ['name1']
card = saved ['name2']
modify label or card with newdata.... CaveatsThis approach can be quite painful, depending on the number of components to update. It might be easier to save all references from firstpage in a nested structure like It would be easier if you could just save reference to the root element of firstpage, and traverse that as need in updatefirstpage() to modify components. However I have yet to find a documented way to retrieve the page root (it's possible by mucking around with Traversing the page is another issue - i.e. using the page root to find all subelements to modify. There was talk awhile ago of adding a search method to find subnodes, perhaps with CSS selector-like syntax. Not sure if that was implemented yet. Search the discussion board and docs. If not, it's possible with node._collect_descendants, but again undocumented and painful. MultiuserFinally, the above code assumes only a single user per nicegui server. If you serve multiple users / clients then you need to distinguish them. There may be something in the Request or Client objects that can help, I forget. If not, you'll need to use a per-client token like a cookie or client storage to distinguish one client from another. Then it becomes something like this : saved = { }
def firstpage () :
token = unique value per client
saved [token] ['name1'] = ui.label ()
saved [token] ['name2'] = ui.card ()
def secondpage () :
...
token = retrieve_token ()
newdata = receive_external ()
updatefirstpage (newdata)
def updatefirstpage (newdata, token) :
label = saved [token] ['name1']
card = saved [token] ['name2']
modify label or card with newdata.... Sorry it's not more detailed. This is just what I remember from the past. There may be more elegant ways to do it. Hope this helps. love the username 😄 |
Beta Was this translation helpful? Give feedback.
-
Hi ed2050, Thank you for the quick and detailed response! I've tried your solution and made some progress. However, I am still facing the issue of needing to call a function on the second page when data is received on the first page. To clarify my problem:
That's my code so far: reciever={"data":None}
@ui.page("/one/{data}")
def one(data):
reciever["data"]=data
@ui.page("/two")
def two():
ui.label().bind_text(reciever,"data")
switch=ui.switch("Delete Data")
def processdata():
if switch.value:
reciever["data"]=None
else:
write_into_database(reciever["data"]) I would appreciate any further suggestions you or anyone else might have on how to solve this issue. Specifically, I'm interested in how I can call the function Thanks again for your help! |
Beta Was this translation helpful? Give feedback.
-
Hi @wurstcasesenario! (Btw, I love your name! 🌭😀) I wonder why you need to process the data "on the second page". When calling the data = {'number': 0}
def process_number():
print(f'Squaring the number {data["number"]}')
data['number'] = data['number'] ** 2
@ui.page("/one/{number}")
def one(number: int):
data['number'] = number
process_number()
visualize_number.refresh()
@ui.refreshable
def visualize_number():
ui.label(f'Here are {data["number"]} boxes:')
for i in range(data['number']):
with ui.card():
ui.label(f'Box {i}')
@ui.page("/two")
def two():
visualize_number() |
Beta Was this translation helpful? Give feedback.
-
Hi @falkoschindler, @ed2050, reciever=[]
@ui.page ("/one/{data}")#opened by the Arduino
def one(data):
reciever. append (data)#data is the Laptops sessionid, send by the Arduino
callback. refresh()
@ui. page ("/two")#open on the Laptop
def two():
ui. label(app.storage.browser[“id"])#shows sessionid from Laptop
@ui. refreshable
def processdata():
ui.label(app.storage.browser[“id”])#shows sessionid from Arduino
for data in reciever:
if data==app.storage. browser[“id"]:
ui. label("True")
else:
ui. label("False")
processdata()
global callback
callback=processdata I hope that I described my problem well enough and thank you in advance. |
Beta Was this translation helpful? Give feedback.
You can add a parameter
browser_id
to the refreshable that is set during its initial call and omitted when calling.refresh()
:Note that I renamed some things in your code to better understa…