Spaces:
Sleeping
Sleeping
Update code_df_custom.py
Browse files- code_df_custom.py +17 -21
code_df_custom.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
-
import traceback
|
| 3 |
|
| 4 |
|
| 5 |
def load_excel(file):
|
|
@@ -14,40 +13,37 @@ def run_code(file, code):
|
|
| 14 |
|
| 15 |
scope['df'] = df
|
| 16 |
try:
|
| 17 |
-
# Attempt to execute the user's code
|
| 18 |
exec(code, scope, scope)
|
| 19 |
except Exception as e:
|
| 20 |
-
# Catch any exceptions that occur and print the error message
|
| 21 |
-
error_msg = traceback.format_exc()
|
| 22 |
print(f"Error executing user code: {error_msg}")
|
| 23 |
-
scope['new_df'] = df
|
| 24 |
-
return scope['new_df'],
|
| 25 |
|
| 26 |
-
print(scope.keys())
|
| 27 |
if 'new_df' not in scope:
|
| 28 |
print("new_df not defined")
|
| 29 |
scope['new_df'] = df.copy()
|
| 30 |
new_df = scope['new_df']
|
| 31 |
|
| 32 |
-
return new_df,
|
| 33 |
else:
|
| 34 |
print("No file provided")
|
| 35 |
return pd.DataFrame(), "No file provided"
|
| 36 |
|
| 37 |
-
def run_code_and_update_ui(file, code):
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
|
| 53 |
def export_df(df, filename):
|
|
|
|
| 1 |
import pandas as pd
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
def load_excel(file):
|
|
|
|
| 13 |
|
| 14 |
scope['df'] = df
|
| 15 |
try:
|
|
|
|
| 16 |
exec(code, scope, scope)
|
| 17 |
except Exception as e:
|
|
|
|
|
|
|
| 18 |
print(f"Error executing user code: {error_msg}")
|
| 19 |
+
scope['new_df'] = df
|
| 20 |
+
return scope['new_df'], e
|
| 21 |
|
| 22 |
+
# print(scope.keys())
|
| 23 |
if 'new_df' not in scope:
|
| 24 |
print("new_df not defined")
|
| 25 |
scope['new_df'] = df.copy()
|
| 26 |
new_df = scope['new_df']
|
| 27 |
|
| 28 |
+
return new_df, ""
|
| 29 |
else:
|
| 30 |
print("No file provided")
|
| 31 |
return pd.DataFrame(), "No file provided"
|
| 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 |
|
| 36 |
+
# # Now, check if there's an error message and handle it appropriately.
|
| 37 |
+
# if error_msg:
|
| 38 |
+
# # You can update some error display component in Gradio here to show the error_msg.
|
| 39 |
+
# # Assuming you have a gr.Textbox to display errors:
|
| 40 |
+
# error_display.update(value=f"Error in code execution: {error_msg}")
|
| 41 |
+
# # Ensure you still return the original DataFrame or an empty one to satisfy the expected output type.
|
| 42 |
+
# return df
|
| 43 |
+
# else:
|
| 44 |
+
# # If no error, clear the error display and return the DataFrame as usual.
|
| 45 |
+
# error_display.update(value="")
|
| 46 |
+
# return df
|
| 47 |
|
| 48 |
|
| 49 |
def export_df(df, filename):
|