-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
104 lines (90 loc) · 2.94 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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
// set the pyodide files URL (packages.json, pyodide.asm.data etc)
window.languagePluginUrl = 'https://brian-team.github.io/brian2_pyodide/';
</script>
<script src="pyodide.js"></script>
</head>
<body>
Pyodide Brian2 test <br>
<p id="loading">Loading libraries...</p>
<label for="code">Code:</label>
<textarea id="code" name="code" rows="43" cols="80" form"codesubmit">
import sys
sys.setrecursionlimit(10**5)
from brian2 import *
taum = 20*ms
taue = 5*ms
taui = 10*ms
Vt = -50*mV
Vr = -60*mV
El = -49*mV
eqs = '''
dv/dt = (ge+gi-(v-El))/taum : volt (unless refractory)
dge/dt = -ge/taue : volt
dgi/dt = -gi/taui : volt
'''
P = NeuronGroup(4000, eqs, threshold='v>Vt', reset='v = Vr', refractory=5*ms,
method='exact')
P.v = 'Vr + rand() * (Vt - Vr)'
P.ge = 0*mV
P.gi = 0*mV
we = (60*0.27/10)*mV # excitatory synaptic weight (voltage)
wi = (-20*4.5/10)*mV # inhibitory synaptic weight
Ce = Synapses(P, P, on_pre='ge += we')
Ci = Synapses(P, P, on_pre='gi += wi')
Ce.connect('i<3200', p=0.02)
Ci.connect('i>=3200', p=0.02)
s_mon = SpikeMonitor(P)
run(1 * second, report='text')
plt.plot(s_mon.t/ms, s_mon.i, ',k')
plt.xlabel('Time (ms)')
plt.ylabel('Neuron index')
# Convert image to base64
import base64
import io
pic_IObytes = io.BytesIO()
plt.savefig(pic_IObytes, format='png')
pic_IObytes.seek(0)
brian_pic = str(base64.b64encode(pic_IObytes.read()), 'utf-8')
</textarea>
<input type="button" id="runcode_button" value="Run code">
<div id="pyplotdiv">
<label for="pyplotfigure">Image stored in <code>brian_pic</code>:</label>
<img id="pyplotfigure"/>
</div>
<script type="text/javascript">
document.getElementById("runcode_button").disabled = true;
var pyodideWorker = new Worker('./webworker.js')
pyodideWorker.onerror = (e) => {
console.log(`Error in pyodideWorker at ${e.filename}, Line: ${e.lineno}, ${e.message}`)
}
pyodideWorker.onmessage = (e) => {
const {results, error} = e.data;
var button = document.getElementById("runcode_button");
button.disabled = false;
console.log('Received message from pyodideWorker: ', e.data);
if (error) {
console.log('pyodideWorker error: ', error)
button.value = "Run code";
}
else if (results) {
console.log('pyodideWorker results: ', results);
document.getElementById("pyplotfigure").src = 'data:image/png;base64,' + results;
button.value = "Run code";
} else {
document.getElementById("loading").textContent="Libraries loaded";
}
}
document.getElementById("runcode_button").onclick = function() {
code = document.getElementById("code").value;
var button = document.getElementById("runcode_button");
button.value = "Running...";
button.disabled = true;
var data = {'python' : code};
pyodideWorker.postMessage(data);
};
</script>
</body>