-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
55 lines (45 loc) · 1.32 KB
/
Copy pathApp.js
File metadata and controls
55 lines (45 loc) · 1.32 KB
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
import React, { useRef } from 'react';
import Editor from '@monaco-editor/react';
function App() {
const editorRef = useRef(null);
function handleEditorDidMount(editor) {
editorRef.current = editor;
}
async function executeCode() {
const code = editorRef.current.getValue();
const payload = {
code,
language_id: 92, // Python 3.11.2, adjust based on required language
};
try {
const response = await fetch('http://127.0.0.1:5000/execute_code', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
const data = await response.json();
console.log('Response from server:', data);
// Display result or error in your frontend
const output = data.stdout || data.stderr || data.message || 'No output returned.';
alert(output);
} catch (error) {
console.error('Error:', error);
alert('Error executing code.');
}
}
return (
<div className="App">
<h1>Hackloft Code Editor</h1>
<Editor
height="90vh"
defaultLanguage="javascript"
defaultValue="// Write your code here..."
onMount={handleEditorDidMount}
/>
<button onClick={executeCode}>Run Code</button>
</div>
);
}
export default App;