-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
202 lines (184 loc) · 5.29 KB
/
index.html
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<html>
<head>
<title>Online Python Editor and Interpreter</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/theme/dracula.min.css">
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background-color: #282a36;
color: #f8f8f2;
margin: 0;
padding: 20px;
}
#editor-container, #output-container {
width: 80%;
margin: 20px 0;
border: 1px solid #6272a4;
border-radius: 5px;
overflow: hidden;
}
.CodeMirror {
height: 400px;
font-size: 16px;
}
.button-container {
display: flex;
justify-content: center;
width: 100%;
margin-bottom: 20px;
}
button {
margin: 0 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #50fa7b;
color: #282a36;
border: none;
border-radius: 5px;
transition: background-color 0.3s;
}
button:hover {
background-color: #5af78e;
}
#file-input {
display: none;
}
h1 {
margin-bottom: 10px;
}
#output {
background-color: #44475a;
color: #f8f8f2;
padding: 10px;
font-family: monospace;
white-space: pre-wrap;
height: 200px;
overflow-y: auto;
}
</style>
</head>
<body>
<h1>Online Python Editor</h1>
<div class="button-container">
<button id="open-btn">Open from Computer</button>
<button id="save-btn">Save to Computer</button>
<button id="run-btn">Run Code</button>
<input type="file" id="file-input" accept=".py">
</div>
<div id="editor-container">
<textarea id="editor" placeholder="# Start typing your Python code here..."></textarea>
</div>
<div id="output-container">
<div id="output"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/mode/python/python.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/addon/edit/closebrackets.min.js"></script>
<script src="https://cdn.jsdelivr.net/pyodide/v0.26.2/full/pyodide.js"></script>
<script>
let pyodide;
async function loadPyodideAndPackages() {
pyodide = await loadPyodide();
await pyodide.loadPackage("micropip");
const micropip = pyodide.pyimport("micropip");
await micropip.install('pynput');
console.log("Pyodide loaded successfully");
}
loadPyodideAndPackages();
document.addEventListener('DOMContentLoaded', () => {
const editor = CodeMirror.fromTextArea(document.getElementById('editor'), {
mode: 'python',
theme: 'dracula',
lineNumbers: true,
indentUnit: 4,
tabSize: 4,
indentWithTabs: false,
autofocus: true,
autoCloseBrackets: true,
extraKeys: {
"'": (cm) => {
cm.replaceSelection("''");
cm.execCommand("goCharLeft");
},
'"': (cm) => {
cm.replaceSelection('""');
cm.execCommand("goCharLeft");
}
}
});
const saveBtn = document.getElementById('save-btn');
const openBtn = document.getElementById('open-btn');
const runBtn = document.getElementById('run-btn');
const fileInput = document.getElementById('file-input');
const output = document.getElementById('output');
saveBtn.addEventListener('click', () => {
const text = editor.getValue();
const blob = new Blob([text], {type: 'text/plain'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'python_script.py';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
openBtn.addEventListener('click', () => {
fileInput.click();
});
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
editor.setValue(e.target.result);
};
reader.readAsText(file);
}
});
runBtn.addEventListener('click', async () => {
if (!pyodide) {
output.textContent = "Pyodide is still loading. Please wait...";
return;
}
const code = editor.getValue();
output.textContent = "Running...";
try {
// Redirect stdout to capture print statements
pyodide.runPython(`
import sys
import io
sys.stdout = io.StringIO()
`);
// Run the user's code asynchronously
await pyodide.runPythonAsync(code);
// Get the captured output
const stdout = pyodide.runPython("sys.stdout.getvalue()");
output.textContent = stdout || "Code executed successfully with no output.";
} catch (err) {
output.textContent = `Error: ${err.message}`;
}
});
// Example script with async loop instead of while True
editor.setValue(`
import asyncio
async def example_async_loop():
counter = 0
while counter < 5:
print(f"Running loop {counter + 1}")
await asyncio.sleep(1) # Avoid blocking the main thread
counter += 1
# Run the async loop
await example_async_loop()
print("Loop completed!")
# Click the "Run Code" button to execute this script!
# Please note that 'While True:' and 'input()' doesn't work!
`.trim());
});
</script>
</body></html>