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.")