Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log.push() not working #288

Closed
smojef opened this issue Jan 23, 2023 · 4 comments
Closed

log.push() not working #288

smojef opened this issue Jan 23, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@smojef
Copy link

smojef commented Jan 23, 2023

I don't understand why log.push() is not working:

from nicegui import ui

def process():
    with ui_container:
        ui.label("Begin process...")
        log = ui.log() 
        for i in range(10):
            log.push(f"process {i}")  # This does not work
        ui.label("End process")

ui.label('Testing process')
ui.button("Process", on_click=process).props('icon=start')
ui_container = ui.column()
    
ui.run(port=8888)

After pressing the button, the labels are shown before and after, and the log is shown. But the log entries are not.
NiceGUI

@falkoschindler
Copy link
Contributor

The ui.log component has been designed to solve the following problem: The app generates various messages throughout its runtime. The client should be able to see a current history of messages without retransmitting the whole history again and again. (The latter would be the case if we would simply append lines in a label.)

Because we had applications in mind, where only the most recent messages are of any use, we decided not to hold any state in Python but only in JavaScript on the client side. So if a client connects, the log is empty at first and gets filled line by line as new messages get pushed into the log.

In your example a new log is created. And before it gets transmitted to the client, multiple lines are pushed. But since the element did not reach the client yet, the lines get lost.

We should probably rethink this design. We could easily keep a copy of the last max_lines lines on the server. When sending a new log element to a client or sending a log element to a new client, these lines should be included.

@falkoschindler falkoschindler self-assigned this Jan 23, 2023
@falkoschindler falkoschindler added the enhancement New feature or request label Jan 23, 2023
@falkoschindler falkoschindler added this to the v1.1.5 milestone Jan 23, 2023
@smojef
Copy link
Author

smojef commented Jan 23, 2023

I actually found a workaround:

import asyncio
from nicegui import ui

async def process():
    with ui_container:
        ui.label("Begin process...")
        log = ui.log() 
        await asyncio.sleep(0.01)  # This actually waits for the log to be created
        for i in range(10):
            log.push(f"process {i}")  # So this works now
        ui.label("End process")

ui.label('Testing process')
ui.button("Process", on_click=process).props('icon=start')
ui_container = ui.column()
    
ui.run()

@falkoschindler
Copy link
Contributor

I created PR #292 to improve ui.log.

@rodja
Copy link
Member

rodja commented Jan 24, 2023

I just merged your branch!

@rodja rodja closed this as completed Jan 24, 2023
@falkoschindler falkoschindler modified the milestones: v1.1.5, v1.1.4 Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants