Spaces:
Sleeping
Sleeping
kolibril13
commited on
Commit
•
99b3577
1
Parent(s):
cbb25bf
change to notebook
Browse files- pages/01_sine_widget.ipynb +124 -0
- pages/01_sine_widget.py +0 -90
pages/01_sine_widget.ipynb
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import ipyreact\n",
|
10 |
+
"from traitlets import List, Any\n",
|
11 |
+
"import numpy as np\n",
|
12 |
+
"\n",
|
13 |
+
"class TldrawSineWidget(ipyreact.ReactWidget):\n",
|
14 |
+
" points = List(List(Any())).tag(sync=True)\n",
|
15 |
+
" _esm = \"\"\"\n",
|
16 |
+
" import { TDShapeType, Tldraw } from \"@tldraw/tldraw\";\n",
|
17 |
+
" import * as React from \"react\";\n",
|
18 |
+
"\n",
|
19 |
+
" export default function App({ points }) {\n",
|
20 |
+
"\n",
|
21 |
+
" const [app, setApp] = React.useState()\n",
|
22 |
+
"\n",
|
23 |
+
" const handleMount = React.useCallback((app: Tldraw) => {\n",
|
24 |
+
" setApp(app)\n",
|
25 |
+
" }, []);\n",
|
26 |
+
"\n",
|
27 |
+
" React.useEffect(() => {\n",
|
28 |
+
" if (app) {\n",
|
29 |
+
" app.createShapes({\n",
|
30 |
+
" type: \"draw\",\n",
|
31 |
+
" id: \"draw1\",\n",
|
32 |
+
" color: 'red',\n",
|
33 |
+
" points: points,\n",
|
34 |
+
" });\n",
|
35 |
+
" }\n",
|
36 |
+
" }, [points, app])\n",
|
37 |
+
"\n",
|
38 |
+
" return (\n",
|
39 |
+
" <div\n",
|
40 |
+
" style={{\n",
|
41 |
+
" position: \"relative\",\n",
|
42 |
+
" width: \"800px\",\n",
|
43 |
+
" height: \"450px\",\n",
|
44 |
+
" }}\n",
|
45 |
+
" >\n",
|
46 |
+
" <Tldraw onMount={handleMount} onChange={e => console.log(\"hii\")} />\n",
|
47 |
+
" </div>\n",
|
48 |
+
" );\n",
|
49 |
+
"}\n",
|
50 |
+
"\n",
|
51 |
+
" \"\"\"\n",
|
52 |
+
"\n",
|
53 |
+
"import solara\n",
|
54 |
+
"import numpy as np\n",
|
55 |
+
"\n",
|
56 |
+
"float_value = solara.reactive(0)\n",
|
57 |
+
"\n",
|
58 |
+
"@solara.component\n",
|
59 |
+
"def Page():\n",
|
60 |
+
" dispersion = \"high\" # \"high\", \"low\", \"const\"\n",
|
61 |
+
" def n(wj):\n",
|
62 |
+
" if dispersion == \"high\":\n",
|
63 |
+
" return 1 + wj * 0.1\n",
|
64 |
+
" if dispersion == \"low\":\n",
|
65 |
+
" return 1.1 - wj * 0.01\n",
|
66 |
+
" if dispersion == \"const\":\n",
|
67 |
+
" return 1\n",
|
68 |
+
"\n",
|
69 |
+
" c = 1\n",
|
70 |
+
" num_of_waves = 121\n",
|
71 |
+
" start_w = 1\n",
|
72 |
+
" end_w = 7\n",
|
73 |
+
" x = np.linspace(-2, 10, 1001)\n",
|
74 |
+
" \n",
|
75 |
+
" def g(x, t):\n",
|
76 |
+
" u1 = 0\n",
|
77 |
+
" for wj in np.linspace(start_w, end_w, num_of_waves):\n",
|
78 |
+
" u1 += np.exp(1j * (wj * n(wj) / c * x - t * wj))\n",
|
79 |
+
" return u1.real\n",
|
80 |
+
"\n",
|
81 |
+
" ooo = 38 \n",
|
82 |
+
" points = np.column_stack((x*50+ooo, g(x, float_value.value)+150)).tolist()\n",
|
83 |
+
" points = points[100:]\n",
|
84 |
+
" points[0] = [ooo-40, 0]\n",
|
85 |
+
"\n",
|
86 |
+
"\n",
|
87 |
+
" # solara.Markdown(\" \\psi(x, t) = \\sum \\limits_j C_j \\cdot e^{\\mathrm{i}( - k_j \\cdot x + \\omega_j \\cdot t )}, \\omega_j= \\frac{k_j}{c(k_j)} $\")\n",
|
88 |
+
" # solara.Markdown(f\"**t**: {float_value.value}\")\n",
|
89 |
+
"\n",
|
90 |
+
"\n",
|
91 |
+
" with solara.Row():\n",
|
92 |
+
" solara.Button(\"Reset\", on_click=lambda: float_value.set(0))\n",
|
93 |
+
" solara.FloatSlider(\"Time t\", value=float_value, min=0, max=4*np.pi, step=0.1)\n",
|
94 |
+
" TldrawSineWidget.element(points=points)\n",
|
95 |
+
"\n",
|
96 |
+
"\n",
|
97 |
+
"\n",
|
98 |
+
"Page()"
|
99 |
+
]
|
100 |
+
}
|
101 |
+
],
|
102 |
+
"metadata": {
|
103 |
+
"kernelspec": {
|
104 |
+
"display_name": "napari-env2",
|
105 |
+
"language": "python",
|
106 |
+
"name": "python3"
|
107 |
+
},
|
108 |
+
"language_info": {
|
109 |
+
"codemirror_mode": {
|
110 |
+
"name": "ipython",
|
111 |
+
"version": 3
|
112 |
+
},
|
113 |
+
"file_extension": ".py",
|
114 |
+
"mimetype": "text/x-python",
|
115 |
+
"name": "python",
|
116 |
+
"nbconvert_exporter": "python",
|
117 |
+
"pygments_lexer": "ipython3",
|
118 |
+
"version": "3.9.15"
|
119 |
+
},
|
120 |
+
"orig_nbformat": 4
|
121 |
+
},
|
122 |
+
"nbformat": 4,
|
123 |
+
"nbformat_minor": 2
|
124 |
+
}
|
pages/01_sine_widget.py
DELETED
@@ -1,90 +0,0 @@
|
|
1 |
-
import ipyreact
|
2 |
-
from traitlets import List, Any
|
3 |
-
import numpy as np
|
4 |
-
|
5 |
-
class TldrawSineWidget(ipyreact.ReactWidget):
|
6 |
-
points = List(List(Any())).tag(sync=True)
|
7 |
-
_esm = """
|
8 |
-
import { TDShapeType, Tldraw } from "@tldraw/tldraw";
|
9 |
-
import * as React from "react";
|
10 |
-
|
11 |
-
export default function App({ points }) {
|
12 |
-
|
13 |
-
const [app, setApp] = React.useState()
|
14 |
-
|
15 |
-
const handleMount = React.useCallback((app: Tldraw) => {
|
16 |
-
setApp(app)
|
17 |
-
}, []);
|
18 |
-
|
19 |
-
React.useEffect(() => {
|
20 |
-
if (app) {
|
21 |
-
app.createShapes({
|
22 |
-
type: "draw",
|
23 |
-
id: "draw1",
|
24 |
-
color: 'red',
|
25 |
-
points: points,
|
26 |
-
});
|
27 |
-
}
|
28 |
-
}, [points, app])
|
29 |
-
|
30 |
-
return (
|
31 |
-
<div
|
32 |
-
style={{
|
33 |
-
position: "relative",
|
34 |
-
width: "800px",
|
35 |
-
height: "450px",
|
36 |
-
}}
|
37 |
-
>
|
38 |
-
<Tldraw onMount={handleMount} onChange={e => console.log("hii")} />
|
39 |
-
</div>
|
40 |
-
);
|
41 |
-
}
|
42 |
-
|
43 |
-
"""
|
44 |
-
|
45 |
-
import solara
|
46 |
-
import numpy as np
|
47 |
-
|
48 |
-
float_value = solara.reactive(0)
|
49 |
-
|
50 |
-
@solara.component
|
51 |
-
def Page():
|
52 |
-
dispersion = "high" # "high", "low", "const"
|
53 |
-
def n(wj):
|
54 |
-
if dispersion == "high":
|
55 |
-
return 1 + wj * 0.1
|
56 |
-
if dispersion == "low":
|
57 |
-
return 1.1 - wj * 0.01
|
58 |
-
if dispersion == "const":
|
59 |
-
return 1
|
60 |
-
|
61 |
-
c = 1
|
62 |
-
num_of_waves = 121
|
63 |
-
start_w = 1
|
64 |
-
end_w = 7
|
65 |
-
x = np.linspace(-2, 10, 1001)
|
66 |
-
|
67 |
-
def g(x, t):
|
68 |
-
u1 = 0
|
69 |
-
for wj in np.linspace(start_w, end_w, num_of_waves):
|
70 |
-
u1 += np.exp(1j * (wj * n(wj) / c * x - t * wj))
|
71 |
-
return u1.real
|
72 |
-
|
73 |
-
ooo = 38
|
74 |
-
points = np.column_stack((x*50+ooo, g(x, float_value.value)+150)).tolist()
|
75 |
-
points = points[100:]
|
76 |
-
points[0] = [ooo-40, 0]
|
77 |
-
|
78 |
-
|
79 |
-
# solara.Markdown(" \psi(x, t) = \sum \limits_j C_j \cdot e^{\mathrm{i}( - k_j \cdot x + \omega_j \cdot t )}, \omega_j= \frac{k_j}{c(k_j)} $")
|
80 |
-
# solara.Markdown(f"**t**: {float_value.value}")
|
81 |
-
|
82 |
-
|
83 |
-
with solara.Row():
|
84 |
-
solara.Button("Reset", on_click=lambda: float_value.set(0))
|
85 |
-
solara.FloatSlider("Time t", value=float_value, min=0, max=4*np.pi, step=0.1)
|
86 |
-
TldrawSineWidget.element(points=points)
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
Page()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|