Artificial-superintelligence commited on
Commit
1598830
·
verified ·
1 Parent(s): 4d82c1e

Create static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +50 -0
static/script.js ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.getElementById("user-input").addEventListener("keydown", function(event) {
2
+ if (event.key === "Enter") {
3
+ let userInput = this.value.trim();
4
+ if (userInput) {
5
+ addOutput(userInput);
6
+ this.value = ""; // Clear input field
7
+
8
+ // Send the user input to the backend for execution
9
+ executePythonCode(userInput);
10
+ }
11
+ }
12
+ });
13
+
14
+ function addOutput(text) {
15
+ const outputDiv = document.createElement("div");
16
+ outputDiv.classList.add("prompt");
17
+ outputDiv.textContent = `>>> ${text}`;
18
+ document.querySelector(".output").appendChild(outputDiv);
19
+ }
20
+
21
+ function executePythonCode(code) {
22
+ // Sending code to Flask backend
23
+ fetch('/execute', {
24
+ method: 'POST',
25
+ headers: {
26
+ 'Content-Type': 'application/json',
27
+ },
28
+ body: JSON.stringify({ code: code }),
29
+ })
30
+ .then(response => response.json())
31
+ .then(data => {
32
+ addExecutionResult(data.result);
33
+ })
34
+ .catch(error => {
35
+ addExecutionResult("Error: " + error);
36
+ });
37
+ }
38
+
39
+ function addExecutionResult(result) {
40
+ const outputDiv = document.createElement("div");
41
+ outputDiv.classList.add("output");
42
+ outputDiv.textContent = result;
43
+ document.querySelector(".output").appendChild(outputDiv);
44
+ scrollToBottom();
45
+ }
46
+
47
+ function scrollToBottom() {
48
+ const terminal = document.getElementById("terminal");
49
+ terminal.scrollTop = terminal.scrollHeight;
50
+ }