Check if client is native #5378
-
First Check
Example Codefrom nicegui import ui, app
from webview import FileDialog
@ui.page('/')
async def page():
"""The UI entry point (after nicegui.ui.run() is called)."""
async def open_dialog():
is_native_client = True
# suggested solution (perhaps by saving the native client ID and comparing it with `ui.context.client.id`)
# is_native_client = ui.context.client.is_native()
if is_native_client:
# only the native client can reach this code
selected_folder = await app.native.main_window.create_file_dialog(dialog_type=FileDialog.FOLDER)
dir.set_value(selected_folder)
else:
# web browsers that navigated to "http://localhost:1234" reach this code
ui.notify("Not possible in web browser!")
ui.label("Selected folder:")
dir = ui.input("None").classes('w-screen')
ui.button("Browse...", on_click=lambda: open_dialog())
ui.run(port=1234, native=True)DescriptionI would like to open a file dialog when using the native window. In my use case, the UI can also be opened remotely via a web browser. Alternatively, I could use a tkinter file dialog as proposed in #283 (comment), but this would obviously only work if the file structure of the remote user is identical to that of the host/native, as the file dialog shows the file system of the remote user and not the host. I am aware of the local file picker example , but this is not a use case I wish to handle in my implementation NiceGUI Version3.2.0 Python Version3.12.7 BrowserChrome, Other Operating SystemWindows Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
You can set a custom user agent for native mode via start_args: #!/usr/bin/env python3
from fastapi import Request
from nicegui import app, ui
def root(request: Request):
user_agent = request.headers.get('user-agent', '').lower()
if 'pywebview' in user_agent:
ui.label('✓ Native app detected').classes('text-green text-2xl')
else:
ui.label('✗ Regular browser detected').classes('text-blue text-2xl')
app.native.start_args['user_agent'] = 'NiceGUI-Native/1.0 (pywebview)'
ui.run(root, native=True) |
Beta Was this translation helpful? Give feedback.
You can set a custom user agent for native mode via start_args: