File size: 1,451 Bytes
1598830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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;
}