1
+ """
2
+ PyGWalker + Reflex integration demo.
3
+
4
+ This demo shows how to integrate PyGWalker with Reflex, a Python web framework.
5
+
6
+ To run this demo:
7
+ 1. Make sure you have installed PyGWalker with the Reflex plugin:
8
+ pip install "pygwalker[reflex]"
9
+
10
+ 2. Navigate to this directory:
11
+ cd examples/reflex_demo
12
+
13
+ 3. Run the app:
14
+ reflex run
15
+ """
16
+
17
+ import os
18
+ import sys
19
+ import traceback
20
+ import pandas as pd
21
+ import reflex as rx
22
+
23
+ # Add the project root directory to sys.path to ensure pygwalker can be imported
24
+ project_root = os .path .abspath (os .path .join (os .path .dirname (__file__ ), '../../..' ))
25
+ if project_root not in sys .path :
26
+ sys .path .insert (0 , project_root )
27
+
28
+ # Try to import PyGWalker Reflex components, fall back to basic demo if not available
29
+ try :
30
+ from pygwalker .api .reflex import get_component
31
+ from pygwalker .communications .reflex_comm import register_pygwalker_api
32
+ PYGWALKER_AVAILABLE = True
33
+ print ("✅ PyGWalker Reflex integration is available!" )
34
+ except Exception as e :
35
+ print (f"⚠️ PyGWalker Reflex integration not available: { e } " )
36
+ print ("Running in fallback mode with basic Reflex demo." )
37
+ PYGWALKER_AVAILABLE = False
38
+ get_component = None
39
+ register_pygwalker_api = None
40
+
41
+
42
+ class State (rx .State ):
43
+ """The app state."""
44
+ pass
45
+
46
+
47
+ def index () -> rx .Component :
48
+ """PyGWalker + Reflex demo page."""
49
+ if PYGWALKER_AVAILABLE :
50
+ return pygwalker_demo ()
51
+ else :
52
+ return fallback_demo ()
53
+
54
+
55
+ def pygwalker_demo () -> rx .Component :
56
+ """PyGWalker integration demo."""
57
+ try :
58
+ # Use local data to avoid network issues
59
+ df = pd .DataFrame ({
60
+ 'Date' : pd .date_range ('2023-01-01' , periods = 100 ),
61
+ 'Temperature' : [20 + 10 * (i % 10 ) / 10 for i in range (100 )],
62
+ 'Humidity' : [50 + 30 * (i % 7 ) / 7 for i in range (100 )],
63
+ 'City' : ['New York' , 'London' , 'Tokyo' , 'Paris' , 'Sydney' ] * 20
64
+ })
65
+
66
+ # Create a PyGWalker component
67
+ pyg_component = get_component (
68
+ df ,
69
+ theme_key = "g2" ,
70
+ appearance = "media" ,
71
+ default_tab = "vis"
72
+ )
73
+
74
+ return rx .vstack (
75
+ rx .heading ("Use Pygwalker In Reflex" , size = "3" ),
76
+ rx .text ("This demo shows PyGWalker integrated with Reflex framework." ),
77
+ rx .text ("✅ PyGWalker components with full API integration!" , color = "green" ),
78
+ pyg_component ,
79
+ spacing = "4" ,
80
+ width = "100%" ,
81
+ align_items = "stretch" ,
82
+ )
83
+ except Exception as e :
84
+ print (f"Error in pygwalker_demo: { e } " )
85
+ print (traceback .format_exc ())
86
+ return fallback_demo_with_error (str (e ))
87
+
88
+
89
+ def fallback_demo () -> rx .Component :
90
+ """Fallback demo when PyGWalker is not available."""
91
+ # Sample data
92
+ df = pd .DataFrame ({
93
+ 'Date' : pd .date_range ('2023-01-01' , periods = 10 ),
94
+ 'Temperature' : [20 , 22 , 25 , 28 , 30 , 27 , 24 , 21 , 19 , 23 ],
95
+ 'Humidity' : [45 , 50 , 55 , 60 , 65 , 58 , 52 , 48 , 44 , 49 ],
96
+ 'City' : ['New York' ] * 10
97
+ })
98
+
99
+ return rx .vstack (
100
+ rx .heading ("PyGWalker + Reflex Demo" , size = "3" ),
101
+ rx .text ("PyGWalker Reflex integration is not available." , color = "orange" ),
102
+ rx .text ("Install it with: pip install 'pygwalker[reflex]'" , font_family = "monospace" ),
103
+
104
+ rx .heading ("Sample Data" , size = "2" , margin_top = "20px" ),
105
+ rx .text (f"Data shape: { df .shape } " ),
106
+
107
+ rx .box (
108
+ rx .text ("Temperature Data:" , font_weight = "bold" ),
109
+ rx .text (f"Min: { df ['Temperature' ].min ()} °C, Max: { df ['Temperature' ].max ()} °C" ),
110
+ rx .text ("Humidity Data:" , font_weight = "bold" ),
111
+ rx .text (f"Min: { df ['Humidity' ].min ()} %, Max: { df ['Humidity' ].max ()} %" ),
112
+ padding = "15px" ,
113
+ border = "1px solid #ccc" ,
114
+ border_radius = "8px" ,
115
+ margin_top = "10px" ,
116
+ ),
117
+
118
+ rx .text (
119
+ "This demo shows the basic Reflex setup working. "
120
+ "When PyGWalker Reflex integration is available, "
121
+ "this will display an interactive data visualization component." ,
122
+ margin_top = "20px" ,
123
+ font_style = "italic"
124
+ ),
125
+
126
+ spacing = "4" ,
127
+ width = "100%" ,
128
+ align_items = "stretch" ,
129
+ padding = "20px" ,
130
+ )
131
+
132
+
133
+ def fallback_demo_with_error (error_msg : str ) -> rx .Component :
134
+ """Fallback demo when PyGWalker components fail."""
135
+ return rx .vstack (
136
+ rx .heading ("PyGWalker + Reflex Demo" , size = "3" ),
137
+ rx .text ("PyGWalker components encountered an error:" , color = "red" ),
138
+ rx .code (error_msg , padding = "10px" , background_color = "gray.100" ),
139
+ rx .text ("Falling back to basic demo." , color = "orange" ),
140
+
141
+ rx .text (
142
+ "This suggests there might be a compatibility issue between "
143
+ "PyGWalker and the current version of Reflex. The basic Reflex "
144
+ "framework is working correctly." ,
145
+ margin_top = "20px" ,
146
+ font_style = "italic"
147
+ ),
148
+
149
+ spacing = "4" ,
150
+ width = "100%" ,
151
+ align_items = "stretch" ,
152
+ padding = "20px" ,
153
+ )
154
+
155
+
156
+ # Create a Reflex app with the fixed PyGWalker API transformer
157
+ try :
158
+ if PYGWALKER_AVAILABLE :
159
+ try :
160
+ app = rx .App (api_transformer = register_pygwalker_api )
161
+ print ("✅ Reflex app created with PyGWalker API transformer!" )
162
+ except Exception as e :
163
+ print (f"⚠️ PyGWalker API transformer failed: { e } " )
164
+ print ("Creating basic Reflex app without transformer..." )
165
+ app = rx .App ()
166
+ print ("✅ Reflex app created without PyGWalker transformer!" )
167
+ else :
168
+ app = rx .App ()
169
+ print ("✅ Basic Reflex app created!" )
170
+
171
+ app .add_page (index )
172
+ print ("✅ Pages added successfully!" )
173
+
174
+ except Exception as e :
175
+ print (f"❌ Error creating Reflex app: { e } " )
176
+ print (traceback .format_exc ())
177
+ raise
0 commit comments