Spaceai / static /script.js
Artificial-superintelligence's picture
Create static/script.js
1598830 verified
raw
history blame
1.45 kB
document.getElementById("user-input").addEventListener("keydown", function(event) {
if (event.key === "Enter") {
let userInput = this.value.trim();
if (userInput) {
addOutput(userInput);
this.value = ""; // Clear input field
// Send the user input to the backend for execution
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) {
// Sending code to Flask backend
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;
}