|
document.getElementById("user-input").addEventListener("keydown", function(event) { |
|
if (event.key === "Enter") { |
|
let userInput = this.value.trim(); |
|
if (userInput) { |
|
addOutput(userInput); |
|
this.value = ""; |
|
|
|
|
|
executePythonCode(userInput); |
|
} |
|
} |
|
}); |
|
|
|
function addOutput(text) { |
|
const outputDiv = document.createElement("div"); |
|
outputDiv.classList.add("prompt"); |
|
outputDiv.textContent = `>>> ${text}`; |
|
document.querySelector(".output").appendChild(outputDiv); |
|
} |
|
|
|
function executePythonCode(code) { |
|
|
|
fetch('/execute', { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
}, |
|
body: JSON.stringify({ code: code }), |
|
}) |
|
.then(response => response.json()) |
|
.then(data => { |
|
addExecutionResult(data.result); |
|
}) |
|
.catch(error => { |
|
addExecutionResult("Error: " + error); |
|
}); |
|
} |
|
|
|
function addExecutionResult(result) { |
|
const outputDiv = document.createElement("div"); |
|
outputDiv.classList.add("output"); |
|
outputDiv.textContent = result; |
|
document.querySelector(".output").appendChild(outputDiv); |
|
scrollToBottom(); |
|
} |
|
|
|
function scrollToBottom() { |
|
const terminal = document.getElementById("terminal"); |
|
terminal.scrollTop = terminal.scrollHeight; |
|
} |