Comments (8)
Yes sir. I am trying to write the minimum reproducible code.
from nicegui.
Thanks a lot, @bin-san! This is very helpful.
Actually, there's another error happening because the backward
function has no current UI context:
The current slot cannot be determined because the slot stack for this task is empty.
This may happen if you try to create UI from a background task.
To fix this, enter the target slot explicitly using `with container_element:`.
But even after fixing this problem by explicitly using the current client
, the warning about a missing storage_secret
remains:
@ui.page('/')
def index():
app.storage.user['name'] = 'San'
client = ui.context.client
def f(v):
with client:
ui.notify(app.storage.user.get('name'))
return v
ui.label().bind_text_from(app.storage.user, 'name', backward=f)
from nicegui.
Need more details, I can't reproduce it.
from nicegui.
Steps to reproduce:
-
get the code at: https://gist.github.com/bin-san/ede5d876aba43eff290a776915659345
-
run the file
-
Click on the indigo color arrow button of the newly created card
->
-
Now check your log:
NiceGUI ready to go on http://localhost:8999, http://172.17.0.1:8999, and http://192.168.1.2:8999
app.storage.user needs a storage_secret passed in ui.run()
Traceback (most recent call last):
File "/home/point/PycharmProjects/PartyRooms/.venv/lib/python3.12/site-packages/nicegui/background_tasks.py", line 52, in _handle_task_result
task.result()
File "/home/point/PycharmProjects/PartyRooms/.venv/lib/python3.12/site-packages/nicegui/binding.py", line 39, in refresh_loop
_refresh_step()
File "/home/point/PycharmProjects/PartyRooms/.venv/lib/python3.12/site-packages/nicegui/binding.py", line 49, in _refresh_step
value = transform(_get_attribute(source_obj, source_name))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/point/PycharmProjects/PartyRooms/main.py", line 444, in monitor_cmd
if not app.storage.user.get('last-command'):
^^^^^^^^^^^^^^^^
File "/home/point/PycharmProjects/PartyRooms/.venv/lib/python3.12/site-packages/nicegui/storage.py", line 143, in user
raise RuntimeError('app.storage.user needs a storage_secret passed in ui.run()')
RuntimeError: app.storage.user needs a storage_secret passed in ui.run()
from nicegui.
Hi @bin-san, this does indeed look like a bug. At least the error message doesn't make too much sense if the storage_secret
is given in ui.run
.
In order to help narrowing down this bug and to compare it to #2520: Could you, please, try to extract a minimum reproducible code example? Thanks!
Why is it important to provide a minimal reproducible example?
from nicegui.
from nicegui import ui, app
@ui.page('/')
def index():
app.storage.user['name'] = 'San'
def f(v):
ui.notify(app.storage.user.get('name'))
return v
ui.label().bind_text_from(app.storage.user, 'name', backward=f)
ui.run(show=False, storage_secret='bin-san-2002')
The problem is that the function given in backward function can not access user storage.
from nicegui.
I set a print in storage.py
,And I found starlette.middleware.base._CachedRequest object
becomes None during first visiting page.
Then I look it up in starlette's repository,similar question is founded:
RuntimeError("No response returned")
in BaseHTTPMiddleware
ยท Issue #2516 ยท encode/starlette
encode/starlette#2516
Maybe it is helpful.Or it is a direction for others who would solve this problem.
from nicegui.
@falkoschindler Hi. Same problem accessing app.storage.user from an app.websocket
from nicegui import ui, app
from fastapi import WebSocket
from uuid import uuid4
from time import time_ns
def uid():
"""Generates an unique id"""
return f'{uuid4()}-{time_ns()}'
app.add_media_file(local_file='./sample.mp4', url_path='/video')
app.storage.general['octopus'] = {}
@app.websocket('/sync_video/{video_id}')
async def sync_video(websocket:WebSocket, video_id):
with ui.context.client:
if not app.storage.user.get('id'):
app.storage.user['id'] = uid()
await websocket.accept()
while True:
msg = await websocket.receive_text()
await websocket.send_text(f'Got {msg}')
@ui.page('/{video_id}')
def index(video_id):
ui.add_head_html(f'''
<script defer>
window.ws = new WebSocket('ws://{host}:{port}/sync_video/{video_id}')
window.ws.onmessage = (event)=>{{
window.display1.innerText = event.data
}}
</script>
''')
ui.input('Enter a message').classes('w-full').props('id=input1')
ui.button('Broadcast').on('click', js_handler='''
(event)=>window.ws.send(input1.value)
''')
ui.label().classes('w-full').props('id=display1')
host = 'localhost'
port = 8000
ui.run(host=host, port=port, show=False, storage_secret='bin-san')
from nicegui.
Related Issues (20)
- Javascript libraries not discovered in 1.4.27 HOT 2
- on air not working from UK HOT 2
- Make `client.ip` available before socket connection is established
- Input unable to add forward slash (/) when it is bind to path HOT 2
- on_upload doesn't seem to be working HOT 10
- Connection lost within 3 seconds when app is deployed on kubernetes HOT 1
- TypeError: Client.__init__() missing 1 required keyword-only argument: 'request' HOT 5
- Pytest for pages with a URL other than root ("/") fail. HOT 2
- Tooltip HOT 4
- ui.clipboard.read does not work in Safari and native mode on Mac HOT 1
- ui.number displayed number format not maintained HOT 1
- Tooltip not working with ui.upload component HOT 2
- ui.codemirror not updating when the binded dictionary changes HOT 3
- 3D objects get stuck in scene when reloading. HOT 1
- Updating Ploty chart with a large dataset causes the UI to hang HOT 6
- ui.select() dropdown width unexpected behaviour
- Reloading is very slow HOT 10
- `True (error) or False (no error)` in `Input validation` should be `True (no error) or False (error)` HOT 1
- How to cut startup time in half while native=True. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nicegui.