-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_backend.py
More file actions
301 lines (257 loc) · 9.18 KB
/
Copy pathpython_backend.py
File metadata and controls
301 lines (257 loc) · 9.18 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
from flask import Flask, request, jsonify
from flask_cors import CORS
import sys
import os
import traceback
from io import StringIO, BytesIO
import base64
import pickle
import tempfile
from contextlib import redirect_stdout
# Import commonly used ML libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats as stats
# Machine learning imports
from sklearn import (
linear_model, ensemble, tree, neighbors, svm,
neural_network, model_selection, metrics, preprocessing,
cluster, decomposition, feature_extraction, manifold
)
import xgboost
import lightgbm
import tensorflow as tf
import torch
import joblib
app = Flask(__name__)
CORS(app)
# Session storage to maintain state between cells
SESSIONS = {}
MAX_MEMORY_MB = 500 # Memory limit per session (MB)
def get_session(session_id):
"""Get or create a new session environment"""
if session_id not in SESSIONS:
SESSIONS[session_id] = {
'env': create_execution_environment(),
'history': [],
'temp_dir': tempfile.mkdtemp()
}
return SESSIONS[session_id]
def create_execution_environment():
"""Create a safe execution environment with ML libraries"""
return {
"__builtins__": __builtins__,
# Data manipulation
"np": np,
"pd": pd,
# Visualization
"plt": plt,
"sns": sns,
# Stats
"stats": stats,
# Scikit-learn
"linear_model": linear_model,
"ensemble": ensemble,
"tree": tree,
"neighbors": neighbors,
"svm": svm,
"neural_network": neural_network,
"model_selection": model_selection,
"metrics": metrics,
"preprocessing": preprocessing,
"cluster": cluster,
"decomposition": decomposition,
"feature_extraction": feature_extraction,
"manifold": manifold,
# Other ML frameworks
"xgboost": xgboost,
"lightgbm": lightgbm,
"tf": tf,
"torch": torch,
"joblib": joblib,
# Common functions
"train_test_split": model_selection.train_test_split
}
def check_memory_usage(session_id):
"""Check if session memory usage exceeds limits"""
session = SESSIONS.get(session_id)
if not session:
return False
# Estimate memory usage (not perfect but gives some protection)
session_size = 0
for var_name, var in session['env'].items():
if var_name.startswith('__'):
continue
try:
if hasattr(var, 'nbytes'):
session_size += var.nbytes
elif isinstance(var, pd.DataFrame):
session_size += var.memory_usage(deep=True).sum()
except:
pass
return session_size > (MAX_MEMORY_MB * 1024 * 1024)
def execute_python_code(code, session_id):
"""Execute Python code and maintain state between executions"""
session = get_session(session_id)
exec_globals = session['env']
# Configure matplotlib for non-interactive backend
plt.switch_backend('Agg')
plt.close('all')
# Create output buffers
output_buffer = StringIO()
# Variables to capture results
plot_data = None
df_html = None
error = None
try:
# Execute code with context
with redirect_stdout(output_buffer):
exec(code, exec_globals)
# Capture DataFrame outputs
for var_name, var in exec_globals.items():
if var_name.startswith('__') or var_name in session['env']:
continue
if isinstance(var, pd.DataFrame):
# Take the first dataframe found or largest one
if df_html is None or (var.shape[0] * var.shape[1]) > 0:
df_html = var.head(100).to_html(classes="table table-striped table-hover")
# Capture matplotlib plots
if plt.get_fignums():
buf = BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight', dpi=100)
buf.seek(0)
plot_data = base64.b64encode(buf.read()).decode('utf-8')
plt.close('all')
# Check for memory limits
if check_memory_usage(session_id):
# If exceeded, we could implement cleanup strategies here
pass
# Save execution history
session['history'].append(code)
return output_buffer.getvalue(), error, df_html, plot_data
except Exception as e:
error = f"{type(e).__name__}: {str(e)}\n{traceback.format_exc()}"
plt.close('all')
return output_buffer.getvalue(), error, None, None
finally:
output_buffer.close()
@app.route('/api/execute', methods=['POST'])
def execute_code():
"""API endpoint to execute code"""
data = request.json
cells = data.get('cells', [])
session_id = data.get('session_id', 'default')
results = []
for cell in cells:
cell_id = cell.get("id")
code = cell.get("code", "")
output, error, df_html, plot_data = execute_python_code(code, session_id)
results.append({
"cell_id": cell_id,
"output": output,
"error": error,
"table_html": df_html,
"plot": plot_data
})
return jsonify(results)
@app.route('/api/reset_session', methods=['POST'])
def reset_session():
"""Reset a specific session or all sessions"""
data = request.json
session_id = data.get('session_id')
if session_id:
if session_id in SESSIONS:
# Clean up temp files
try:
if os.path.exists(SESSIONS[session_id]['temp_dir']):
for file in os.listdir(SESSIONS[session_id]['temp_dir']):
os.remove(os.path.join(SESSIONS[session_id]['temp_dir'], file))
os.rmdir(SESSIONS[session_id]['temp_dir'])
except:
pass
# Delete session
del SESSIONS[session_id]
return jsonify({"status": "success", "message": f"Session {session_id} reset"})
return jsonify({"status": "error", "message": "Session not found"})
else:
# Reset all sessions
for sid in list(SESSIONS.keys()):
try:
if os.path.exists(SESSIONS[sid]['temp_dir']):
for file in os.listdir(SESSIONS[sid]['temp_dir']):
os.remove(os.path.join(SESSIONS[sid]['temp_dir'], file))
os.rmdir(SESSIONS[sid]['temp_dir'])
except:
pass
SESSIONS.clear()
return jsonify({"status": "success", "message": "All sessions reset"})
@app.route('/api/get_variables', methods=['POST'])
def get_variables():
"""Get variable information for the current session"""
data = request.json
session_id = data.get('session_id', 'default')
session = get_session(session_id)
variables = {}
for var_name, var in session['env'].items():
# Skip built-in and library references
if var_name.startswith('__') or var_name in create_execution_environment():
continue
var_info = {
"type": type(var).__name__
}
# Add shape for arrays and dataframes
if hasattr(var, 'shape'):
var_info["shape"] = str(var.shape)
elif isinstance(var, list):
var_info["length"] = len(var)
# Add more specific info based on type
if isinstance(var, pd.DataFrame):
var_info["columns"] = var.columns.tolist()
var_info["dtypes"] = {col: str(dtype) for col, dtype in var.dtypes.items()}
variables[var_name] = var_info
return jsonify({"variables": variables})
@app.route('/api/export_notebook', methods=['POST'])
def export_notebook():
"""Export session history as Jupyter notebook format"""
data = request.json
session_id = data.get('session_id', 'default')
session = get_session(session_id)
# Create a simple Jupyter notebook structure
notebook = {
"cells": [
{
"cell_type": "code",
"execution_count": i + 1,
"metadata": {},
"source": code.split('\n'),
"outputs": []
}
for i, code in enumerate(session['history'])
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
return jsonify(notebook)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)