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#
prevent_links
#
List of url-prefixes that should not be followed/loaded/downloaded.
Events#
on_console_message
#
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
#
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
#
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
#
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
#
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
#
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
#
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
#
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
#
can_go_back() -> bool
Whether there's a back history item.
Note
Works only on the following platforms: iOS, Android and macOS.
Returns:
-
bool
–True
if there is a back history item,False
otherwise.
can_go_forward
#
can_go_forward() -> bool
Whether there's a forward history item.
Note
Works only on the following platforms: iOS, Android and macOS.
Returns:
-
bool
–True
if there is a forward history item,False
otherwise.
clear_cache
#
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
#
Clears the local storage used by the WebView.
Note
Works only on the following platforms: iOS, Android and macOS.
disable_zoom
#
Disables zooming using the on-screen zoom controls and gestures.
Note
Works only on the following platforms: iOS, Android and macOS.
enable_zoom
#
Enables zooming using the on-screen zoom controls and gestures.
Note
Works only on the following platforms: iOS, Android and macOS.
get_current_url
#
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
None
if no URL was ever loaded.
get_title
#
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
#
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
#
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
#
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
#
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
#
load_request
#
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
#
Reloads the current URL.
Note
Works only on the following platforms: iOS, Android and macOS.
run_javascript
#
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
#
scroll_to
#
set_javascript_mode
#
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