WebView#
Display web content inside your Flet app using the flet-webview extension, which wraps Flutter's webview_flutter package.
Platform Support#
| Platform | Windows | macOS | Linux | iOS | Android | Web |
|---|---|---|---|---|---|---|
| Supported | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ |
Usage#
Add flet-webview to your project dependencies:
Example#
import flet_webview as fwv
import flet as ft
def main(page: ft.Page):
page.add(
fwv.WebView(
url="https://flet.dev",
on_page_started=lambda _: print("Page started"),
on_page_ended=lambda _: print("Page ended"),
on_web_resource_error=lambda e: print("WebView error:", e.data),
expand=True,
)
)
ft.run(main)
Description#
Inherits: LayoutControl
Easily load webpages while allowing user interaction.
Note
Works only on the following platforms: iOS, Android, macOS and Web.
Properties
-
bgcolor(ColorValue | None) –Defines the background color of the WebView.
-
prevent_links(list[str] | None) –List of url-prefixes that should not be followed/loaded/downloaded.
-
url(str) –The URL of the web page to load.
Events
-
on_console_message(EventHandler[WebViewConsoleMessageEvent] | None) –Fires when a log message is written to the JavaScript console.
-
on_javascript_alert_dialog(EventHandler[WebViewJavaScriptEvent] | None) –Fires when the web page attempts to display a JavaScript alert() dialog.
-
on_page_ended(ControlEventHandler[WebView] | None) –Fires when all the webview page loading processes are ended.
-
on_page_started(ControlEventHandler[WebView] | None) –Fires soon as the first loading process of the webview page is started.
-
on_progress(ControlEventHandler[WebView] | None) –Fires when the progress of the webview page loading is changed.
-
on_scroll(EventHandler[WebViewScrollEvent] | None) –Fires when the web page's scroll position changes.
-
on_url_change(ControlEventHandler[WebView] | None) –Fires when the URL of the webview page is changed.
-
on_web_resource_error(ControlEventHandler[WebView] | None) –Fires when there is error with loading a webview page resource.
Methods
-
can_go_back–Whether there's a back history item.
-
can_go_forward–Whether there's a forward history item.
-
clear_cache–Clears all caches used by the WebView.
-
clear_local_storage–Clears the local storage used by the WebView.
-
disable_zoom–Disables zooming using the on-screen zoom controls and gestures.
-
enable_zoom–Enables zooming using the on-screen zoom controls and gestures.
-
get_current_url–Gets the current URL that the WebView is displaying or
None -
get_title–Get the title of the currently loaded page.
-
get_user_agent–Get the value used for the HTTP
User-Agent:request header. -
go_back–Goes back in the history of the webview, if
can_go_back()isTrue. -
go_forward–Goes forward in the history of the webview,
-
load_file–Loads the provided local file.
-
load_html–Loads the provided HTML string.
-
load_request–Makes an HTTP request and loads the response in the webview.
-
reload–Reloads the current URL.
-
run_javascript–Runs the given JavaScript in the context of the current page.
-
scroll_by–Scrolls by the provided number of webview pixels.
-
scroll_to–Scrolls to the provided position of webview pixels.
-
set_javascript_mode–Sets the JavaScript mode of the WebView.
Properties#
bgcolor
class-attribute
instance-attribute
#
bgcolor: ColorValue | None = None
Defines the background color of the WebView.
prevent_links
class-attribute
instance-attribute
#
List of url-prefixes that should not be followed/loaded/downloaded.
Events#
on_console_message
class-attribute
instance-attribute
#
on_console_message: (
EventHandler[WebViewConsoleMessageEvent] | None
) = None
Fires when a log message is written to the JavaScript console.
Note
Works only on the following platforms: iOS, Android and macOS.
on_javascript_alert_dialog
class-attribute
instance-attribute
#
on_javascript_alert_dialog: (
EventHandler[WebViewJavaScriptEvent] | None
) = None
Fires when the web page attempts to display a JavaScript alert() dialog.
Note
Works only on the following platforms: iOS, Android and macOS.
on_page_ended
class-attribute
instance-attribute
#
on_page_ended: ControlEventHandler[WebView] | None = None
Fires when all the webview page loading processes are ended.
Event handler argument's data property is of type str
and contains the URL.
Note
Works only on the following platforms: iOS, Android and macOS.
on_page_started
class-attribute
instance-attribute
#
on_page_started: ControlEventHandler[WebView] | None = None
Fires soon as the first loading process of the webview page is started.
Event handler argument's data property is of type
str and contains the URL.
Note
Works only on the following platforms: iOS, Android and macOS.
on_progress
class-attribute
instance-attribute
#
on_progress: ControlEventHandler[WebView] | None = None
Fires when the progress of the webview page loading is changed.
Event handler argument's data property is of type
int and contains the progress value.
Note
Works only on the following platforms: iOS, Android and macOS.
on_scroll
class-attribute
instance-attribute
#
on_scroll: EventHandler[WebViewScrollEvent] | None = None
Fires when the web page's scroll position changes.
Note
Works only on the following platforms: iOS, Android and macOS.
on_url_change
class-attribute
instance-attribute
#
on_url_change: ControlEventHandler[WebView] | None = None
Fires when the URL of the webview page is changed.
Event handler argument's data property is of type
str and contains the new URL.
Note
Works only on the following platforms: iOS, Android and macOS.
on_web_resource_error
class-attribute
instance-attribute
#
on_web_resource_error: (
ControlEventHandler[WebView] | None
) = None
Fires when there is error with loading a webview page resource.
Event handler argument's data property is of type
str and contains the error message.
Note
Works only on the following platforms: iOS, Android and macOS.
Methods#
can_go_back
async
#
can_go_back() -> bool
Whether there's a back history item.
Note
Works only on the following platforms: iOS, Android and macOS.
Returns:
-
bool–Trueif there is a back history item,Falseotherwise.
can_go_forward
async
#
can_go_forward() -> bool
Whether there's a forward history item.
Note
Works only on the following platforms: iOS, Android and macOS.
Returns:
-
bool–Trueif there is a forward history item,Falseotherwise.
clear_cache
async
#
Clears all caches used by the WebView.
The following caches are cleared
- Browser HTTP Cache
- Cache API caches. Service workers tend to use this cache.
- Application cache
Note
Works only on the following platforms: iOS, Android and macOS.
clear_local_storage
async
#
Clears the local storage used by the WebView.
Note
Works only on the following platforms: iOS, Android and macOS.
disable_zoom
async
#
Disables zooming using the on-screen zoom controls and gestures.
Note
Works only on the following platforms: iOS, Android and macOS.
enable_zoom
async
#
Enables zooming using the on-screen zoom controls and gestures.
Note
Works only on the following platforms: iOS, Android and macOS.
get_current_url
async
#
get_current_url() -> str | None
Gets the current URL that the WebView is displaying or None
if no URL was ever loaded.
Note
Works only on the following platforms: iOS, Android and macOS.
Returns:
-
str | None–The current URL that the WebView is displaying or
Noneif no URL was ever loaded.
get_title
async
#
get_title() -> str | None
Get the title of the currently loaded page.
Note
Works only on the following platforms: iOS, Android and macOS.
Returns:
-
str | None–The title of the currently loaded page.
get_user_agent
async
#
get_user_agent() -> str | None
Get the value used for the HTTP User-Agent: request header.
Note
Works only on the following platforms: iOS, Android and macOS.
Returns:
-
str | None–The value used for the HTTP
User-Agent:request header.
go_back
async
#
Goes back in the history of the webview, if can_go_back() is True.
Note
Works only on the following platforms: iOS, Android and macOS.
go_forward
async
#
Goes forward in the history of the webview,
if can_go_forward() is True.
Note
Works only on the following platforms: iOS, Android and macOS.
load_file
async
#
load_file(path: str)
Loads the provided local file.
Note
Works only on the following platforms: iOS, Android and macOS.
Parameters:
-
path(str) –The absolute path to the file.
load_html
async
#
load_request
async
#
load_request(url: str, method: RequestMethod = GET)
Makes an HTTP request and loads the response in the webview.
Parameters:
-
url(str) –The URL to load.
-
method(RequestMethod, default:GET) –The HTTP method to use.
Note
Works only on the following platforms: iOS, Android and macOS.
reload
async
#
Reloads the current URL.
Note
Works only on the following platforms: iOS, Android and macOS.
run_javascript
async
#
run_javascript(value: str)
Runs the given JavaScript in the context of the current page.
Parameters:
-
value(str) –The JavaScript code to run.
Note
Works only on the following platforms: iOS, Android and macOS.
scroll_by
async
#
scroll_to
async
#
set_javascript_mode
async
#
Sets the JavaScript mode of the WebView.
Note
- Works only on the following platforms: iOS, Android and macOS.
- Disabling the JavaScript execution on the page may result to unexpected web page behaviour.
Parameters:
-
mode(JavaScriptMode) –The JavaScript mode to set.
See also types:
- RequestMethod
- LogLevelSeverity
- WebViewConsoleMessageEvent
- WebViewJavaScriptEvent
- WebViewScrollEvent