Last active
June 27, 2022 13:18
-
-
Save AdamSpannbauer/67d76efa0c69026599c899f0d4655893 to your computer and use it in GitHub Desktop.
Test using PyWebView to run a Plotly Dash app as a native web app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import webview | |
from dash import Dash, html | |
def run_native_dash_app(dash_app: Dash, window_title: str = None) -> None: | |
"""Run dash app as native web app | |
Use PyWebView to run a dash app as a native web app | |
* install with `pip install pywebview` | |
* project home page: https://pywebview.flowrl.com/ | |
* github: https://github.com/r0x0r/pywebview | |
* Actively developed as of 2022-04-27 (checked on 2022-06-27) | |
:param dash_app: a dash app with server and title attributes | |
:param window_title: title for app window; use None if `dash_app.title` should be used | |
:return: None | |
>>> app = Dash('Native Dash App Demo') | |
>>> app.layout = html.Div(['App Layout']) | |
>>> run_native_dash_app(app) | |
""" | |
if window_title is None: | |
window_title = dash_app.title | |
webview.create_window(window_title, dash_app.server) | |
webview.start() | |
if __name__ == "__main__": | |
# Demo app to input text and output text in all caps | |
from dash import dcc, Input, Output | |
app = Dash("Native Dash App Demo") | |
app.layout = html.Div( | |
[ | |
html.Div(["input text: ", dcc.Input(id="my-input", type="text")]), | |
html.Div(id="my-output"), | |
] | |
) | |
@app.callback( | |
Output(component_id="my-output", component_property="children"), | |
Input(component_id="my-input", component_property="value"), | |
) | |
def update_output_div(input_value): | |
return f"{input_value.upper()}" | |
run_native_dash_app(app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment