Skip to main content

WebView

Display web content in a WebView to be shown in your Flet apps.

It is powered by the webview_flutter and webview_flutter_web Flutter packages.

Platform Support​

PlatformWindowsmacOSLinuxiOSAndroidWeb
Supported❌✅❌✅✅✅

Usage​

Add flet-webview to your project dependencies:

uv add flet-webview

Example​

WebView​

import flet as ft
import flet_webview as fwv


def main(page: ft.Page):
page.add(
ft.SafeArea(
expand=True,
content=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,
),
)
)


if __name__ == "__main__":
ft.run(main)

Troubleshooting​

NET::ERR_CLEARTEXT_NOT_PERMITTED Error​

If you run into the NET::ERR_CLEARTEXT_NOT_PERMITTED error in Android, then the app you’re using is trying to access a web page that wants to transmit cleartext or unsecured information. Android blocks apps from doing this in order to avoid compromising user data.

For more details, see this and this.

To fix it, your app's configuration (precisely, the manifest application attributes) needs to be modified as follows:

[tool.flet.android.manifest_application]
usesCleartextTraffic = "true"

Description​

Easily load webpages while allowing user interaction.

Note

Supported only on the following platforms: iOS, Android, macOS, and Web. Concerning Windows and Linux support, subscribe to this issue.

Inherits: LayoutControl

Properties

  • bgcolor - Defines the background color of the WebView.
  • prevent_links - List of url-prefixes that should not be followed/loaded/downloaded.
  • url - The URL of the web page to load.

Events

  • on_console_message - Fires when a log message is written to the JavaScript console.
  • on_javascript_alert_dialog - Fires when the web page attempts to display a JavaScript alert() dialog.
  • on_page_ended - Fires when all the webview page loading processes are ended.
  • on_page_started - Fires soon as the first loading process of the webview page is started.
  • on_progress - Fires when the progress of the webview page loading is changed.
  • on_scroll - Fires when the web page's scroll position changes.
  • on_url_change - Fires when the URL of the webview page is changed.
  • on_web_resource_error - 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 if no URL was ever loaded.
  • 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() is True.
  • go_forward - Goes forward in the history of the webview, if can_go_forward is True.
  • 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​

bgcolorclass-attributeinstance-attribute​

bgcolor: ColorValue | None = None

Defines the background color of the WebView.

prevent_links: list[str] | None = None

List of url-prefixes that should not be followed/loaded/downloaded.

urlclass-attributeinstance-attribute​

url: str | None = None

The URL of the web page to load.

Note

A file:// URL pointing to a local file is supported on the following platforms only: iOS, Android, and macOS. It loads that file's sibling assets (scripts, stylesheets, images) as well, and is equivalent to calling load_file with the same file once this control is mounted. On web, only URLs the browser can load in an iframe work.

Events​

on_console_messageclass-attributeinstance-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_dialogclass-attributeinstance-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_endedclass-attributeinstance-attribute​

on_page_ended: ControlEventHandler[WebView] | None = None

Fires when all the webview page loading processes are ended.

The data property of the event handler argument is of type str and contains the URL.

Note

Works only on the following platforms: iOS, Android and macOS.

on_page_startedclass-attributeinstance-attribute​

on_page_started: ControlEventHandler[WebView] | None = (
    None
)

Fires soon as the first loading process of the webview page is started.

The data property of the event handler argument is of type str and contains the URL.

Note

Works only on the following platforms: iOS, Android and macOS.

on_progressclass-attributeinstance-attribute​

on_progress: ControlEventHandler[WebView] | None = None

Fires when the progress of the webview page loading is changed.

The data property of the event handler argument is of type int and contains the progress value.

Note

Works only on the following platforms: iOS, Android and macOS.

on_scrollclass-attributeinstance-attribute​

on_scroll: EventHandler[WebViewScrollEvent] | None = None

Fires when the web page's scroll position changes.

Note

Works only on the following platforms: iOS and Android.

on_url_changeclass-attributeinstance-attribute​

on_url_change: ControlEventHandler[WebView] | None = None

Fires when the URL of the webview page is changed.

The data property of the event handler argument is of type str and contains the new URL.

Note

Works only on the following platforms: iOS, Android and macOS.

on_web_resource_errorclass-attributeinstance-attribute​

on_web_resource_error: (
    ControlEventHandler[WebView] | None
) = None

Fires when there is error with loading a webview page resource.

The data property of the event handler argument is of type str and contains the error message.

Note

Works only on the following platforms: iOS, Android and macOS.

Methods​

can_go_backasync​

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_forwardasync​

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_cacheasync​

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_storageasync​

clear_local_storage()

Clears the local storage used by the WebView.

Note

Works only on the following platforms: iOS, Android, and macOS.

disable_zoomasync​

disable_zoom()

Disables zooming using the on-screen zoom controls and gestures.

Note

Works only on the following platforms: iOS, Android, and macOS.

enable_zoomasync​

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_urlasync​

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_titleasync​

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_agentasync​

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_backasync​

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_forwardasync​

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_fileasync​

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_htmlasync​

load_html(
    value: str, base_url: str | None = None
) -> Self

Loads the provided HTML string.

Note

Works only on the following platforms: iOS, Android, and macOS.

Parameters:

  • value (str) - The HTML string to load.
  • base_url (str | None, default: None) - The base URL to use when resolving relative URLs within the value. May be a file:// URL, in which case the referenced local files are made readable to the webview.

load_requestasync​

load_request(
    url: str, method: RequestMethod = RequestMethod.GET
)

Makes an HTTP request and loads the response in the webview.

Parameters:

  • url (str) - The URL to load. A file:// URL is loaded as a local file, the same way load_file does.
  • method (RequestMethod, default: RequestMethod.GET) - The HTTP method to use. Ignored for file:// URLs.
Note

Works only on the following platforms: iOS, Android, and macOS.

reloadasync​

reload()

Reloads the current URL.

Note

Works only on the following platforms: iOS, Android, and macOS.

run_javascriptasync​

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_byasync​

scroll_by(x: int, y: int)

Scrolls by the provided number of webview pixels.

Note

Works only on the following platforms: iOS, Android, and macOS.

Parameters:

  • x (int) - The number of pixels to scroll by on the x-axis.
  • y (int) - The number of pixels to scroll by on the y-axis.

scroll_toasync​

scroll_to(x: int, y: int)

Scrolls to the provided position of webview pixels.

Note

Works only on the following platforms: iOS, Android, and macOS.

Parameters:

  • x (int) - The x-coordinate of the scroll position.
  • y (int) - The y-coordinate of the scroll position.

set_javascript_modeasync​

set_javascript_mode(mode: JavaScriptMode)

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.
  • Defaults to JavaScriptMode.UNRESTRICTED, which is applied before the first page load.

Parameters: