UrlLauncher
Inherits: Service
Methods
-
can_launch_url–Checks whether the specified URL can be handled by some app
-
close_in_app_web_view–Closes the in-app web view if it is currently open.
-
launch_url–Opens a web browser or in-app view to a given
url. -
open_window–Opens a popup browser window in web environments.
-
supports_close_for_launch_mode–Checks whether
close_in_app_web_viewis supported for a launch mode. -
supports_launch_mode–Checks whether the specified launch mode is supported.
Examples#
Basic Example#
import flet as ft
async def main(page: ft.Page):
url_launcher = ft.UrlLauncher()
url = ft.TextField(label="URL to open", value="https://flet.dev", expand=True)
status = ft.Text()
async def can_launch():
can = await url_launcher.can_launch_url(url.value)
status.value = f"Can launch: {can}"
async def launch_default():
await url_launcher.launch_url(url.value)
async def launch_in_app_webview():
await url_launcher.launch_url(
url.value,
mode=ft.LaunchMode.IN_APP_WEB_VIEW,
web_view_configuration=ft.WebViewConfiguration(
enable_javascript=True, enable_dom_storage=True
),
)
async def launch_in_app_browser_view():
await url_launcher.launch_url(
url.value,
mode=ft.LaunchMode.IN_APP_BROWSER_VIEW,
browser_configuration=ft.BrowserConfiguration(show_title=True),
)
async def launch_external():
await url_launcher.launch_url(
url.value,
mode=ft.LaunchMode.EXTERNAL_APPLICATION,
web_only_window_name="_blank",
)
async def launch_popup():
await url_launcher.open_window(
url.value, title="Flet popup", width=480, height=640
)
async def close_webview(_):
supported = await url_launcher.supports_close_for_launch_mode(
ft.LaunchMode.IN_APP_WEB_VIEW
)
if supported:
await url_launcher.close_in_app_web_view()
else:
status.value = "Close in-app web view not supported on this platform"
page.add(
ft.Column(
[
url,
ft.Row(
[
ft.Button("Launch URL", on_click=launch_default),
ft.Button(
"Launch in-app webview", on_click=launch_in_app_webview
),
ft.Button(
"Launch in-app browser view",
on_click=launch_in_app_browser_view,
),
ft.Button("Launch external/new tab", on_click=launch_external),
ft.Button("Open popup window (web)", on_click=launch_popup),
ft.Button("Can launch?", on_click=can_launch),
ft.Button("Close in-app webview", on_click=close_webview),
],
wrap=True,
),
status,
],
)
)
ft.run(main)
Methods#
can_launch_url
async
#
Checks whether the specified URL can be handled by some app installed on the device.
Parameters:
Returns:
-
bool–Trueif it is possible to verify that there is a handler available.Falseif there is no handler available, or the application does not have permission to check. For example:- On recent versions of Android and iOS, this will always return
Falseunless the application has been configuration to allow querying the system for launch support. - On web, this will always return
Falseexcept for a few specific schemes that are always assumed to be supported (such as http(s)), as web pages are never allowed to query installed applications.
- On recent versions of Android and iOS, this will always return
close_in_app_web_view
async
#
Closes the in-app web view if it is currently open.
launch_url
async
#
launch_url(
url: str | Url,
*,
mode: LaunchMode = PLATFORM_DEFAULT,
web_view_configuration: WebViewConfiguration
| None = None,
browser_configuration: BrowserConfiguration
| None = None,
web_only_window_name: str | None = None,
)
Opens a web browser or in-app view to a given url.
Parameters:
-
url(str | Url) –The URL to open.
-
mode(LaunchMode, default:PLATFORM_DEFAULT) –Preferred launch mode for opening the URL.
-
web_view_configuration(WebViewConfiguration | None, default:None) –Optional configuration for in-app web views.
-
browser_configuration(BrowserConfiguration | None, default:None) –Optional configuration for in-app browser views.
-
web_only_window_name(str | None, default:None) –Window name for web-only launches.
open_window
async
#
open_window(
url: str | Url,
*,
title: str | None = None,
width: float | None = None,
height: float | None = None,
)
Opens a popup browser window in web environments.
Parameters:
supports_close_for_launch_mode
async
#
supports_close_for_launch_mode(mode: LaunchMode) -> bool
Checks whether close_in_app_web_view is supported for a launch mode.
Parameters:
-
mode(LaunchMode) –Launch mode to verify close support for.
Returns:
-
bool–Trueif closing an in-app web view is supported; otherwiseFalse.
supports_launch_mode
async
#
supports_launch_mode(mode: LaunchMode) -> bool
Checks whether the specified launch mode is supported.
Parameters:
-
mode(LaunchMode) –Launch mode to verify.
Returns:
-
bool–Trueif the launch mode is supported by the platform; otherwiseFalse.