Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import io
|
| 3 |
+
import sys
|
| 4 |
+
from contextlib import redirect_stdout
|
| 5 |
+
import re
|
| 6 |
+
import mistune
|
| 7 |
+
|
| 8 |
+
def extract_python_code(markdown_text):
|
| 9 |
+
"""Extract Python code blocks from markdown text."""
|
| 10 |
+
pattern = r"```python\s*(.*?)\s*```"
|
| 11 |
+
matches = re.findall(pattern, markdown_text, re.DOTALL)
|
| 12 |
+
return matches
|
| 13 |
+
|
| 14 |
+
def execute_code(code):
|
| 15 |
+
"""Execute Python code and return the output."""
|
| 16 |
+
# Create string buffer to capture output
|
| 17 |
+
buffer = io.StringIO()
|
| 18 |
+
|
| 19 |
+
# Create a dictionary for local variables
|
| 20 |
+
local_vars = {}
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
# Redirect stdout to our buffer
|
| 24 |
+
with redirect_stdout(buffer):
|
| 25 |
+
# Execute the code
|
| 26 |
+
exec(code, {}, local_vars)
|
| 27 |
+
|
| 28 |
+
# Get the output
|
| 29 |
+
output = buffer.getvalue()
|
| 30 |
+
return output, None
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return None, str(e)
|
| 33 |
+
finally:
|
| 34 |
+
buffer.close()
|
| 35 |
+
|
| 36 |
+
def main():
|
| 37 |
+
st.set_page_config(page_title="Python Code Executor", layout="wide")
|
| 38 |
+
|
| 39 |
+
st.title("📝 Python Code Executor")
|
| 40 |
+
st.markdown("Enter Python code directly or upload a .py/.md file")
|
| 41 |
+
|
| 42 |
+
# File uploader
|
| 43 |
+
uploaded_file = st.file_uploader("Upload a Python (.py) or Markdown (.md) file",
|
| 44 |
+
type=['py', 'md'])
|
| 45 |
+
|
| 46 |
+
# Code input area
|
| 47 |
+
if uploaded_file is None:
|
| 48 |
+
code = st.text_area("Or enter your Python code here:",
|
| 49 |
+
height=200,
|
| 50 |
+
placeholder="Enter your Python code...")
|
| 51 |
+
else:
|
| 52 |
+
content = uploaded_file.getvalue().decode()
|
| 53 |
+
if uploaded_file.type == "text/markdown":
|
| 54 |
+
# Extract Python code from markdown
|
| 55 |
+
code_blocks = extract_python_code(content)
|
| 56 |
+
if code_blocks:
|
| 57 |
+
code = code_blocks[0] # Use the first Python code block found
|
| 58 |
+
else:
|
| 59 |
+
st.error("No Python code blocks found in the markdown file!")
|
| 60 |
+
return
|
| 61 |
+
else:
|
| 62 |
+
# Assume it's a Python file
|
| 63 |
+
code = content
|
| 64 |
+
|
| 65 |
+
st.code(code, language='python')
|
| 66 |
+
|
| 67 |
+
# Execute button
|
| 68 |
+
if st.button("▶️ Run Code"):
|
| 69 |
+
if code:
|
| 70 |
+
st.markdown("### Output:")
|
| 71 |
+
output, error = execute_code(code)
|
| 72 |
+
|
| 73 |
+
if error:
|
| 74 |
+
st.error(f"Error:\n{error}")
|
| 75 |
+
elif output:
|
| 76 |
+
st.code(output)
|
| 77 |
+
else:
|
| 78 |
+
st.info("Code executed successfully with no output.")
|
| 79 |
+
else:
|
| 80 |
+
st.warning("Please enter some code to execute!")
|
| 81 |
+
|
| 82 |
+
# Add some usage information
|
| 83 |
+
with st.expander("ℹ️ How to use"):
|
| 84 |
+
st.markdown("""
|
| 85 |
+
1. Either upload a Python (.py) or Markdown (.md) file containing Python code
|
| 86 |
+
2. Or enter Python code directly in the text area
|
| 87 |
+
3. Click the 'Run Code' button to execute
|
| 88 |
+
|
| 89 |
+
**Note:** For markdown files, the code should be in Python code blocks like:
|
| 90 |
+
````
|
| 91 |
+
```python
|
| 92 |
+
print("Hello World!")
|
| 93 |
+
```
|
| 94 |
+
````
|
| 95 |
+
""")
|
| 96 |
+
|
| 97 |
+
# Add safety information
|
| 98 |
+
with st.expander("⚠️ Safety Notice"):
|
| 99 |
+
st.markdown("""
|
| 100 |
+
- The code executor runs in a restricted environment
|
| 101 |
+
- Some operations may be limited for security reasons
|
| 102 |
+
- Be careful when executing code from unknown sources
|
| 103 |
+
""")
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
main()
|