Giter VIP home page Giter VIP logo

Comments (12)

gavinpotter avatar gavinpotter commented on July 4, 2024 2

No - I don't have a minimal example yet. I'm struggling to find what in the code causes the problem. My code is complex and uses the win32 api to position windows on the screen etc as well as using nicegui. I am trying to isolate the issue, but so far haven't had any luck.
I'd suggest closing the issue as solved. If I can create a minimal example I'll open a new issue. I don't think there is anything you can do as your suggested minimal examples work fine.

Working with version 1.4.18 is also fine for me. Again, I congratulate you on producing a great product. Its, imho, much better than any of the other gui libraries and treads a nice line between providing all the functionality required and ease of use.

from nicegui.

gavinpotter avatar gavinpotter commented on July 4, 2024

It works fine in version 1.4.18 - I haven't tried any later versions other than 1.4.26

from nicegui.

falkoschindler avatar falkoschindler commented on July 4, 2024

Hi @gavinpotter,

Technically there could be another client connected to a native app. Therefore awaiting JavaScript would still not be well defined.
But you can simply wrap your page into a page function and the problem should be gone.

from nicegui.

gavinpotter avatar gavinpotter commented on July 4, 2024

Thanks - but I'm struggling with your work around. Here is a minimal replicable example. I maybe doing something wrong with the page wrapping? When I hover the mouse over the label, I get the error message I described above.

#Minimal example
from nicegui import ui,app

class testClass():

	@ui.page("/testClassinit")
	def __init__(self):
		self.label = ui.label("Hello world")
		self.label.on("mouseenter",lambda: self.mouseEnter())
		
	@ui.page("/mouseEnter")
	async def mouseEnter(self):
		s = await ui.run_javascript("window.innerHeight;")
		ui.label("Mouse entered label")

app.on_startup(testClass)

ui.run(native=True)

from nicegui.

falkoschindler avatar falkoschindler commented on July 4, 2024

The @page decorator doesn't work for class methods or initializers, but for functions only.
Assuming you want to keep app.on_startup for some reason, you could arrange it like this:

def createUi():
    @ui.page('/')
    def index():
        label = ui.label("Hello world")
        label.on("mouseenter", mouseEnter)

    async def mouseEnter():
        innerHeight = await ui.run_javascript("window.innerHeight;")
        ui.label(f"Mouse entered label ({innerHeight=})")

app.on_startup(createUi)

from nicegui.

gavinpotter avatar gavinpotter commented on July 4, 2024

Unfortunately my application is divided into 42 classes each performing a separate task for the user. Each class encapsulates state for the user, a set of class methods and a user interface built using nicegui. It is designed to run in native mode, so each user has a copy on their own laptop. Its not really practical to rewrite it in the manner you describe.

Thank you anyway.

from nicegui.

falkoschindler avatar falkoschindler commented on July 4, 2024

Ok, so how would your minimal example look like without page decorators? What is your main entry point?

Without adding page functions to testClass, you could call it from a page function:

def create_main_page():
    @ui.page('/')
    def main_page():
        testClass()

app.on_startup(create_main_page)

from nicegui.

gavinpotter avatar gavinpotter commented on July 4, 2024

Thank you that is very close and works on the minimal example, but unfortunately not on my full application. The reason why is that my main entry point is written as a coroutine not as a function (it automatically pulls in a number of files from the user's machine as well as setting up the nicegui interface and performs some other tasks best handled as async). The approach you suggested doesn't appear to work with coroutines. However, I can probably rewrite my code and separate out the interface creation into a function called with app.on_startup, and trigger the other functionality some other way.

Thanks for your help.

from nicegui.

rodja avatar rodja commented on July 4, 2024

It should also work with coroutines. And I wonder why you would need the on_startup at all. Have you tried

@ui.page('/')
async def main_page():
    t = testClass()
    await t.build_ui() # or some other async functions of your classes

from nicegui.

gavinpotter avatar gavinpotter commented on July 4, 2024

Thank you, I will try it in the way suggested on Monday.

from nicegui.

gavinpotter avatar gavinpotter commented on July 4, 2024

OK - I've tried it with my code. I get different behaviour when I use app.on_startup and @ui.page("/"). It works properly with app.on_startup() but I get presented with a blank screeen when using ui.page("/"). I'm trying to find out why - its not to do with running in native mode, but I've not been able to isolate where my code is causing a problem. I'd suggest either mark this as closed, or leave it open until I figure out what is happening. As an observation, if you only have one client attached then the "RuntimeError: Cannot await JavaScript responses on the auto-index page. There could be multiple clients connected and it is not clear which one to wait for." is unnecessary which would resolve my problem.

from nicegui.

rodja avatar rodja commented on July 4, 2024

It works properly with app.on_startup() but I get presented with a blank screeen when using ui.page("/")

Do you have an minimal example?

As an observation, if you only have one client attached then the "RuntimeError: Cannot await JavaScript responses on the auto-index page. There could be multiple clients connected and it is not clear which one to wait for." is unnecessary which would resolve my problem.

But there is no guarantee that you'll always have just one client. We figured it would be quite hard to find such an issue if the error only appears when a second client is connected.

from nicegui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.