CoolFace
Apppublic

Almaatla/Standard_Intelligence_Dev

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
code_df_custom.py53 linesDownload Raw Back to root
1import pandas as pd2 3 4def load_excel(file):5    df = pd.read_excel(file)6    return file, df7 8def 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'] = df15        try:16            exec(code, scope, scope)17        except Exception as e:18            scope['new_df'] = df19            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    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 df43#     else:44#         # If no error, clear the error display and return the DataFrame as usual.45#         error_display.update(value="")46#         return df47 48 49def export_df(df, filename):50    filename = filename.replace('.xlsx', '_coded.xlsx')51    df.to_excel(filename, index=False)52    return filename53