Spaces:
Sleeping
Sleeping
Update code_df_custom.py
Browse files- code_df_custom.py +10 -20
code_df_custom.py
CHANGED
|
@@ -6,29 +6,19 @@ def load_excel(file):
|
|
| 6 |
return file, df
|
| 7 |
|
| 8 |
def run_code(file, code):
|
| 9 |
-
|
| 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 |
# def run_code_and_update_ui(file, code):
|
| 33 |
# df, error_msg = run_code(file, code) # This is your updated run_code function.
|
| 34 |
|
|
|
|
| 6 |
return file, df
|
| 7 |
|
| 8 |
def run_code(file, code):
|
| 9 |
+
if not file:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
print("No file provided")
|
| 11 |
return pd.DataFrame(), "No file provided"
|
| 12 |
|
| 13 |
+
try:
|
| 14 |
+
df = pd.read_excel(file)
|
| 15 |
+
exec_globals = {'pd': pd, 'df': df}
|
| 16 |
+
exec(code, exec_globals)
|
| 17 |
+
new_df = exec_globals.get('df', df)
|
| 18 |
+
return new_df, ""
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return df, f"# ERROR: {str(e)}"
|
| 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 |
|