File size: 547 Bytes
dacc0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import sys
import contextlib

async def run_pandas_script(pandas_script , df) : 
    
    captured_output = None
    
    string_io = io.StringIO()
    
    try : 

        with contextlib.redirect_stdout(string_io) : exec(pandas_script , {'df' : df})

        captured_output = string_io.getvalue()

    except Exception as e : 

        print(f'Error executing the generated script:\n{e}')

        captured_output = f'Error: {e}'
    
    finally : string_io.close()
    
    print(captured_output)
        
    return captured_output