Spaces:
Sleeping
Sleeping
Update code_df_custom.py
Browse files- code_df_custom.py +21 -10
code_df_custom.py
CHANGED
@@ -6,19 +6,30 @@ def load_excel(file):
|
|
6 |
return file, df
|
7 |
|
8 |
def run_code(file, code):
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
try:
|
14 |
df = pd.read_excel(file)
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
return new_df, ""
|
19 |
-
|
20 |
-
|
|
|
21 |
|
|
|
22 |
# def run_code_and_update_ui(file, code):
|
23 |
# df, error_msg = run_code(file, code) # This is your updated run_code function.
|
24 |
|
|
|
6 |
return file, df
|
7 |
|
8 |
def run_code(file, code):
|
9 |
+
scope = {'pd': pd}
|
10 |
+
if file:
|
11 |
+
print('file ok')
|
|
|
|
|
12 |
df = pd.read_excel(file)
|
13 |
+
|
14 |
+
scope['df'] = df
|
15 |
+
try:
|
16 |
+
exec(code, scope, scope)
|
17 |
+
except Exception as e:
|
18 |
+
scope['new_df'] = df
|
19 |
+
return scope['new_df'], f"# ERROR: {str(e)}"
|
20 |
+
|
21 |
+
# print(scope.keys())
|
22 |
+
if 'new_df' not in scope:
|
23 |
+
print("new_df not defined")
|
24 |
+
scope['new_df'] = df.copy()
|
25 |
+
new_df = scope['new_df']
|
26 |
+
|
27 |
return new_df, ""
|
28 |
+
else:
|
29 |
+
print("No file provided")
|
30 |
+
return pd.DataFrame(), "No file provided"
|
31 |
|
32 |
+
|
33 |
# def run_code_and_update_ui(file, code):
|
34 |
# df, error_msg = run_code(file, code) # This is your updated run_code function.
|
35 |
|