webui-dev / python-webui

Use any web browser as GUI, with Python in the backend and HTML5 in the frontend.
https://webui.me/
MIT License
235 stars 11 forks source link

Python API, show() and close() methods problem #13

Closed JerryFox closed 9 months ago

JerryFox commented 9 months ago

Python API

Problem with MyWindow.show() method

in documentation:

If you need to update the whole UI content, you can also use the same function show(), which allows you to refresh the window UI with any new HTML content.

on my computer (windows 11 OS):

Problem with MyWindow.close() method

The method does not close the window.

I found this project on purpose. I hope it could be helpful for me. I need the Python interface. I am just testing it if it is suitable for me. Is it a good way to communicate through issues or is there another better way?

Thank you!

Jaroslav Vysoký Czech Republic

hassandraga commented 9 months ago

I just test it and it's working fine. Can you share your code?

from webui import webui

MyWindow1 = webui.window()
MyWindow2 = webui.window()
count = 10

def events(e : webui.event):
    global count
    count = count + 1
    MyWindow2.show(f'<html><script src="webui.js"></script> count = {count} </html>')

MyWindow1.bind("test", events)
MyWindow1.show('<html><script src="webui.js"></script> <button id="test">update window 2</button> </html>')

webui.wait()
JerryFox commented 9 months ago

ERR_CONNECTION_REFUSED - it was probably my fault

Your code works fine on my comp too.

Here is my code:

from webui import webui
import time 

MyWindow = webui.window()

html = "<html>Hello</html>"
new_html = "<html>New World!</html>"

# Open a window
MyWindow.show(html, webui.browser.chrome)
# it is OK

input("press...")

# Refresh the same window with the new content
MyWindow.show(new_html, webui.browser.chrome)
# it opens a new window

input("press...")

MyWindow.close()
# it does not close the window

input("press...")
hassandraga commented 9 months ago

I see the issue. when you call .show(), the window needs a couple of second to be ready (running the web browser and connect to websocket). So the second .show() will be missed.

I noticed some users adding a delay after .show(), which is not the best thing to do... I guess we should make WebUI itself wait for window to connect. So, let me fix this. Thank you for the report.

JerryFox commented 9 months ago

You are not right. There is an input() function in the code between the two calls of show() method. I was afraid of that what you said and due to it I tried to wait different time between the two calls of the show() command. The second call is not missed but it shows a SECOND window.

And why the close() method does not close the window???

Thx!

J.

hassandraga commented 9 months ago

Sorry, I didn't catch that you are missing webui.js in your HTML. So just update your code with this:

html = '<html><script src="webui.js"></script> Hello</html>'
new_html = '<html><script src="webui.js"></script> New World!</html>'

However, WebUI v2.4.2 has now .Show that wait for connection. Thanks again for the report.

JerryFox commented 9 months ago

Yes, that was the problem! The online documentation is little bit confusing...

Thanks!

J.

hassandraga commented 9 months ago

Yes, it's old (2.4.0)... It will be updated soon 👍

hassandraga commented 9 months ago

The update is started: https://webui.me/docs/2.4/#/ Thank you.