j1-template 2022.5.0.rc3 → 2022.5.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/assets/data/banner.html +2 -2
- data/assets/themes/j1/adapter/js/masterslider.js +22 -5
- data/lib/j1/version.rb +1 -1
- data/lib/starter_web/_config.yml +3 -3
- data/lib/starter_web/_data/blocks/banner.yml +1 -1
- data/lib/starter_web/_data/modules/defaults/masterslider.yml +1 -1
- data/lib/starter_web/_data/modules/masterslider.yml +0 -4
- data/lib/starter_web/package.json +1 -1
- data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_interactive_widgets.html +512 -512
- data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets.html +629 -629
- data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_bar_chart.html +95 -95
- data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_hist_chart.html +93 -93
- data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_line_chart.html +96 -96
- data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_question_multiple_choice.html +70 -70
- data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_question_short_answer.html +156 -156
- data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_scatter_chart.html +95 -95
- data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_scatter_drag_chart.html +93 -93
- data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_odes_in_python.html +955 -955
- data/lib/starter_web/utilsrv/_defaults/package.json +1 -1
- data/lib/starter_web/utilsrv/package.json +1 -1
- metadata +2 -2
@@ -1,629 +1,629 @@
|
|
1
|
-
<div class="cell text_cell">
|
2
|
-
<button class="js-nbinteract-widget">
|
3
|
-
Loading widgets...
|
4
|
-
</button>
|
5
|
-
</div>
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
10
|
-
<div class="text_cell_render border-box-sizing rendered_html">
|
11
|
-
<h2 id="Widgets-from-j1-nbinteract">Widgets from j1-nbinteract<a class="anchor-link" href="#Widgets-from-j1-nbinteract">¶</a></h2><p>The library <em>j1-nbinteract</em> comes with a set of functions that produce Javascript-based plots designed for interaction. The package combines the <em>ipywidgets</em> library and the <em>bqplot</em>
|
12
|
-
plotting library to implement function-driven interfaces to interactive plotting.</p>
|
13
|
-
<p>The nbinteract plotting methods use <strong>ipywidgets</strong> to generate and display widgets, inferring the widget type as needed. When a user interacts with a widget, a Python callback updates the visualization <strong>without</strong> a complete rerender. This noticeably lowers visualization update time compared to using ipywidgets alone to render static images.</p>
|
14
|
-
|
15
|
-
</div>
|
16
|
-
</div>
|
17
|
-
</div>
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
<div class="
|
22
|
-
cell border-box-sizing code_cell rendered">
|
23
|
-
<div class="input">
|
24
|
-
|
25
|
-
<div class="inner_cell">
|
26
|
-
<div class="input_area">
|
27
|
-
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">nbinteract</span> <span class="k">as</span> <span class="nn">nbi</span>
|
28
|
-
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
29
|
-
<span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">arange</span>
|
30
|
-
</pre></div>
|
31
|
-
|
32
|
-
</div>
|
33
|
-
</div>
|
34
|
-
</div>
|
35
|
-
|
36
|
-
</div>
|
37
|
-
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
38
|
-
<div class="text_cell_render border-box-sizing rendered_html">
|
39
|
-
<h3 id="Histogram">Histogram<a class="anchor-link" href="#Histogram">¶</a></h3><p>The widget <code>nbinteract.nbinteract.hist</code> generates a histogram that allows interaction with the parameters for the response function. The widget takes in a single response function. The response function returns the array of numerical values that will be shown in the histogram. The hist function allows interaction with the response function's parameters by specifying them as keyword arguments in the same format as ipywidgets.interact. Any argument that can be used for ipywidgets.interact can be used for hist.</p>
|
40
|
-
|
41
|
-
</div>
|
42
|
-
</div>
|
43
|
-
</div>
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
<div class="
|
48
|
-
cell border-box-sizing code_cell rendered">
|
49
|
-
<div class="input">
|
50
|
-
|
51
|
-
<div class="inner_cell">
|
52
|
-
<div class="input_area">
|
53
|
-
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">hist_function</span><span class="p">(</span><span class="n">mean</span><span class="p">,</span> <span class="n">sd</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">1000</span><span class="p">):</span>
|
54
|
-
<span class="sd">'''</span>
|
55
|
-
<span class="sd"> Returns 1000 values picked at random from the normal</span>
|
56
|
-
<span class="sd"> distribution with the Mean value and SD given.</span>
|
57
|
-
<span class="sd"> '''</span>
|
58
|
-
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="n">mean</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="n">sd</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span>
|
59
|
-
|
60
|
-
|
61
|
-
<span class="n">options</span> <span class="o">=</span> <span class="p">{</span>
|
62
|
-
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'Histogram'</span><span class="p">,</span>
|
63
|
-
<span class="s1">'xlabel'</span><span class="p">:</span> <span class="s1">'Mean value (mean)'</span><span class="p">,</span>
|
64
|
-
<span class="s1">'ylabel'</span><span class="p">:</span> <span class="s1">'Standard Deviation (sd)'</span><span class="p">,</span>
|
65
|
-
<span class="s1">'bins'</span><span class="p">:</span> <span class="mi">10</span>
|
66
|
-
<span class="p">}</span>
|
67
|
-
|
68
|
-
<span class="n">layouts</span> <span class="o">=</span> <span class="p">{</span>
|
69
|
-
<span class="s1">'plot_height'</span><span class="p">:</span> <span class="s1">'480px'</span><span class="p">,</span>
|
70
|
-
<span class="s1">'plot_width'</span><span class="p">:</span> <span class="s1">'800px'</span><span class="p">,</span>
|
71
|
-
<span class="p">}</span>
|
72
|
-
|
73
|
-
<span class="n">hist_chart</span> <span class="o">=</span> <span class="n">nbi</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span>
|
74
|
-
<span class="n">hist_function</span><span class="p">,</span>
|
75
|
-
<span class="n">mean</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
|
76
|
-
<span class="n">sd</span><span class="o">=</span><span class="p">(</span><span class="mf">0.2</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">),</span>
|
77
|
-
<span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
|
78
|
-
<span class="n">layouts</span><span class="o">=</span><span class="n">layouts</span>
|
79
|
-
<span class="p">)</span>
|
80
|
-
|
81
|
-
<span class="n">hist_chart</span>
|
82
|
-
</pre></div>
|
83
|
-
|
84
|
-
</div>
|
85
|
-
</div>
|
86
|
-
</div>
|
87
|
-
|
88
|
-
<div class="output_wrapper">
|
89
|
-
<div class="output">
|
90
|
-
|
91
|
-
|
92
|
-
<div class="output_area">
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
<div class="output_subarea output_widget_view ">
|
99
|
-
<button class="js-nbinteract-widget">
|
100
|
-
Loading widgets...
|
101
|
-
</button>
|
102
|
-
</div>
|
103
|
-
|
104
|
-
</div>
|
105
|
-
|
106
|
-
</div>
|
107
|
-
</div>
|
108
|
-
|
109
|
-
</div>
|
110
|
-
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
111
|
-
<div class="text_cell_render border-box-sizing rendered_html">
|
112
|
-
<h3 id="Barchart">Barchart<a class="anchor-link" href="#Barchart">¶</a></h3><p>The widget <code>nbinteract.nbinteract.bar</code> generates an bar plot that allows interaction with the parameters for the response functions.</p>
|
113
|
-
<p>The first two arguments of bar are response functions that return the x and y-axis data arrays, respectively. Either argument can be arrays themselves. Arguments for the response functions must be passed in as keyword arguments to bar in the format expected by interact. The response function for the y-axis data will be called with the x-axis data as its first argument.</p>
|
114
|
-
<p>For example, in the bar plot below categories generates the categories to plot on the x-axis and heights generates the y-axis heights. The heights function uses the parameter xs which is the array of x-axis data points.</p>
|
115
|
-
|
116
|
-
</div>
|
117
|
-
</div>
|
118
|
-
</div>
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
<div class="
|
123
|
-
cell border-box-sizing code_cell rendered">
|
124
|
-
<div class="input">
|
125
|
-
|
126
|
-
<div class="inner_cell">
|
127
|
-
<div class="input_area">
|
128
|
-
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">x_fn</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
129
|
-
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
|
130
|
-
|
131
|
-
<span class="k">def</span> <span class="nf">y_fn</span><span class="p">(</span><span class="n">xs</span><span class="p">,</span> <span class="n">offset</span><span class="p">):</span>
|
132
|
-
<span class="k">return</span> <span class="n">xs</span> <span class="o">+</span> <span class="n">offset</span>
|
133
|
-
|
134
|
-
|
135
|
-
<span class="n">options</span> <span class="o">=</span> <span class="p">{</span>
|
136
|
-
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'Barchart'</span><span class="p">,</span>
|
137
|
-
<span class="s1">'xlabel'</span><span class="p">:</span> <span class="s1">'Number of bars (n)'</span><span class="p">,</span>
|
138
|
-
<span class="s1">'ylabel'</span><span class="p">:</span> <span class="s1">'Offset value'</span><span class="p">,</span>
|
139
|
-
<span class="s1">'animation_duration'</span><span class="p">:</span> <span class="mi">500</span><span class="p">,</span>
|
140
|
-
<span class="s1">'aspect_ratio'</span><span class="p">:</span> <span class="mf">1.778</span><span class="p">,</span>
|
141
|
-
<span class="s1">'ylim'</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span>
|
142
|
-
<span class="p">}</span>
|
143
|
-
|
144
|
-
<span class="n">layouts</span> <span class="o">=</span> <span class="p">{</span>
|
145
|
-
<span class="s1">'plot_height'</span><span class="p">:</span> <span class="s1">'480px'</span><span class="p">,</span>
|
146
|
-
<span class="s1">'plot_width'</span><span class="p">:</span> <span class="s1">'800px'</span><span class="p">,</span>
|
147
|
-
<span class="p">}</span>
|
148
|
-
|
149
|
-
<span class="n">bar_chart</span> <span class="o">=</span> <span class="n">nbi</span><span class="o">.</span><span class="n">bar</span><span class="p">(</span>
|
150
|
-
<span class="n">x_fn</span><span class="p">,</span>
|
151
|
-
<span class="n">y_fn</span><span class="p">,</span>
|
152
|
-
<span class="n">n</span><span class="o">=</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
|
153
|
-
<span class="n">offset</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
|
154
|
-
<span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
|
155
|
-
<span class="n">layouts</span><span class="o">=</span><span class="n">layouts</span>
|
156
|
-
<span class="p">)</span>
|
157
|
-
|
158
|
-
<span class="n">bar_chart</span>
|
159
|
-
</pre></div>
|
160
|
-
|
161
|
-
</div>
|
162
|
-
</div>
|
163
|
-
</div>
|
164
|
-
|
165
|
-
<div class="output_wrapper">
|
166
|
-
<div class="output">
|
167
|
-
|
168
|
-
|
169
|
-
<div class="output_area">
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
<div class="output_subarea output_widget_view ">
|
176
|
-
<button class="js-nbinteract-widget">
|
177
|
-
Loading widgets...
|
178
|
-
</button>
|
179
|
-
</div>
|
180
|
-
|
181
|
-
</div>
|
182
|
-
|
183
|
-
</div>
|
184
|
-
</div>
|
185
|
-
|
186
|
-
</div>
|
187
|
-
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
188
|
-
<div class="text_cell_render border-box-sizing rendered_html">
|
189
|
-
<h3 id="Interactive-Scatter-chart">Interactive Scatter chart<a class="anchor-link" href="#Interactive-Scatter-chart">¶</a></h3><p>The widget <code>nbinteract.scatter_drag</code> generates a scatter plot that allows interaction through clicking and dragging the points on the graph.</p>
|
190
|
-
<p>scatter_drag takes in two lists/arrays consisting of the x-coordinates and y-coordinates of the points to plot. It generates an interactive scatter plot where the points can be dragged by the user and a best fit line is updated automatically according to the placement of the points.</p>
|
191
|
-
<p>scatter_drag does not allow response functions as inputs.</p>
|
192
|
-
|
193
|
-
</div>
|
194
|
-
</div>
|
195
|
-
</div>
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
<div class="
|
200
|
-
cell border-box-sizing code_cell rendered">
|
201
|
-
<div class="input">
|
202
|
-
|
203
|
-
<div class="inner_cell">
|
204
|
-
<div class="input_area">
|
205
|
-
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x_points</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
206
|
-
<span class="n">y_points</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
207
|
-
|
208
|
-
|
209
|
-
<span class="n">options</span> <span class="o">=</span> <span class="p">{</span>
|
210
|
-
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'Interactive Scatter Plot'</span><span class="p">,</span>
|
211
|
-
<span class="s1">'animation_duration'</span><span class="p">:</span> <span class="mi">1000</span><span class="p">,</span>
|
212
|
-
<span class="s1">'xlabel'</span><span class="p">:</span> <span class="s1">'x-values'</span><span class="p">,</span>
|
213
|
-
<span class="s1">'ylabel'</span><span class="p">:</span> <span class="s1">'y-values'</span><span class="p">,</span>
|
214
|
-
<span class="s1">'xlim'</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
|
215
|
-
<span class="s1">'ylim'</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
|
216
|
-
<span class="p">}</span>
|
217
|
-
|
218
|
-
<span class="n">layouts</span> <span class="o">=</span> <span class="p">{</span>
|
219
|
-
<span class="s1">'plot_height'</span><span class="p">:</span> <span class="s1">'480px'</span><span class="p">,</span>
|
220
|
-
<span class="s1">'plot_width'</span><span class="p">:</span> <span class="s1">'800px'</span><span class="p">,</span>
|
221
|
-
<span class="p">}</span>
|
222
|
-
|
223
|
-
<span class="n">show_eqn</span> <span class="o">=</span> <span class="kc">False</span>
|
224
|
-
|
225
|
-
<span class="n">scatter_drag</span> <span class="o">=</span> <span class="n">nbi</span><span class="o">.</span><span class="n">scatter_drag</span><span class="p">(</span>
|
226
|
-
<span class="n">x_points</span><span class="p">,</span>
|
227
|
-
<span class="n">y_points</span><span class="p">,</span>
|
228
|
-
<span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
|
229
|
-
<span class="n">layouts</span><span class="o">=</span><span class="n">layouts</span>
|
230
|
-
<span class="p">)</span>
|
231
|
-
|
232
|
-
<span class="n">scatter_drag</span>
|
233
|
-
</pre></div>
|
234
|
-
|
235
|
-
</div>
|
236
|
-
</div>
|
237
|
-
</div>
|
238
|
-
|
239
|
-
<div class="output_wrapper">
|
240
|
-
<div class="output">
|
241
|
-
|
242
|
-
|
243
|
-
<div class="output_area">
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
<div class="output_subarea output_widget_view ">
|
250
|
-
<button class="js-nbinteract-widget">
|
251
|
-
Loading widgets...
|
252
|
-
</button>
|
253
|
-
</div>
|
254
|
-
|
255
|
-
</div>
|
256
|
-
|
257
|
-
</div>
|
258
|
-
</div>
|
259
|
-
|
260
|
-
</div>
|
261
|
-
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
262
|
-
<div class="text_cell_render border-box-sizing rendered_html">
|
263
|
-
<h3 id="Scatter-chart">Scatter chart<a class="anchor-link" href="#Scatter-chart">¶</a></h3><p>The scatter widget <code>nbinteract.scatter</code> generates a scatter plot that allows interaction with the parameters to the response functions. This is different from scatter_drag which facilitates interaction using click and drag actions.</p>
|
264
|
-
<p>The first two arguments of scatter are response functions that return the x and y-axis coordinates, respectively. Either argument can be arrays themselves. Arguments for the response functions must be passed in as keyword arguments to scatter in the format expected by interact. The response function for the y-coordinates will be called with the x-coordinates as its first argument.</p>
|
265
|
-
|
266
|
-
</div>
|
267
|
-
</div>
|
268
|
-
</div>
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
<div class="
|
273
|
-
cell border-box-sizing code_cell rendered">
|
274
|
-
<div class="input">
|
275
|
-
|
276
|
-
<div class="inner_cell">
|
277
|
-
<div class="input_area">
|
278
|
-
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">x_fn</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
279
|
-
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
|
280
|
-
|
281
|
-
<span class="k">def</span> <span class="nf">y_fn</span><span class="p">(</span><span class="n">xs</span><span class="p">):</span>
|
282
|
-
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">xs</span><span class="p">))</span>
|
283
|
-
|
284
|
-
|
285
|
-
<span class="n">options</span> <span class="o">=</span> <span class="p">{</span>
|
286
|
-
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'Scatter Plot'</span><span class="p">,</span>
|
287
|
-
<span class="s1">'marker'</span><span class="p">:</span> <span class="s1">'circle'</span><span class="p">,</span>
|
288
|
-
<span class="s1">'animation_duration'</span><span class="p">:</span> <span class="mi">1000</span><span class="p">,</span>
|
289
|
-
<span class="s1">'xlabel'</span><span class="p">:</span> <span class="s1">'x-values'</span><span class="p">,</span>
|
290
|
-
<span class="s1">'ylabel'</span><span class="p">:</span> <span class="s1">'y-values'</span><span class="p">,</span>
|
291
|
-
<span class="s1">'xlim'</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">),</span>
|
292
|
-
<span class="s1">'ylim'</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
|
293
|
-
<span class="p">}</span>
|
294
|
-
|
295
|
-
<span class="n">layouts</span> <span class="o">=</span> <span class="p">{</span>
|
296
|
-
<span class="s1">'plot_height'</span><span class="p">:</span> <span class="s1">'480px'</span><span class="p">,</span>
|
297
|
-
<span class="s1">'plot_width'</span><span class="p">:</span> <span class="s1">'800px'</span><span class="p">,</span>
|
298
|
-
<span class="p">}</span>
|
299
|
-
|
300
|
-
<span class="n">scatter_chart</span> <span class="o">=</span> <span class="n">nbi</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span>
|
301
|
-
<span class="n">x_fn</span><span class="p">,</span>
|
302
|
-
<span class="n">y_fn</span><span class="p">,</span>
|
303
|
-
<span class="n">n</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">200</span><span class="p">),</span>
|
304
|
-
<span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
|
305
|
-
<span class="n">layouts</span><span class="o">=</span><span class="n">layouts</span>
|
306
|
-
<span class="p">)</span>
|
307
|
-
|
308
|
-
<span class="n">scatter_chart</span>
|
309
|
-
</pre></div>
|
310
|
-
|
311
|
-
</div>
|
312
|
-
</div>
|
313
|
-
</div>
|
314
|
-
|
315
|
-
<div class="output_wrapper">
|
316
|
-
<div class="output">
|
317
|
-
|
318
|
-
|
319
|
-
<div class="output_area">
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
<div class="output_subarea output_widget_view ">
|
326
|
-
<button class="js-nbinteract-widget">
|
327
|
-
Loading widgets...
|
328
|
-
</button>
|
329
|
-
</div>
|
330
|
-
|
331
|
-
</div>
|
332
|
-
|
333
|
-
</div>
|
334
|
-
</div>
|
335
|
-
|
336
|
-
</div>
|
337
|
-
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
338
|
-
<div class="text_cell_render border-box-sizing rendered_html">
|
339
|
-
<h3 id="Line-chart">Line chart<a class="anchor-link" href="#Line-chart">¶</a></h3><p>The line widget <code>nbinteract.line</code> generates a line plot that allows interaction with the parameters to the response functions.</p>
|
340
|
-
<p>The first two arguments of line are response functions that return the x and y-axis coordinates, respectively. Either argument can be arrays themselves. Arguments for the response functions must be passed in as keyword arguments to line in the format expected by interact. The response function for the y-coordinates will be called with the x-coordinates as its first argument.</p>
|
341
|
-
|
342
|
-
</div>
|
343
|
-
</div>
|
344
|
-
</div>
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
<div class="
|
349
|
-
cell border-box-sizing code_cell rendered">
|
350
|
-
<div class="input">
|
351
|
-
|
352
|
-
<div class="inner_cell">
|
353
|
-
<div class="input_area">
|
354
|
-
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">x_fn</span><span class="p">(</span><span class="nb">max</span><span class="p">):</span>
|
355
|
-
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
|
356
|
-
|
357
|
-
<span class="k">def</span> <span class="nf">y_fn</span><span class="p">(</span><span class="n">xs</span><span class="p">,</span> <span class="n">sd</span><span class="p">):</span>
|
358
|
-
<span class="k">return</span> <span class="n">xs</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="n">sd</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">xs</span><span class="p">))</span>
|
359
|
-
|
360
|
-
|
361
|
-
<span class="n">options</span> <span class="o">=</span> <span class="p">{</span>
|
362
|
-
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'Line chart'</span><span class="p">,</span>
|
363
|
-
<span class="s1">'xlabel'</span><span class="p">:</span> <span class="s1">'x-values (max)'</span><span class="p">,</span>
|
364
|
-
<span class="s1">'ylabel'</span><span class="p">:</span> <span class="s1">'y-values (sd)'</span><span class="p">,</span>
|
365
|
-
<span class="s1">'xlim'</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">50</span><span class="p">),</span>
|
366
|
-
<span class="s1">'ylim'</span><span class="p">:</span> <span class="p">(</span><span class="o">-</span><span class="mi">20</span><span class="p">,</span> <span class="mi">70</span><span class="p">),</span>
|
367
|
-
<span class="s1">'animation_duration'</span><span class="p">:</span> <span class="mi">500</span>
|
368
|
-
<span class="p">}</span>
|
369
|
-
|
370
|
-
<span class="n">layouts</span> <span class="o">=</span> <span class="p">{</span>
|
371
|
-
<span class="s1">'plot_height'</span><span class="p">:</span> <span class="s1">'480px'</span><span class="p">,</span>
|
372
|
-
<span class="s1">'plot_width'</span><span class="p">:</span> <span class="s1">'800px'</span><span class="p">,</span>
|
373
|
-
<span class="p">}</span>
|
374
|
-
|
375
|
-
<span class="n">line_chart</span> <span class="o">=</span> <span class="n">nbi</span><span class="o">.</span><span class="n">line</span><span class="p">(</span>
|
376
|
-
<span class="n">x_fn</span><span class="p">,</span>
|
377
|
-
<span class="n">y_fn</span><span class="p">,</span>
|
378
|
-
<span class="nb">max</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">50</span><span class="p">),</span>
|
379
|
-
<span class="n">sd</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
|
380
|
-
<span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
|
381
|
-
<span class="n">layouts</span><span class="o">=</span><span class="n">layouts</span>
|
382
|
-
<span class="p">)</span>
|
383
|
-
|
384
|
-
<span class="n">line_chart</span>
|
385
|
-
</pre></div>
|
386
|
-
|
387
|
-
</div>
|
388
|
-
</div>
|
389
|
-
</div>
|
390
|
-
|
391
|
-
<div class="output_wrapper">
|
392
|
-
<div class="output">
|
393
|
-
|
394
|
-
|
395
|
-
<div class="output_area">
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
<div class="output_subarea output_widget_view ">
|
402
|
-
<button class="js-nbinteract-widget">
|
403
|
-
Loading widgets...
|
404
|
-
</button>
|
405
|
-
</div>
|
406
|
-
|
407
|
-
</div>
|
408
|
-
|
409
|
-
</div>
|
410
|
-
</div>
|
411
|
-
|
412
|
-
</div>
|
413
|
-
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
414
|
-
<div class="text_cell_render border-box-sizing rendered_html">
|
415
|
-
<h2 id="Interactive-Questions">Interactive Questions<a class="anchor-link" href="#Interactive-Questions">¶</a></h2><p>nbinteract also provides built-in methods to easily create multiple choice and short answer questions.</p>
|
416
|
-
|
417
|
-
</div>
|
418
|
-
</div>
|
419
|
-
</div>
|
420
|
-
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
421
|
-
<div class="text_cell_render border-box-sizing rendered_html">
|
422
|
-
<h3 id="Multiple-choice">Multiple choice<a class="anchor-link" href="#Multiple-choice">¶</a></h3><p>The widget <code>nbinteract.multiple_choice</code> takes a question, a list of possible answer choices, and the correct answer. Clicking the buttons shows whether the choice was correct.</p>
|
423
|
-
|
424
|
-
</div>
|
425
|
-
</div>
|
426
|
-
</div>
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
<div class="
|
431
|
-
cell border-box-sizing code_cell rendered">
|
432
|
-
<div class="input">
|
433
|
-
|
434
|
-
<div class="inner_cell">
|
435
|
-
<div class="input_area">
|
436
|
-
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">nbi</span><span class="o">.</span><span class="n">multiple_choice</span><span class="p">(</span><span class="n">question</span><span class="o">=</span><span class="s2">"What is 10 + 2 * 5?"</span><span class="p">,</span>
|
437
|
-
<span class="n">choices</span><span class="o">=</span><span class="p">[</span><span class="s1">'12'</span><span class="p">,</span> <span class="s1">'60'</span><span class="p">,</span> <span class="s1">'20'</span><span class="p">],</span>
|
438
|
-
<span class="n">answers</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
|
439
|
-
</pre></div>
|
440
|
-
|
441
|
-
</div>
|
442
|
-
</div>
|
443
|
-
</div>
|
444
|
-
|
445
|
-
<div class="output_wrapper">
|
446
|
-
<div class="output">
|
447
|
-
|
448
|
-
|
449
|
-
<div class="output_area">
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
<div class="output_subarea output_widget_view ">
|
456
|
-
<button class="js-nbinteract-widget">
|
457
|
-
Loading widgets...
|
458
|
-
</button>
|
459
|
-
</div>
|
460
|
-
|
461
|
-
</div>
|
462
|
-
|
463
|
-
</div>
|
464
|
-
</div>
|
465
|
-
|
466
|
-
</div>
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
<div class="
|
471
|
-
cell border-box-sizing code_cell rendered">
|
472
|
-
<div class="input">
|
473
|
-
|
474
|
-
<div class="inner_cell">
|
475
|
-
<div class="input_area">
|
476
|
-
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">nbi</span><span class="o">.</span><span class="n">multiple_choice</span><span class="p">(</span><span class="n">question</span><span class="o">=</span><span class="s2">"Select all prime numbers."</span><span class="p">,</span>
|
477
|
-
<span class="n">choices</span><span class="o">=</span><span class="p">[</span><span class="s1">'12'</span><span class="p">,</span> <span class="s1">'3'</span><span class="p">,</span> <span class="s1">'31'</span><span class="p">],</span>
|
478
|
-
<span class="n">answers</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>
|
479
|
-
</pre></div>
|
480
|
-
|
481
|
-
</div>
|
482
|
-
</div>
|
483
|
-
</div>
|
484
|
-
|
485
|
-
<div class="output_wrapper">
|
486
|
-
<div class="output">
|
487
|
-
|
488
|
-
|
489
|
-
<div class="output_area">
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
<div class="output_subarea output_widget_view ">
|
496
|
-
<button class="js-nbinteract-widget">
|
497
|
-
Loading widgets...
|
498
|
-
</button>
|
499
|
-
</div>
|
500
|
-
|
501
|
-
</div>
|
502
|
-
|
503
|
-
</div>
|
504
|
-
</div>
|
505
|
-
|
506
|
-
</div>
|
507
|
-
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
508
|
-
<div class="text_cell_render border-box-sizing rendered_html">
|
509
|
-
<h3 id="Short-answer">Short answer<a class="anchor-link" href="#Short-answer">¶</a></h3><p>The widget <code>nbinteract.short_answer</code> takes a question and an answer. The answer can either be a string, a list of strings, or a function that returns <code>True</code> when called with the user's input. If the function errors, an error message will be displayed.</p>
|
510
|
-
|
511
|
-
</div>
|
512
|
-
</div>
|
513
|
-
</div>
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
<div class="
|
518
|
-
cell border-box-sizing code_cell rendered">
|
519
|
-
<div class="input">
|
520
|
-
|
521
|
-
<div class="inner_cell">
|
522
|
-
<div class="input_area">
|
523
|
-
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">nbi</span><span class="o">.</span><span class="n">short_answer</span><span class="p">(</span><span class="s1">'What is 1+1?'</span><span class="p">,</span> <span class="n">answers</span><span class="o">=</span><span class="s1">'2'</span><span class="p">,</span> <span class="n">explanation</span><span class="o">=</span><span class="s1">'1+1 is 2'</span><span class="p">)</span>
|
524
|
-
</pre></div>
|
525
|
-
|
526
|
-
</div>
|
527
|
-
</div>
|
528
|
-
</div>
|
529
|
-
|
530
|
-
<div class="output_wrapper">
|
531
|
-
<div class="output">
|
532
|
-
|
533
|
-
|
534
|
-
<div class="output_area">
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
<div class="output_subarea output_widget_view ">
|
541
|
-
<button class="js-nbinteract-widget">
|
542
|
-
Loading widgets...
|
543
|
-
</button>
|
544
|
-
</div>
|
545
|
-
|
546
|
-
</div>
|
547
|
-
|
548
|
-
</div>
|
549
|
-
</div>
|
550
|
-
|
551
|
-
</div>
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
<div class="
|
556
|
-
cell border-box-sizing code_cell rendered">
|
557
|
-
<div class="input">
|
558
|
-
|
559
|
-
<div class="inner_cell">
|
560
|
-
<div class="input_area">
|
561
|
-
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">nbi</span><span class="o">.</span><span class="n">short_answer</span><span class="p">(</span><span class="s1">'Enter the first name of a member of the Beatles.'</span><span class="p">,</span>
|
562
|
-
<span class="p">[</span><span class="s1">'John'</span><span class="p">,</span> <span class="s1">'Paul'</span><span class="p">,</span> <span class="s1">'George'</span><span class="p">,</span> <span class="s1">'Ringo'</span><span class="p">])</span>
|
563
|
-
</pre></div>
|
564
|
-
|
565
|
-
</div>
|
566
|
-
</div>
|
567
|
-
</div>
|
568
|
-
|
569
|
-
<div class="output_wrapper">
|
570
|
-
<div class="output">
|
571
|
-
|
572
|
-
|
573
|
-
<div class="output_area">
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
<div class="output_subarea output_widget_view ">
|
580
|
-
<button class="js-nbinteract-widget">
|
581
|
-
Loading widgets...
|
582
|
-
</button>
|
583
|
-
</div>
|
584
|
-
|
585
|
-
</div>
|
586
|
-
|
587
|
-
</div>
|
588
|
-
</div>
|
589
|
-
|
590
|
-
</div>
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
<div class="
|
595
|
-
cell border-box-sizing code_cell rendered">
|
596
|
-
<div class="input">
|
597
|
-
|
598
|
-
<div class="inner_cell">
|
599
|
-
<div class="input_area">
|
600
|
-
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">nbi</span><span class="o">.</span><span class="n">short_answer</span><span class="p">(</span><span class="s1">'Enter an even number.'</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
|
601
|
-
</pre></div>
|
602
|
-
|
603
|
-
</div>
|
604
|
-
</div>
|
605
|
-
</div>
|
606
|
-
|
607
|
-
<div class="output_wrapper">
|
608
|
-
<div class="output">
|
609
|
-
|
610
|
-
|
611
|
-
<div class="output_area">
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
<div class="output_subarea output_widget_view ">
|
618
|
-
<button class="js-nbinteract-widget">
|
619
|
-
Loading widgets...
|
620
|
-
</button>
|
621
|
-
</div>
|
622
|
-
|
623
|
-
</div>
|
624
|
-
|
625
|
-
</div>
|
626
|
-
</div>
|
627
|
-
|
628
|
-
</div>
|
629
|
-
|
1
|
+
<div class="cell text_cell">
|
2
|
+
<button class="js-nbinteract-widget">
|
3
|
+
Loading widgets...
|
4
|
+
</button>
|
5
|
+
</div>
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
10
|
+
<div class="text_cell_render border-box-sizing rendered_html">
|
11
|
+
<h2 id="Widgets-from-j1-nbinteract">Widgets from j1-nbinteract<a class="anchor-link" href="#Widgets-from-j1-nbinteract">¶</a></h2><p>The library <em>j1-nbinteract</em> comes with a set of functions that produce Javascript-based plots designed for interaction. The package combines the <em>ipywidgets</em> library and the <em>bqplot</em>
|
12
|
+
plotting library to implement function-driven interfaces to interactive plotting.</p>
|
13
|
+
<p>The nbinteract plotting methods use <strong>ipywidgets</strong> to generate and display widgets, inferring the widget type as needed. When a user interacts with a widget, a Python callback updates the visualization <strong>without</strong> a complete rerender. This noticeably lowers visualization update time compared to using ipywidgets alone to render static images.</p>
|
14
|
+
|
15
|
+
</div>
|
16
|
+
</div>
|
17
|
+
</div>
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
<div class="
|
22
|
+
cell border-box-sizing code_cell rendered">
|
23
|
+
<div class="input">
|
24
|
+
|
25
|
+
<div class="inner_cell">
|
26
|
+
<div class="input_area">
|
27
|
+
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">nbinteract</span> <span class="k">as</span> <span class="nn">nbi</span>
|
28
|
+
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
29
|
+
<span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">arange</span>
|
30
|
+
</pre></div>
|
31
|
+
|
32
|
+
</div>
|
33
|
+
</div>
|
34
|
+
</div>
|
35
|
+
|
36
|
+
</div>
|
37
|
+
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
38
|
+
<div class="text_cell_render border-box-sizing rendered_html">
|
39
|
+
<h3 id="Histogram">Histogram<a class="anchor-link" href="#Histogram">¶</a></h3><p>The widget <code>nbinteract.nbinteract.hist</code> generates a histogram that allows interaction with the parameters for the response function. The widget takes in a single response function. The response function returns the array of numerical values that will be shown in the histogram. The hist function allows interaction with the response function's parameters by specifying them as keyword arguments in the same format as ipywidgets.interact. Any argument that can be used for ipywidgets.interact can be used for hist.</p>
|
40
|
+
|
41
|
+
</div>
|
42
|
+
</div>
|
43
|
+
</div>
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
<div class="
|
48
|
+
cell border-box-sizing code_cell rendered">
|
49
|
+
<div class="input">
|
50
|
+
|
51
|
+
<div class="inner_cell">
|
52
|
+
<div class="input_area">
|
53
|
+
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">hist_function</span><span class="p">(</span><span class="n">mean</span><span class="p">,</span> <span class="n">sd</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">1000</span><span class="p">):</span>
|
54
|
+
<span class="sd">'''</span>
|
55
|
+
<span class="sd"> Returns 1000 values picked at random from the normal</span>
|
56
|
+
<span class="sd"> distribution with the Mean value and SD given.</span>
|
57
|
+
<span class="sd"> '''</span>
|
58
|
+
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="n">mean</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="n">sd</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span>
|
59
|
+
|
60
|
+
|
61
|
+
<span class="n">options</span> <span class="o">=</span> <span class="p">{</span>
|
62
|
+
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'Histogram'</span><span class="p">,</span>
|
63
|
+
<span class="s1">'xlabel'</span><span class="p">:</span> <span class="s1">'Mean value (mean)'</span><span class="p">,</span>
|
64
|
+
<span class="s1">'ylabel'</span><span class="p">:</span> <span class="s1">'Standard Deviation (sd)'</span><span class="p">,</span>
|
65
|
+
<span class="s1">'bins'</span><span class="p">:</span> <span class="mi">10</span>
|
66
|
+
<span class="p">}</span>
|
67
|
+
|
68
|
+
<span class="n">layouts</span> <span class="o">=</span> <span class="p">{</span>
|
69
|
+
<span class="s1">'plot_height'</span><span class="p">:</span> <span class="s1">'480px'</span><span class="p">,</span>
|
70
|
+
<span class="s1">'plot_width'</span><span class="p">:</span> <span class="s1">'800px'</span><span class="p">,</span>
|
71
|
+
<span class="p">}</span>
|
72
|
+
|
73
|
+
<span class="n">hist_chart</span> <span class="o">=</span> <span class="n">nbi</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span>
|
74
|
+
<span class="n">hist_function</span><span class="p">,</span>
|
75
|
+
<span class="n">mean</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
|
76
|
+
<span class="n">sd</span><span class="o">=</span><span class="p">(</span><span class="mf">0.2</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">),</span>
|
77
|
+
<span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
|
78
|
+
<span class="n">layouts</span><span class="o">=</span><span class="n">layouts</span>
|
79
|
+
<span class="p">)</span>
|
80
|
+
|
81
|
+
<span class="n">hist_chart</span>
|
82
|
+
</pre></div>
|
83
|
+
|
84
|
+
</div>
|
85
|
+
</div>
|
86
|
+
</div>
|
87
|
+
|
88
|
+
<div class="output_wrapper">
|
89
|
+
<div class="output">
|
90
|
+
|
91
|
+
|
92
|
+
<div class="output_area">
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
<div class="output_subarea output_widget_view ">
|
99
|
+
<button class="js-nbinteract-widget">
|
100
|
+
Loading widgets...
|
101
|
+
</button>
|
102
|
+
</div>
|
103
|
+
|
104
|
+
</div>
|
105
|
+
|
106
|
+
</div>
|
107
|
+
</div>
|
108
|
+
|
109
|
+
</div>
|
110
|
+
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
111
|
+
<div class="text_cell_render border-box-sizing rendered_html">
|
112
|
+
<h3 id="Barchart">Barchart<a class="anchor-link" href="#Barchart">¶</a></h3><p>The widget <code>nbinteract.nbinteract.bar</code> generates an bar plot that allows interaction with the parameters for the response functions.</p>
|
113
|
+
<p>The first two arguments of bar are response functions that return the x and y-axis data arrays, respectively. Either argument can be arrays themselves. Arguments for the response functions must be passed in as keyword arguments to bar in the format expected by interact. The response function for the y-axis data will be called with the x-axis data as its first argument.</p>
|
114
|
+
<p>For example, in the bar plot below categories generates the categories to plot on the x-axis and heights generates the y-axis heights. The heights function uses the parameter xs which is the array of x-axis data points.</p>
|
115
|
+
|
116
|
+
</div>
|
117
|
+
</div>
|
118
|
+
</div>
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
<div class="
|
123
|
+
cell border-box-sizing code_cell rendered">
|
124
|
+
<div class="input">
|
125
|
+
|
126
|
+
<div class="inner_cell">
|
127
|
+
<div class="input_area">
|
128
|
+
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">x_fn</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
129
|
+
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
|
130
|
+
|
131
|
+
<span class="k">def</span> <span class="nf">y_fn</span><span class="p">(</span><span class="n">xs</span><span class="p">,</span> <span class="n">offset</span><span class="p">):</span>
|
132
|
+
<span class="k">return</span> <span class="n">xs</span> <span class="o">+</span> <span class="n">offset</span>
|
133
|
+
|
134
|
+
|
135
|
+
<span class="n">options</span> <span class="o">=</span> <span class="p">{</span>
|
136
|
+
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'Barchart'</span><span class="p">,</span>
|
137
|
+
<span class="s1">'xlabel'</span><span class="p">:</span> <span class="s1">'Number of bars (n)'</span><span class="p">,</span>
|
138
|
+
<span class="s1">'ylabel'</span><span class="p">:</span> <span class="s1">'Offset value'</span><span class="p">,</span>
|
139
|
+
<span class="s1">'animation_duration'</span><span class="p">:</span> <span class="mi">500</span><span class="p">,</span>
|
140
|
+
<span class="s1">'aspect_ratio'</span><span class="p">:</span> <span class="mf">1.778</span><span class="p">,</span>
|
141
|
+
<span class="s1">'ylim'</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span>
|
142
|
+
<span class="p">}</span>
|
143
|
+
|
144
|
+
<span class="n">layouts</span> <span class="o">=</span> <span class="p">{</span>
|
145
|
+
<span class="s1">'plot_height'</span><span class="p">:</span> <span class="s1">'480px'</span><span class="p">,</span>
|
146
|
+
<span class="s1">'plot_width'</span><span class="p">:</span> <span class="s1">'800px'</span><span class="p">,</span>
|
147
|
+
<span class="p">}</span>
|
148
|
+
|
149
|
+
<span class="n">bar_chart</span> <span class="o">=</span> <span class="n">nbi</span><span class="o">.</span><span class="n">bar</span><span class="p">(</span>
|
150
|
+
<span class="n">x_fn</span><span class="p">,</span>
|
151
|
+
<span class="n">y_fn</span><span class="p">,</span>
|
152
|
+
<span class="n">n</span><span class="o">=</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
|
153
|
+
<span class="n">offset</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
|
154
|
+
<span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
|
155
|
+
<span class="n">layouts</span><span class="o">=</span><span class="n">layouts</span>
|
156
|
+
<span class="p">)</span>
|
157
|
+
|
158
|
+
<span class="n">bar_chart</span>
|
159
|
+
</pre></div>
|
160
|
+
|
161
|
+
</div>
|
162
|
+
</div>
|
163
|
+
</div>
|
164
|
+
|
165
|
+
<div class="output_wrapper">
|
166
|
+
<div class="output">
|
167
|
+
|
168
|
+
|
169
|
+
<div class="output_area">
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
<div class="output_subarea output_widget_view ">
|
176
|
+
<button class="js-nbinteract-widget">
|
177
|
+
Loading widgets...
|
178
|
+
</button>
|
179
|
+
</div>
|
180
|
+
|
181
|
+
</div>
|
182
|
+
|
183
|
+
</div>
|
184
|
+
</div>
|
185
|
+
|
186
|
+
</div>
|
187
|
+
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
188
|
+
<div class="text_cell_render border-box-sizing rendered_html">
|
189
|
+
<h3 id="Interactive-Scatter-chart">Interactive Scatter chart<a class="anchor-link" href="#Interactive-Scatter-chart">¶</a></h3><p>The widget <code>nbinteract.scatter_drag</code> generates a scatter plot that allows interaction through clicking and dragging the points on the graph.</p>
|
190
|
+
<p>scatter_drag takes in two lists/arrays consisting of the x-coordinates and y-coordinates of the points to plot. It generates an interactive scatter plot where the points can be dragged by the user and a best fit line is updated automatically according to the placement of the points.</p>
|
191
|
+
<p>scatter_drag does not allow response functions as inputs.</p>
|
192
|
+
|
193
|
+
</div>
|
194
|
+
</div>
|
195
|
+
</div>
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
<div class="
|
200
|
+
cell border-box-sizing code_cell rendered">
|
201
|
+
<div class="input">
|
202
|
+
|
203
|
+
<div class="inner_cell">
|
204
|
+
<div class="input_area">
|
205
|
+
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x_points</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
206
|
+
<span class="n">y_points</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
207
|
+
|
208
|
+
|
209
|
+
<span class="n">options</span> <span class="o">=</span> <span class="p">{</span>
|
210
|
+
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'Interactive Scatter Plot'</span><span class="p">,</span>
|
211
|
+
<span class="s1">'animation_duration'</span><span class="p">:</span> <span class="mi">1000</span><span class="p">,</span>
|
212
|
+
<span class="s1">'xlabel'</span><span class="p">:</span> <span class="s1">'x-values'</span><span class="p">,</span>
|
213
|
+
<span class="s1">'ylabel'</span><span class="p">:</span> <span class="s1">'y-values'</span><span class="p">,</span>
|
214
|
+
<span class="s1">'xlim'</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
|
215
|
+
<span class="s1">'ylim'</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
|
216
|
+
<span class="p">}</span>
|
217
|
+
|
218
|
+
<span class="n">layouts</span> <span class="o">=</span> <span class="p">{</span>
|
219
|
+
<span class="s1">'plot_height'</span><span class="p">:</span> <span class="s1">'480px'</span><span class="p">,</span>
|
220
|
+
<span class="s1">'plot_width'</span><span class="p">:</span> <span class="s1">'800px'</span><span class="p">,</span>
|
221
|
+
<span class="p">}</span>
|
222
|
+
|
223
|
+
<span class="n">show_eqn</span> <span class="o">=</span> <span class="kc">False</span>
|
224
|
+
|
225
|
+
<span class="n">scatter_drag</span> <span class="o">=</span> <span class="n">nbi</span><span class="o">.</span><span class="n">scatter_drag</span><span class="p">(</span>
|
226
|
+
<span class="n">x_points</span><span class="p">,</span>
|
227
|
+
<span class="n">y_points</span><span class="p">,</span>
|
228
|
+
<span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
|
229
|
+
<span class="n">layouts</span><span class="o">=</span><span class="n">layouts</span>
|
230
|
+
<span class="p">)</span>
|
231
|
+
|
232
|
+
<span class="n">scatter_drag</span>
|
233
|
+
</pre></div>
|
234
|
+
|
235
|
+
</div>
|
236
|
+
</div>
|
237
|
+
</div>
|
238
|
+
|
239
|
+
<div class="output_wrapper">
|
240
|
+
<div class="output">
|
241
|
+
|
242
|
+
|
243
|
+
<div class="output_area">
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
<div class="output_subarea output_widget_view ">
|
250
|
+
<button class="js-nbinteract-widget">
|
251
|
+
Loading widgets...
|
252
|
+
</button>
|
253
|
+
</div>
|
254
|
+
|
255
|
+
</div>
|
256
|
+
|
257
|
+
</div>
|
258
|
+
</div>
|
259
|
+
|
260
|
+
</div>
|
261
|
+
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
262
|
+
<div class="text_cell_render border-box-sizing rendered_html">
|
263
|
+
<h3 id="Scatter-chart">Scatter chart<a class="anchor-link" href="#Scatter-chart">¶</a></h3><p>The scatter widget <code>nbinteract.scatter</code> generates a scatter plot that allows interaction with the parameters to the response functions. This is different from scatter_drag which facilitates interaction using click and drag actions.</p>
|
264
|
+
<p>The first two arguments of scatter are response functions that return the x and y-axis coordinates, respectively. Either argument can be arrays themselves. Arguments for the response functions must be passed in as keyword arguments to scatter in the format expected by interact. The response function for the y-coordinates will be called with the x-coordinates as its first argument.</p>
|
265
|
+
|
266
|
+
</div>
|
267
|
+
</div>
|
268
|
+
</div>
|
269
|
+
|
270
|
+
|
271
|
+
|
272
|
+
<div class="
|
273
|
+
cell border-box-sizing code_cell rendered">
|
274
|
+
<div class="input">
|
275
|
+
|
276
|
+
<div class="inner_cell">
|
277
|
+
<div class="input_area">
|
278
|
+
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">x_fn</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
279
|
+
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
|
280
|
+
|
281
|
+
<span class="k">def</span> <span class="nf">y_fn</span><span class="p">(</span><span class="n">xs</span><span class="p">):</span>
|
282
|
+
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">xs</span><span class="p">))</span>
|
283
|
+
|
284
|
+
|
285
|
+
<span class="n">options</span> <span class="o">=</span> <span class="p">{</span>
|
286
|
+
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'Scatter Plot'</span><span class="p">,</span>
|
287
|
+
<span class="s1">'marker'</span><span class="p">:</span> <span class="s1">'circle'</span><span class="p">,</span>
|
288
|
+
<span class="s1">'animation_duration'</span><span class="p">:</span> <span class="mi">1000</span><span class="p">,</span>
|
289
|
+
<span class="s1">'xlabel'</span><span class="p">:</span> <span class="s1">'x-values'</span><span class="p">,</span>
|
290
|
+
<span class="s1">'ylabel'</span><span class="p">:</span> <span class="s1">'y-values'</span><span class="p">,</span>
|
291
|
+
<span class="s1">'xlim'</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">),</span>
|
292
|
+
<span class="s1">'ylim'</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
|
293
|
+
<span class="p">}</span>
|
294
|
+
|
295
|
+
<span class="n">layouts</span> <span class="o">=</span> <span class="p">{</span>
|
296
|
+
<span class="s1">'plot_height'</span><span class="p">:</span> <span class="s1">'480px'</span><span class="p">,</span>
|
297
|
+
<span class="s1">'plot_width'</span><span class="p">:</span> <span class="s1">'800px'</span><span class="p">,</span>
|
298
|
+
<span class="p">}</span>
|
299
|
+
|
300
|
+
<span class="n">scatter_chart</span> <span class="o">=</span> <span class="n">nbi</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span>
|
301
|
+
<span class="n">x_fn</span><span class="p">,</span>
|
302
|
+
<span class="n">y_fn</span><span class="p">,</span>
|
303
|
+
<span class="n">n</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">200</span><span class="p">),</span>
|
304
|
+
<span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
|
305
|
+
<span class="n">layouts</span><span class="o">=</span><span class="n">layouts</span>
|
306
|
+
<span class="p">)</span>
|
307
|
+
|
308
|
+
<span class="n">scatter_chart</span>
|
309
|
+
</pre></div>
|
310
|
+
|
311
|
+
</div>
|
312
|
+
</div>
|
313
|
+
</div>
|
314
|
+
|
315
|
+
<div class="output_wrapper">
|
316
|
+
<div class="output">
|
317
|
+
|
318
|
+
|
319
|
+
<div class="output_area">
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
<div class="output_subarea output_widget_view ">
|
326
|
+
<button class="js-nbinteract-widget">
|
327
|
+
Loading widgets...
|
328
|
+
</button>
|
329
|
+
</div>
|
330
|
+
|
331
|
+
</div>
|
332
|
+
|
333
|
+
</div>
|
334
|
+
</div>
|
335
|
+
|
336
|
+
</div>
|
337
|
+
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
338
|
+
<div class="text_cell_render border-box-sizing rendered_html">
|
339
|
+
<h3 id="Line-chart">Line chart<a class="anchor-link" href="#Line-chart">¶</a></h3><p>The line widget <code>nbinteract.line</code> generates a line plot that allows interaction with the parameters to the response functions.</p>
|
340
|
+
<p>The first two arguments of line are response functions that return the x and y-axis coordinates, respectively. Either argument can be arrays themselves. Arguments for the response functions must be passed in as keyword arguments to line in the format expected by interact. The response function for the y-coordinates will be called with the x-coordinates as its first argument.</p>
|
341
|
+
|
342
|
+
</div>
|
343
|
+
</div>
|
344
|
+
</div>
|
345
|
+
|
346
|
+
|
347
|
+
|
348
|
+
<div class="
|
349
|
+
cell border-box-sizing code_cell rendered">
|
350
|
+
<div class="input">
|
351
|
+
|
352
|
+
<div class="inner_cell">
|
353
|
+
<div class="input_area">
|
354
|
+
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">x_fn</span><span class="p">(</span><span class="nb">max</span><span class="p">):</span>
|
355
|
+
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
|
356
|
+
|
357
|
+
<span class="k">def</span> <span class="nf">y_fn</span><span class="p">(</span><span class="n">xs</span><span class="p">,</span> <span class="n">sd</span><span class="p">):</span>
|
358
|
+
<span class="k">return</span> <span class="n">xs</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="n">sd</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">xs</span><span class="p">))</span>
|
359
|
+
|
360
|
+
|
361
|
+
<span class="n">options</span> <span class="o">=</span> <span class="p">{</span>
|
362
|
+
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'Line chart'</span><span class="p">,</span>
|
363
|
+
<span class="s1">'xlabel'</span><span class="p">:</span> <span class="s1">'x-values (max)'</span><span class="p">,</span>
|
364
|
+
<span class="s1">'ylabel'</span><span class="p">:</span> <span class="s1">'y-values (sd)'</span><span class="p">,</span>
|
365
|
+
<span class="s1">'xlim'</span><span class="p">:</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">50</span><span class="p">),</span>
|
366
|
+
<span class="s1">'ylim'</span><span class="p">:</span> <span class="p">(</span><span class="o">-</span><span class="mi">20</span><span class="p">,</span> <span class="mi">70</span><span class="p">),</span>
|
367
|
+
<span class="s1">'animation_duration'</span><span class="p">:</span> <span class="mi">500</span>
|
368
|
+
<span class="p">}</span>
|
369
|
+
|
370
|
+
<span class="n">layouts</span> <span class="o">=</span> <span class="p">{</span>
|
371
|
+
<span class="s1">'plot_height'</span><span class="p">:</span> <span class="s1">'480px'</span><span class="p">,</span>
|
372
|
+
<span class="s1">'plot_width'</span><span class="p">:</span> <span class="s1">'800px'</span><span class="p">,</span>
|
373
|
+
<span class="p">}</span>
|
374
|
+
|
375
|
+
<span class="n">line_chart</span> <span class="o">=</span> <span class="n">nbi</span><span class="o">.</span><span class="n">line</span><span class="p">(</span>
|
376
|
+
<span class="n">x_fn</span><span class="p">,</span>
|
377
|
+
<span class="n">y_fn</span><span class="p">,</span>
|
378
|
+
<span class="nb">max</span><span class="o">=</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">50</span><span class="p">),</span>
|
379
|
+
<span class="n">sd</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
|
380
|
+
<span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
|
381
|
+
<span class="n">layouts</span><span class="o">=</span><span class="n">layouts</span>
|
382
|
+
<span class="p">)</span>
|
383
|
+
|
384
|
+
<span class="n">line_chart</span>
|
385
|
+
</pre></div>
|
386
|
+
|
387
|
+
</div>
|
388
|
+
</div>
|
389
|
+
</div>
|
390
|
+
|
391
|
+
<div class="output_wrapper">
|
392
|
+
<div class="output">
|
393
|
+
|
394
|
+
|
395
|
+
<div class="output_area">
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
<div class="output_subarea output_widget_view ">
|
402
|
+
<button class="js-nbinteract-widget">
|
403
|
+
Loading widgets...
|
404
|
+
</button>
|
405
|
+
</div>
|
406
|
+
|
407
|
+
</div>
|
408
|
+
|
409
|
+
</div>
|
410
|
+
</div>
|
411
|
+
|
412
|
+
</div>
|
413
|
+
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
414
|
+
<div class="text_cell_render border-box-sizing rendered_html">
|
415
|
+
<h2 id="Interactive-Questions">Interactive Questions<a class="anchor-link" href="#Interactive-Questions">¶</a></h2><p>nbinteract also provides built-in methods to easily create multiple choice and short answer questions.</p>
|
416
|
+
|
417
|
+
</div>
|
418
|
+
</div>
|
419
|
+
</div>
|
420
|
+
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
421
|
+
<div class="text_cell_render border-box-sizing rendered_html">
|
422
|
+
<h3 id="Multiple-choice">Multiple choice<a class="anchor-link" href="#Multiple-choice">¶</a></h3><p>The widget <code>nbinteract.multiple_choice</code> takes a question, a list of possible answer choices, and the correct answer. Clicking the buttons shows whether the choice was correct.</p>
|
423
|
+
|
424
|
+
</div>
|
425
|
+
</div>
|
426
|
+
</div>
|
427
|
+
|
428
|
+
|
429
|
+
|
430
|
+
<div class="
|
431
|
+
cell border-box-sizing code_cell rendered">
|
432
|
+
<div class="input">
|
433
|
+
|
434
|
+
<div class="inner_cell">
|
435
|
+
<div class="input_area">
|
436
|
+
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">nbi</span><span class="o">.</span><span class="n">multiple_choice</span><span class="p">(</span><span class="n">question</span><span class="o">=</span><span class="s2">"What is 10 + 2 * 5?"</span><span class="p">,</span>
|
437
|
+
<span class="n">choices</span><span class="o">=</span><span class="p">[</span><span class="s1">'12'</span><span class="p">,</span> <span class="s1">'60'</span><span class="p">,</span> <span class="s1">'20'</span><span class="p">],</span>
|
438
|
+
<span class="n">answers</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
|
439
|
+
</pre></div>
|
440
|
+
|
441
|
+
</div>
|
442
|
+
</div>
|
443
|
+
</div>
|
444
|
+
|
445
|
+
<div class="output_wrapper">
|
446
|
+
<div class="output">
|
447
|
+
|
448
|
+
|
449
|
+
<div class="output_area">
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
<div class="output_subarea output_widget_view ">
|
456
|
+
<button class="js-nbinteract-widget">
|
457
|
+
Loading widgets...
|
458
|
+
</button>
|
459
|
+
</div>
|
460
|
+
|
461
|
+
</div>
|
462
|
+
|
463
|
+
</div>
|
464
|
+
</div>
|
465
|
+
|
466
|
+
</div>
|
467
|
+
|
468
|
+
|
469
|
+
|
470
|
+
<div class="
|
471
|
+
cell border-box-sizing code_cell rendered">
|
472
|
+
<div class="input">
|
473
|
+
|
474
|
+
<div class="inner_cell">
|
475
|
+
<div class="input_area">
|
476
|
+
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">nbi</span><span class="o">.</span><span class="n">multiple_choice</span><span class="p">(</span><span class="n">question</span><span class="o">=</span><span class="s2">"Select all prime numbers."</span><span class="p">,</span>
|
477
|
+
<span class="n">choices</span><span class="o">=</span><span class="p">[</span><span class="s1">'12'</span><span class="p">,</span> <span class="s1">'3'</span><span class="p">,</span> <span class="s1">'31'</span><span class="p">],</span>
|
478
|
+
<span class="n">answers</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>
|
479
|
+
</pre></div>
|
480
|
+
|
481
|
+
</div>
|
482
|
+
</div>
|
483
|
+
</div>
|
484
|
+
|
485
|
+
<div class="output_wrapper">
|
486
|
+
<div class="output">
|
487
|
+
|
488
|
+
|
489
|
+
<div class="output_area">
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
<div class="output_subarea output_widget_view ">
|
496
|
+
<button class="js-nbinteract-widget">
|
497
|
+
Loading widgets...
|
498
|
+
</button>
|
499
|
+
</div>
|
500
|
+
|
501
|
+
</div>
|
502
|
+
|
503
|
+
</div>
|
504
|
+
</div>
|
505
|
+
|
506
|
+
</div>
|
507
|
+
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
|
508
|
+
<div class="text_cell_render border-box-sizing rendered_html">
|
509
|
+
<h3 id="Short-answer">Short answer<a class="anchor-link" href="#Short-answer">¶</a></h3><p>The widget <code>nbinteract.short_answer</code> takes a question and an answer. The answer can either be a string, a list of strings, or a function that returns <code>True</code> when called with the user's input. If the function errors, an error message will be displayed.</p>
|
510
|
+
|
511
|
+
</div>
|
512
|
+
</div>
|
513
|
+
</div>
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
<div class="
|
518
|
+
cell border-box-sizing code_cell rendered">
|
519
|
+
<div class="input">
|
520
|
+
|
521
|
+
<div class="inner_cell">
|
522
|
+
<div class="input_area">
|
523
|
+
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">nbi</span><span class="o">.</span><span class="n">short_answer</span><span class="p">(</span><span class="s1">'What is 1+1?'</span><span class="p">,</span> <span class="n">answers</span><span class="o">=</span><span class="s1">'2'</span><span class="p">,</span> <span class="n">explanation</span><span class="o">=</span><span class="s1">'1+1 is 2'</span><span class="p">)</span>
|
524
|
+
</pre></div>
|
525
|
+
|
526
|
+
</div>
|
527
|
+
</div>
|
528
|
+
</div>
|
529
|
+
|
530
|
+
<div class="output_wrapper">
|
531
|
+
<div class="output">
|
532
|
+
|
533
|
+
|
534
|
+
<div class="output_area">
|
535
|
+
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
|
540
|
+
<div class="output_subarea output_widget_view ">
|
541
|
+
<button class="js-nbinteract-widget">
|
542
|
+
Loading widgets...
|
543
|
+
</button>
|
544
|
+
</div>
|
545
|
+
|
546
|
+
</div>
|
547
|
+
|
548
|
+
</div>
|
549
|
+
</div>
|
550
|
+
|
551
|
+
</div>
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
<div class="
|
556
|
+
cell border-box-sizing code_cell rendered">
|
557
|
+
<div class="input">
|
558
|
+
|
559
|
+
<div class="inner_cell">
|
560
|
+
<div class="input_area">
|
561
|
+
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">nbi</span><span class="o">.</span><span class="n">short_answer</span><span class="p">(</span><span class="s1">'Enter the first name of a member of the Beatles.'</span><span class="p">,</span>
|
562
|
+
<span class="p">[</span><span class="s1">'John'</span><span class="p">,</span> <span class="s1">'Paul'</span><span class="p">,</span> <span class="s1">'George'</span><span class="p">,</span> <span class="s1">'Ringo'</span><span class="p">])</span>
|
563
|
+
</pre></div>
|
564
|
+
|
565
|
+
</div>
|
566
|
+
</div>
|
567
|
+
</div>
|
568
|
+
|
569
|
+
<div class="output_wrapper">
|
570
|
+
<div class="output">
|
571
|
+
|
572
|
+
|
573
|
+
<div class="output_area">
|
574
|
+
|
575
|
+
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
<div class="output_subarea output_widget_view ">
|
580
|
+
<button class="js-nbinteract-widget">
|
581
|
+
Loading widgets...
|
582
|
+
</button>
|
583
|
+
</div>
|
584
|
+
|
585
|
+
</div>
|
586
|
+
|
587
|
+
</div>
|
588
|
+
</div>
|
589
|
+
|
590
|
+
</div>
|
591
|
+
|
592
|
+
|
593
|
+
|
594
|
+
<div class="
|
595
|
+
cell border-box-sizing code_cell rendered">
|
596
|
+
<div class="input">
|
597
|
+
|
598
|
+
<div class="inner_cell">
|
599
|
+
<div class="input_area">
|
600
|
+
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">nbi</span><span class="o">.</span><span class="n">short_answer</span><span class="p">(</span><span class="s1">'Enter an even number.'</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
|
601
|
+
</pre></div>
|
602
|
+
|
603
|
+
</div>
|
604
|
+
</div>
|
605
|
+
</div>
|
606
|
+
|
607
|
+
<div class="output_wrapper">
|
608
|
+
<div class="output">
|
609
|
+
|
610
|
+
|
611
|
+
<div class="output_area">
|
612
|
+
|
613
|
+
|
614
|
+
|
615
|
+
|
616
|
+
|
617
|
+
<div class="output_subarea output_widget_view ">
|
618
|
+
<button class="js-nbinteract-widget">
|
619
|
+
Loading widgets...
|
620
|
+
</button>
|
621
|
+
</div>
|
622
|
+
|
623
|
+
</div>
|
624
|
+
|
625
|
+
</div>
|
626
|
+
</div>
|
627
|
+
|
628
|
+
</div>
|
629
|
+
|