forked from elnardu/react-use-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.jsx
70 lines (61 loc) · 1.54 KB
/
client.jsx
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
import React, { useState } from "react";
import ReactDOM from "react-dom";
// I haven't used any of this bs in a year, hopefully it still the same
/// Look mum! I am so cool, I put strings into a different file
function css(strings) {
let style = document.getElementById("css-in-js-style-thing");
if (!style) {
style = document.createElement("style");
style.id = "css-in-js-style-thing";
document.head.appendChild(style);
}
const randomClassId = Math.random().toString(36).slice(2);
style.textContent += `
.${randomClassId} {
${strings[0]}
}`;
return randomClassId;
}
async function runPython(code) {
const res = await fetch("/rpc/rce", {
method: "POST",
body: JSON.stringify({ code: code }),
headers: {
"Content-Type": "application/json",
},
});
return await res.json();
}
const appStyles = css`
margin: 3rem auto;
max-width: 600px;
padding: 0 1rem;
`;
const App = () => (
<div className={appStyles}>
<h1>React use Python demo</h1>
<Demo />
</div>
);
const Demo = () => {
const [output, setOutput] = useState(null);
const pyHelloWorld = async () => {
"use python";
import numpy as np
# 配列の平均値を計算
arr = np.array([1, 2, 3, 4, 5])
print(np.mean(arr))
};
const onClick = async () => {
const out = await pyHelloWorld();
setOutput(out.stdout);
};
return (
<div>
<button onClick={onClick}>Run Python code!</button>
<p>Output from Python:</p>
<pre>{ output }</pre>
</div>
);
};
ReactDOM.render(<App />, document.getElementById("app"));