|
import base64 |
|
import time |
|
import streamlit as st |
|
from code_editor import code_editor |
|
import requests |
|
import json |
|
from streamlit_ace import st_ace |
|
import subprocess |
|
import os |
|
import tempfile |
|
import dotenv |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
|
|
|
|
|
|
|
|
JUDGE0_API_URL = "https://judge0-ce.p.rapidapi.com/submissions" |
|
JUDGE0_API_KEY = "065c9c9b12mshe3bfa8aa9549b16p11954ajsnda1e6cb80ec4" |
|
HEADERS = { |
|
"X-RapidAPI-Key": JUDGE0_API_KEY, |
|
"X-RapidAPI-Host": "judge0-ce.p.rapidapi.com", |
|
"Content-Type": "application/json", |
|
} |
|
|
|
|
|
LANGUAGES = { |
|
"C": 50, |
|
"C++": 54, |
|
"Java": 62, |
|
"Python": 71, |
|
"JavaScript": 63, |
|
} |
|
|
|
|
|
def execute_code(code, language_id): |
|
|
|
|
|
|
|
|
|
encoded_code = base64.b64encode(code.encode("utf-8")).decode("utf-8") |
|
|
|
|
|
data = { |
|
"source_code": encoded_code, |
|
"language_id": language_id, |
|
"base64_encoded": True, |
|
} |
|
response = requests.post( |
|
f"{JUDGE0_API_URL}?base64_encoded=true&wait=false", |
|
headers=HEADERS, |
|
json=data, |
|
) |
|
if response.status_code != 201: |
|
return f"Error: Unable to create submission. Status code: {response.status_code}, Response: {response.text}" |
|
|
|
submission_token = response.json()["token"] |
|
|
|
|
|
while True: |
|
result_response = requests.get( |
|
f"{JUDGE0_API_URL}/{submission_token}?base64_encoded=true", |
|
headers=HEADERS, |
|
) |
|
if result_response.status_code != 200: |
|
return f"Error: Unable to fetch result. Status code: {result_response.status_code}, Response: {result_response.text}" |
|
|
|
result = result_response.json() |
|
if result["status"]["id"] not in [1, 2]: |
|
break |
|
time.sleep(1) |
|
|
|
|
|
if result["status"]["id"] == 3: |
|
output = result["stdout"] if result["stdout"] else "No output" |
|
if output: |
|
|
|
output = base64.b64decode(output).decode("utf-8") |
|
return output |
|
else: |
|
error = result["stderr"] if result["stderr"] else result["compile_output"] |
|
if error: |
|
|
|
error = base64.b64decode(error).decode("utf-8") |
|
return error |
|
|
|
|
|
ACE_LANGUAGE_MODES = { |
|
"C": "c_cpp", |
|
"C++": "c_cpp", |
|
"Java": "java", |
|
"Python": "python", |
|
"JavaScript": "javascript", |
|
} |
|
|
|
def display_code_playground(session, student_id, course_id): |
|
st.markdown("#### Code Playground") |
|
|
|
|
|
language = st.selectbox("Select Language", list(LANGUAGES.keys())) |
|
|
|
|
|
code = st_ace( |
|
placeholder="Write your code here...", |
|
language=ACE_LANGUAGE_MODES[language], |
|
theme="clouds_midnight", |
|
key="code-editor", |
|
font_size=14, |
|
tab_size=4, |
|
wrap=True, |
|
show_gutter=True, |
|
show_print_margin=True, |
|
auto_update=False, |
|
height=300, |
|
) |
|
|
|
|
|
if st.button("Run"): |
|
if code: |
|
st.markdown("##### Output") |
|
with st.spinner("Executing code..."): |
|
output = execute_code(code, LANGUAGES[language]) |
|
st.code(output, language="text") |
|
else: |
|
st.warning("Please write some code before running.") |
|
|