-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
77 lines (68 loc) · 2.05 KB
/
app.py
File metadata and controls
77 lines (68 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import dash
from dash import html
from dash.dependencies import Input, Output
from dash_vite_plugin import VitePlugin, NpmPackage
# Create VitePlugin instance with React support
vite_plugin = VitePlugin(
build_assets_paths=['assets/js', 'assets/react'],
entry_js_paths=['assets/js/main.js'],
npm_packages=[
NpmPackage('react'),
NpmPackage('react-dom'),
],
download_node=True,
clean_after=True,
)
# Call setup BEFORE creating Dash app (as required by the plugin architecture)
vite_plugin.setup()
# Create a Dash app
app = dash.Dash(__name__)
# Call use AFTER creating Dash app (as required by the plugin architecture)
vite_plugin.use(app)
# Define app layout with a container for React
app.layout = html.Div(
[
html.H1('Vite Plugin Test - React Support', id='header'),
html.P('This tests the Vite plugin with React support.', id='paragraph'),
# Container for React app
html.Div(
[
'The content from React',
html.Div(id='react-container'),
]
),
html.Div(
[
'The content from Dash',
html.Div(
[html.H1('Hello from Dash!', id='dash-title'), html.Button('Control React', id='dash-button')],
id='dash-app',
style={'margin': '20px'},
),
],
id='dash-container',
),
]
)
# Add callback to test React functionality with a simpler approach
app.clientside_callback(
"""
function(n_clicks) {
if (n_clicks > 0) {
const reactApp = document.getElementById('react-app');
if (reactApp) {
const button = reactApp.querySelector('#control-react-button');
if (button) {
button.click();
return 'Hello from Dash!';
}
}
}
return 'Hello from Dash!';
}
""",
Output('dash-title', 'children'),
Input('dash-button', 'n_clicks'),
)
if __name__ == '__main__':
app.run(debug=True)