File size: 3,810 Bytes
1eb8c2b |
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
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()
# Code 2:
# Supported languages and their file extensions
# Judge0 API configuration
JUDGE0_API_URL = "https://judge0-ce.p.rapidapi.com/submissions"
JUDGE0_API_KEY = "065c9c9b12mshe3bfa8aa9549b16p11954ajsnda1e6cb80ec4"# Replace with your RapidAPI key
HEADERS = {
"X-RapidAPI-Key": JUDGE0_API_KEY,
"X-RapidAPI-Host": "judge0-ce.p.rapidapi.com",
"Content-Type": "application/json",
}
# Supported languages and their Judge0 language IDs
LANGUAGES = {
"C": 50,
"C++": 54,
"Java": 62,
"Python": 71,
"JavaScript": 63,
}
# Function to execute code using Judge0 API
def execute_code(code, language_id):
# Create a submission
# code 3:
# Function to execute code using Judge0 API
# Encode the code in base64
encoded_code = base64.b64encode(code.encode("utf-8")).decode("utf-8")
# Create a submission
data = {
"source_code": encoded_code,
"language_id": language_id,
"base64_encoded": True, # Indicate that the code is base64 encoded
}
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"]
# Poll for the result
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]: # Status 1 = in queue, 2 = processing
break
time.sleep(1) # Wait before polling again
# Parse the result
if result["status"]["id"] == 3: # Status 3 = accepted
output = result["stdout"] if result["stdout"] else "No output"
if output:
# Decode the output from base64
output = base64.b64decode(output).decode("utf-8")
return output
else:
error = result["stderr"] if result["stderr"] else result["compile_output"]
if error:
# Decode the error from base64
error = base64.b64decode(error).decode("utf-8")
return error
# Map languages to Ace Editor language modes
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 selection
language = st.selectbox("Select Language", list(LANGUAGES.keys()))
# Code editor
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,
)
# Run button
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.")
|