Context
Manages the context for Flet controls, including page reference and auto-update behavior.
Context instance is accessed via ft.context.
Examples#
Get page#
import flet as ft
def main(page: ft.Page):
def button_click(e):
print("Page width:", ft.context.page.width)
page.add(ft.Button("Get page width", on_click=button_click))
ft.run(main)
Disable auto update#
import flet as ft
def main(page: ft.Page):
def button_click():
ft.context.disable_auto_update()
b.content = "Button clicked!"
# update just the button
b.update()
page.controls.append(ft.Text("This won't appear"))
# no page.update() will be called here
page.controls.append(b := ft.Button("Action!", on_click=button_click))
# page.update() - auto-update is enabled by default
ft.run(main)
Context
#
Manages the context for Flet controls, including page reference and auto-update behavior.
Context instance is accessed via ft.context.
page
#
page: Page
Returns the current Page associated with the context.
For example:
| RETURNS | DESCRIPTION |
|---|---|
Page
|
The current page. |
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
if property is called outside of Flet app. |
auto_update_enabled
#
auto_update_enabled() -> bool
Returns whether auto-update is enabled in the current context.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
disable_auto_update
#
Disables auto-update behavior for the current context.
For example:
import flet as ft
def main(page: ft.Page):
def button_click():
ft.context.disable_auto_update()
b.content = "Button clicked!"
# update just the button
b.update()
page.controls.append(ft.Text("This won't appear"))
# no page.update() will be called here
page.controls.append(b := ft.Button("Action!", on_click=button_click))
# page.update() - auto-update is enabled by default
ft.run(main)
enable_auto_update
#
Enables auto-update behavior for the current context.
For example:
import flet as ft
# disable auto-update globally for the app
ft.context.disable_auto_update()
def main(page: ft.Page):
# enable auto-update just inside main
ft.context.enable_auto_update()
page.controls.append(ft.Text("Hello, world!"))
# page.update() - we don't need to call it explicitly
ft.run(main)
reset_auto_update
#
Copies the parent auto-update state into the current context.