Spaces:
Running
Running
import streamlit as st | |
import requests | |
import os | |
# Load API key from Hugging Face secrets | |
api_key = os.getenv("KEY") | |
# Streamlit UI | |
st.title("Python Code Compiler") | |
st.write("Compile and run Python code using an online compiler API.") | |
code_input = st.text_area("Enter your Python code:", "print('Hello, World!')") | |
def compile_code(code): | |
url = "https://python-code-compiler.p.rapidapi.com/compile" | |
headers = { | |
"x-rapidapi-key": api_key, | |
"x-rapidapi-host": "python-code-compiler.p.rapidapi.com", | |
"Content-Type": "text/plain" | |
} | |
response = requests.post(url, data=code, headers=headers) | |
return response.json() | |
if st.button("Run Code"): | |
with st.spinner("Compiling..."): | |
result = compile_code(code_input) | |
stdout_output = result.get("stdout", "") | |
if stdout_output: | |
st.success("Execution Output:") | |
st.code(stdout_output, language="python") | |
else: | |
st.error("Failed to compile code.") | |
st.write("Response Data:", result) # Debugging: Show response data | |