-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbackend.py
More file actions
531 lines (427 loc) · 18.1 KB
/
backend.py
File metadata and controls
531 lines (427 loc) · 18.1 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import os
import json
from flask import Flask, request, jsonify
from flask_cors import CORS
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly
import json as plotly_json
import inspect
import io
from contextlib import redirect_stdout, redirect_stderr
from pathview.convert_to_python import convert_graph_to_python
from pathview.pathsim_utils import make_pathsim_model, map_str_to_object
from pathview.custom_pathsim_blocks import Table1D
from pathsim.blocks import Scope, Spectrum
# Sphinx imports for docstring processing
from docutils.core import publish_parts
# imports for logging progress
from flask import Response, stream_with_context
import time
import logging
from queue import Queue, Empty
def docstring_to_html(docstring):
"""Convert a Python docstring to HTML using docutils (like Sphinx does)."""
if not docstring:
return "<p>No documentation available.</p>"
try:
# Use docutils to convert reStructuredText to HTML
# This is similar to what Sphinx does internally
overrides = {
"input_encoding": "utf-8",
"doctitle_xform": False,
"initial_header_level": 2,
}
parts = publish_parts(
source=docstring, writer_name="html", settings_overrides=overrides
)
# Return just the body content (without full HTML document structure)
html_content = parts["body"]
# Clean up the HTML a bit for better display in the sidebar
html_content = html_content.replace('<div class="document">', "<div>")
return html_content
except Exception as e:
# Fallback in case of any parsing errors
import html
escaped = html.escape(docstring)
return f"<pre>Error parsing docstring: {str(e)}\n\n{escaped}</pre>"
# Configure Flask app for Cloud Run
app = Flask(__name__, static_folder="../dist", static_url_path="")
# Configure CORS based on environment
if os.getenv("FLASK_ENV") == "production":
# Production: Allow Cloud Run domains and common domains
CORS(
app,
resources={
r"/*": {
"origins": ["*"], # Allow all origins for Cloud Run
"methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
"allow_headers": ["Content-Type", "Authorization"],
}
},
)
else:
# Development: Only allow localhost
CORS(
app,
resources={
r"/*": {"origins": ["http://localhost:5173", "http://localhost:3000"]}
},
supports_credentials=True,
)
### for capturing logs from pathsim
@app.get("/logs/stream")
def logs_stream():
def gen():
yield "retry: 500\n\n"
while True:
line = log_queue.get()
for chunk in line.replace("\r", "\n").splitlines():
yield f"data: {chunk}\n\n"
return Response(gen(), mimetype="text/event-stream")
log_queue = Queue()
class QueueHandler(logging.Handler):
def emit(self, record):
try:
msg = self.format(record)
log_queue.put_nowait(msg)
except Exception:
pass
qhandler = QueueHandler()
qhandler.setLevel(logging.INFO)
qhandler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
root = logging.getLogger()
root.setLevel(logging.INFO)
root.addHandler(qhandler)
### log backend ends
# Serve React frontend for production
@app.route("/")
def serve_frontend():
"""Serve the React frontend in production."""
if os.getenv("FLASK_ENV") == "production":
return app.send_static_file("index.html")
else:
return jsonify({"message": "PathView API", "status": "running"})
# Health check endpoint for Cloud Run
@app.route("/health", methods=["GET"])
def health_check():
return jsonify({"status": "healthy", "message": "PathView Backend is running"}), 200
# Version information endpoint
@app.route("/version", methods=["GET"])
def get_version():
try:
# Get pathsim version
import pathsim
pathsim_version = getattr(pathsim, "__version__", "Unknown")
import pathview
pathview_version = getattr(pathview, "__version__", "Unknown")
return jsonify(
{
"pathsim_version": pathsim_version,
"pathview_version": pathview_version,
"status": "success",
}
), 200
except Exception as e:
return jsonify(
{
"pathsim_version": "Unknown",
"pathview_version": "Unknown",
"status": "error",
"error": str(e),
}
), 200
@app.route("/default-values-all", methods=["GET"])
def get_all_default_values():
try:
all_default_values = {}
for node_type, block_class in map_str_to_object.items():
parameters_for_class = inspect.signature(block_class.__init__).parameters
default_values = {}
for param in parameters_for_class:
if param != "self": # Skip 'self' parameter
default_value = parameters_for_class[param].default
if default_value is inspect._empty:
default_values[param] = None # Handle empty defaults
else:
default_values[param] = default_value
# check if default value is serializable to JSON
if not isinstance(
default_value, (int, float, str, bool, list, dict)
):
# Attempt to convert to JSON serializable type
try:
default_values[param] = json.dumps(default_value)
except TypeError:
# If conversion fails, set to a string 'default'
default_values[param] = "default"
all_default_values[node_type] = default_values
return jsonify(all_default_values)
except Exception as e:
return jsonify({"error": f"Could not get all default values: {str(e)}"}), 400
# returns default values for parameters of a node
@app.route("/default-values/<string:node_type>", methods=["GET"])
def get_default_values(node_type):
try:
if node_type not in map_str_to_object:
return jsonify({"error": f"Unknown node type: {node_type}"}), 400
block_class = map_str_to_object[node_type]
parameters_for_class = inspect.signature(block_class.__init__).parameters
default_values = {}
for param in parameters_for_class:
if param != "self": # Skip 'self' parameter
default_value = parameters_for_class[param].default
if default_value is inspect._empty:
default_values[param] = None # Handle empty defaults
else:
default_values[param] = default_value
# check if default value is serializable to JSON
if not isinstance(
default_value, (int, float, str, bool, list, dict)
):
# Attempt to convert to JSON serializable type
try:
default_values[param] = json.dumps(default_value)
except TypeError:
# If conversion fails, set to a string 'default'
default_values[param] = "default"
return jsonify(default_values)
except Exception as e:
return jsonify(
{"error": f"Could not get default values for {node_type}: {str(e)}"}
), 400
@app.route("/get-all-docs", methods=["GET"])
def get_all_docs():
try:
all_docs = {}
for node_type, block_class in map_str_to_object.items():
docstring = inspect.getdoc(block_class)
# If no docstring, provide a basic description
if not docstring:
docstring = f"No documentation available for {node_type}."
# Convert docstring to HTML using docutils/Sphinx-style processing
html_content = docstring_to_html(docstring)
all_docs[node_type] = {
"docstring": docstring, # Keep original for backwards compatibility
"html": html_content, # New HTML version
}
return jsonify(all_docs)
except Exception as e:
return jsonify({"error": f"Could not get docs for all nodes: {str(e)}"}), 400
@app.route("/get-docs/<string:node_type>", methods=["GET"])
def get_docs(node_type):
try:
if node_type not in map_str_to_object:
return jsonify({"error": f"Unknown node type: {node_type}"}), 400
block_class = map_str_to_object[node_type]
docstring = inspect.getdoc(block_class)
# If no docstring, provide a basic description
if not docstring:
docstring = f"No documentation available for {node_type}."
# Convert docstring to HTML using docutils/Sphinx-style processing
html_content = docstring_to_html(docstring)
return jsonify(
{
"docstring": docstring, # Keep original for backwards compatibility
"html": html_content, # New HTML version
}
)
except Exception as e:
return jsonify({"error": f"Could not get docs for {node_type}: {str(e)}"}), 400
# Function to convert graph to Python script
@app.route("/convert-to-python", methods=["POST"])
def convert_to_python():
try:
data = request.json
graph_data = data.get("graph")
if not graph_data:
return jsonify({"error": "No graph data provided"}), 400
# Generate the Python script directly using the imported function
script_content = convert_graph_to_python(graph_data)
return jsonify(
{
"success": True,
"script": script_content,
"message": "Python script generated successfully",
}
)
except Exception as e:
return jsonify({"success": False, "error": f"Server error: {str(e)}"}), 500
# Helper function to extract CSV payload from scopes
def make_csv_payload(scopes):
csv_payload = {"time": [], "series": {}}
max_len = 0
for scope in scopes:
time, values = scope.read()
max_len = max(max_len, len(time))
csv_payload["time"] = time.tolist()
for i, series in enumerate(values):
label = scope.labels[i] if i < len(scope.labels) else f"{scope.label} {i}"
csv_payload["series"][label] = series.tolist()
return csv_payload
# Function to convert graph to pathsim and run simulation
@app.route("/run-pathsim", methods=["POST"])
def run_pathsim():
try:
data = request.json
graph_data = data.get("graph")
if not graph_data:
return jsonify({"error": "No graph data provided"}), 400
my_simulation, duration = make_pathsim_model(graph_data)
# get the pathsim logger and add the queue handler
logger = my_simulation.logger
logger.addHandler(qhandler)
# Run the simulation
my_simulation.run(duration)
# Generate the plot
scopes = [block for block in my_simulation.blocks if isinstance(block, Scope)]
spectra = [
block for block in my_simulation.blocks if isinstance(block, Spectrum)
]
# FIXME right now only the scopes are converted to CSV
# extra work is needed since spectra and scopes don't share the same x axis
csv_payload = make_csv_payload(scopes)
# Share x only if there are only scopes or only spectra
shared_x = len(scopes) * len(spectra) == 0
n_rows = len(scopes) + len(spectra)
absolute_vertical_spacing = 0.05
relative_vertical_spacing = absolute_vertical_spacing / n_rows
fig = make_subplots(
rows=n_rows,
cols=1,
shared_xaxes=shared_x,
subplot_titles=[scope.label for scope in scopes]
+ [spec.label for spec in spectra],
vertical_spacing=relative_vertical_spacing,
)
# make scope plots
for i, scope in enumerate(scopes):
time, data = scope.read()
for p, d in enumerate(data):
lb = scope.labels[p] if p < len(scope.labels) else f"port {p}"
fig.add_trace(
go.Scatter(x=time, y=d, mode="lines", name=lb), row=i + 1, col=1
)
fig.update_xaxes(title_text="Time", row=len(scopes), col=1)
# make Table1D plots
for b in my_simulation.blocks:
# if it's a Table1d, check if it's connected to a scope
if isinstance(b, Table1D):
for c in my_simulation.connections:
if b in c.get_blocks():
for s in scopes:
if s in c.get_blocks():
# if connected to a scope, add a vertical line at each table point
time_points = b.points
values = b.values
# add scatter points
fig.add_trace(
go.Scatter(
x=time_points,
y=values,
mode="markers",
name=f"{b.label} points",
marker=dict(symbol="x", size=10),
),
row=scopes.index(s) + 1,
col=1,
)
# make spectrum plots
for i, spec in enumerate(spectra):
time, data = spec.read()
for p, d in enumerate(data):
lb = spec.labels[p] if p < len(spec.labels) else f"port {p}"
d = abs(d)
fig.add_trace(
go.Scatter(x=time, y=d, mode="lines", name=lb),
row=len(scopes) + i + 1,
col=1,
)
fig.update_xaxes(title_text="Frequency", row=len(scopes) + i + 1, col=1)
fig.update_layout(
height=500 * (len(scopes) + len(spectra)), hovermode="x unified"
)
# Convert plot to JSON
plot_data = plotly_json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
plot_html = fig.to_html()
return jsonify(
{
"success": True,
"plot": plot_data,
"html": plot_html,
"csv_data": csv_payload,
"message": "Pathsim simulation completed successfully",
}
)
except Exception as e:
return jsonify({"success": False, "error": f"Server error: {str(e)}"}), 500
@app.route("/execute-python", methods=["POST"])
def execute_python():
"""Execute Python code and returns variables/functions."""
try:
data = request.json
code = data.get("code", "")
if not code.strip():
return jsonify({"success": False, "error": "No code provided"}), 400
# Create a temporary namespace that includes current eval_namespace
temp_namespace = {}
# temp_namespace.update(globals())
# Capture stdout and stderr
stdout_capture = io.StringIO()
stderr_capture = io.StringIO()
try:
with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
exec(code, temp_namespace)
# Capture any output
output = stdout_capture.getvalue()
error_output = stderr_capture.getvalue()
if error_output:
return jsonify({"success": False, "error": error_output}), 400
# Find new variables and functions
vars = set(temp_namespace.keys())
# new_vars = vars_after - vars_before
# Filter out built-ins and modules, keep user-defined items
user_variables = {}
user_functions = []
for var_name in vars:
if not var_name.startswith("__"):
value = temp_namespace[var_name]
if callable(value) and hasattr(value, "__name__"):
user_functions.append(var_name)
else:
# Try to serialize the value for display
try:
if isinstance(value, (int, float, str, bool, list, dict)):
user_variables[var_name] = value
else:
user_variables[var_name] = str(value)
except Exception:
user_variables[var_name] = (
f"<{type(value).__name__} object>"
)
return jsonify(
{
"success": True,
"output": output if output else None,
"variables": user_variables,
"functions": user_functions,
"message": f"Executed successfully. Added {len(user_variables)} variables and {len(user_functions)} functions to namespace.",
}
)
except SyntaxError as e:
return jsonify({"success": False, "error": f"Syntax Error: {str(e)}"}), 400
except Exception as e:
return jsonify({"success": False, "error": f"Runtime Error: {str(e)}"}), 400
except Exception as e:
return jsonify({"success": False, "error": f"Server error: {str(e)}"}), 500
# Catch-all route for React Router (SPA routing)
@app.route("/<path:path>")
def catch_all(path):
"""Serve React app for all routes in production (for client-side routing)."""
if os.getenv("FLASK_ENV") == "production":
return app.send_static_file("index.html")
else:
return jsonify({"error": "Route not found"}), 404
if __name__ == "__main__":
port = int(os.getenv("PORT", 8000))
app.run(host="0.0.0.0", port=port, debug=os.getenv("FLASK_ENV") != "production")