Skip to content

page.run_thread does not receive kwargs #5318

Closed
@python-and-novella

Description

@python-and-novella
Contributor

Duplicate Check

  • I have searched the opened issues and there are no duplicates

Describe the bug

page.run_thread does not receive kwargs, but page.run_task receives.

Code sample

Code
import flet as ft
import asyncio
import time
from datetime import datetime

async def main(page: ft.Page):
    page.add(
        text := ft.Text(f'{datetime.now()}'),
        text2 := ft.Text(f'{datetime.now()}')
    )
    async def update_time(name=None,*,end=''):
        while True:
            text.value = f'{name} is {datetime.now()} {end}'
            text.update()
            await asyncio.sleep(1)
            
    page.run_task(update_time,'Time',end='.')
    
    def update_time_sync(name=None,*,end=''):
        while True:
            text2.value = f'{name} is {datetime.now()} {end}'
            text2.update()
            time.sleep(1)
            
    page.run_thread(update_time_sync,'Time',end='.')

    
ft.app(target=main)

To reproduce

Just run it.

Expected behavior

Both of them should work well, but only run_task works well.

Screenshots / Videos

Captures

Image

Image

Operating System

Windows

Operating system details

Windows 10 LTSC 2021

Flet version

0.28.2

Regression

I'm not sure / I don't know

Suggestions

Line 903 in flet/core/page.py should be:

def __context_wrapper(self, handler: Callable[..., Any]) -> Wrapper:
        def wrapper(*args,**kwargs):
            _session_page.set(self)
            handler(*args,**kwargs)

Logs

Logs
Future exception was never retrieved
future: <Future finished exception=TypeError("Page.__context_wrapper.<locals>.wrapper() got an unexpected keyword argument 'end'")>
Traceback (most recent call last):
  File "D:\Programs\Python\Python312\Lib\concurrent\futures\thread.py", line 59, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Page.__context_wrapper.<locals>.wrapper() got an unexpected keyword argument 'end'

Additional details

No response

Activity

changed the title [-]`page.run_thread` do not receive kwargs[/-] [+]`page.run_thread` does not receive kwargs[/+] on May 19, 2025
python-and-novella

python-and-novella commented on May 19, 2025

@python-and-novella
ContributorAuthor

If someone need fix it temporarily, plz check following code:

import flet as ft
import asyncio
import time
from datetime import datetime

# patch is here
from flet.core.page import _session_page
from typing import (
    Any,
    Callable,
)
from flet.core.types import (
    Wrapper,
)
class Page(__import__('flet').Page):
    def __context_wrapper(self, handler: Callable[..., Any]) -> Wrapper:
        def wrapper(*args,**kwargs):
            _session_page.set(self)
            handler(*args,**kwargs)
        return wrapper
# patch is over

async def main(page: ft.Page):

    # runtime patch code is here
    page = Page(
        page.connection,
        page.session_id,
        executor=page.executor,
        loop=page.loop
    )
    # runtime patch code is over

    page.add(
        text := ft.Text(f'{datetime.now()}'),
        text2 := ft.Text(f'{datetime.now()}')
    )
    async def update_time(name=None,*,end=''):
        while True:
            text.value = f'{name} is {datetime.now()} {end}'
            text.update()
            await asyncio.sleep(1)
            
    page.run_task(update_time,'Time',end='.')
    
    def update_time_sync(name=None,*,end=''):
        while True:
            text2.value = f'{name} is {datetime.now()} {end}'
            text2.update()
            time.sleep(1)
            
    page.run_thread(update_time_sync,'Time',end='.')

    
ft.app(target=main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @python-and-novella

      Issue actions

        `page.run_thread` does not receive kwargs · Issue #5318 · flet-dev/flet