Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import sys
|
| 3 |
+
import subprocess
|
| 4 |
+
import importlib
|
| 5 |
+
import io
|
| 6 |
+
import contextlib
|
| 7 |
+
from pygments import highlight
|
| 8 |
+
from pygments.lexers import PythonLexer
|
| 9 |
+
from pygments.formatters import HtmlFormatter
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
import numpy as np
|
| 12 |
+
import pandas as pd
|
| 13 |
+
|
| 14 |
+
# Function to install packages dynamically
|
| 15 |
+
def install_package(package):
|
| 16 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 17 |
+
|
| 18 |
+
# Function to execute user code with output capturing and visualization support
|
| 19 |
+
def execute_code(code, libraries):
|
| 20 |
+
# Install libraries
|
| 21 |
+
for library in libraries.splitlines():
|
| 22 |
+
if library.strip():
|
| 23 |
+
install_package(library.strip())
|
| 24 |
+
|
| 25 |
+
# Prepare output capturing
|
| 26 |
+
output = io.StringIO()
|
| 27 |
+
error_output = io.StringIO()
|
| 28 |
+
|
| 29 |
+
# Execute the code
|
| 30 |
+
try:
|
| 31 |
+
with contextlib.redirect_stdout(output), contextlib.redirect_stderr(error_output):
|
| 32 |
+
exec_globals = {
|
| 33 |
+
'plt': plt,
|
| 34 |
+
'np': np,
|
| 35 |
+
'pd': pd,
|
| 36 |
+
}
|
| 37 |
+
exec(code, exec_globals)
|
| 38 |
+
|
| 39 |
+
# Check if a plot was created
|
| 40 |
+
if plt.get_fignums():
|
| 41 |
+
plt.savefig('plot.png')
|
| 42 |
+
plt.close()
|
| 43 |
+
return output.getvalue(), error_output.getvalue(), 'plot.png'
|
| 44 |
+
else:
|
| 45 |
+
return output.getvalue(), error_output.getvalue(), None
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return "", str(e), None
|
| 48 |
+
|
| 49 |
+
# Function to highlight Python code
|
| 50 |
+
def highlight_code(code):
|
| 51 |
+
return highlight(code, PythonLexer(), HtmlFormatter(style="monokai"))
|
| 52 |
+
|
| 53 |
+
# Define the Gradio interface
|
| 54 |
+
with gr.Blocks(css=".gradio-container {background-color: #f0f0f0;}") as demo:
|
| 55 |
+
gr.Markdown("# Advanced Python Code Executor")
|
| 56 |
+
|
| 57 |
+
with gr.Tabs():
|
| 58 |
+
with gr.TabItem("Code Execution"):
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column(scale=2):
|
| 61 |
+
code_input = gr.Code(label="Python Code", language="python", lines=10)
|
| 62 |
+
libraries_input = gr.TextArea(label="Libraries (one per line)", placeholder="e.g.\nrequests\nscipy", lines=3)
|
| 63 |
+
execute_button = gr.Button("Run Code", variant="primary")
|
| 64 |
+
with gr.Column(scale=1):
|
| 65 |
+
output = gr.Textbox(label="Standard Output", lines=10)
|
| 66 |
+
error_output = gr.Textbox(label="Error Output", lines=5)
|
| 67 |
+
plot_output = gr.Image(label="Plot Output")
|
| 68 |
+
|
| 69 |
+
with gr.TabItem("Code Highlighting"):
|
| 70 |
+
code_to_highlight = gr.Code(label="Enter Python code to highlight", language="python", lines=10)
|
| 71 |
+
highlight_button = gr.Button("Highlight Code")
|
| 72 |
+
highlighted_code = gr.HTML(label="Highlighted Code")
|
| 73 |
+
|
| 74 |
+
execute_button.click(
|
| 75 |
+
fn=execute_code,
|
| 76 |
+
inputs=[code_input, libraries_input],
|
| 77 |
+
outputs=[output, error_output, plot_output]
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
highlight_button.click(
|
| 81 |
+
fn=highlight_code,
|
| 82 |
+
inputs=[code_to_highlight],
|
| 83 |
+
outputs=[highlighted_code]
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
gr.Markdown("""
|
| 87 |
+
## Features:
|
| 88 |
+
- Execute Python code with custom library imports
|
| 89 |
+
- Capture and display standard output and error messages separately
|
| 90 |
+
- Support for data visualization with matplotlib
|
| 91 |
+
- Code highlighting for better readability
|
| 92 |
+
- Tab-based interface for different functionalities
|
| 93 |
+
|
| 94 |
+
## Tips:
|
| 95 |
+
- Use `plt` for matplotlib, `np` for numpy, and `pd` for pandas (pre-imported)
|
| 96 |
+
- Your plot will automatically be displayed if you use `plt.plot()` or similar functions
|
| 97 |
+
""")
|
| 98 |
+
|
| 99 |
+
# Launch the interface
|
| 100 |
+
demo.launch()
|