Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f811bce

Browse files
committedFeb 1, 2019
Update notebooks
1 parent 0191344 commit f811bce

File tree

5 files changed

+468
-198
lines changed

5 files changed

+468
-198
lines changed
 
Binary file not shown.
Loading
Loading
Lines changed: 418 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,418 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"[![xeus-cling](images/xeus-cling.png)](https://github.com/QuantStack/xeus-cling/)\n",
8+
"\n",
9+
"A Jupyter kernel for C++ based on the `cling` C++ interpreter and the `xeus` native implementation of the Jupyter protocol, xeus.\n",
10+
"\n",
11+
"- GitHub repository: https://github.com/QuantStack/xeus-cling/\n",
12+
"- Online documentation: https://xeus-cling.readthedocs.io/"
13+
]
14+
},
15+
{
16+
"cell_type": "markdown",
17+
"metadata": {},
18+
"source": [
19+
"## Usage\n",
20+
"\n",
21+
"<div style=\"background: #efffed;\n",
22+
" border: 1px solid grey;\n",
23+
" margin: 8px 0 8px 0;\n",
24+
" text-align: center;\n",
25+
" padding: 8px; \">\n",
26+
" <i class=\"fa-play fa\" \n",
27+
" style=\"font-size: 40px;\n",
28+
" line-height: 40px;\n",
29+
" margin: 8px;\n",
30+
" color: #444;\">\n",
31+
" </i>\n",
32+
" <div>\n",
33+
" To run the selected code cell, hit <pre style=\"background: #efffed\">Shift + Enter</pre>\n",
34+
" </div>\n",
35+
"</div>"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"## Output and error streams\n",
43+
"\n",
44+
"`std::cout` and `std::cerr` are redirected to the notebook frontend."
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"#include <iostream>\n",
54+
"\n",
55+
"std::cout << \"some output\" << std::endl;"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"std::cerr << \"some error\" << std::endl;"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"#include <stdexcept>"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"metadata": {},
80+
"outputs": [],
81+
"source": [
82+
"throw std::runtime_error(\"Unknown exception\");"
83+
]
84+
},
85+
{
86+
"cell_type": "markdown",
87+
"metadata": {},
88+
"source": [
89+
"Omitting the `;` in the last statement of a cell results in an output being printed"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": null,
95+
"metadata": {},
96+
"outputs": [],
97+
"source": [
98+
"int j = 5;"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": null,
104+
"metadata": {},
105+
"outputs": [],
106+
"source": [
107+
"j"
108+
]
109+
},
110+
{
111+
"cell_type": "markdown",
112+
"metadata": {},
113+
"source": [
114+
"## Documentation and completion\n",
115+
"\n",
116+
" - Documentation for types of the standard library is retrieved on cppreference.com.\n",
117+
" - The quick-help feature can also be enabled for user-defined types and third-party libraries. More documentation on this feature is available at https://xeus-cling.readthedocs.io/en/latest/inline_help.html.\n"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": null,
123+
"metadata": {},
124+
"outputs": [],
125+
"source": [
126+
"?std::vector"
127+
]
128+
},
129+
{
130+
"cell_type": "markdown",
131+
"metadata": {},
132+
"source": [
133+
"## Using the `display_data` mechanism"
134+
]
135+
},
136+
{
137+
"cell_type": "markdown",
138+
"metadata": {},
139+
"source": [
140+
"For a user-defined type `T`, the rich rendering in the notebook and JupyterLab can be enabled by by implementing the function `xeus::xjson mime_bundle_repr(const T& im)`, which returns the JSON mime bundle for that type.\n",
141+
"\n",
142+
"More documentation on the rich display system of Jupyter and Xeus-cling is available at https://xeus-cling.readthedocs.io/en/latest/rich_display.html"
143+
]
144+
},
145+
{
146+
"cell_type": "markdown",
147+
"metadata": {},
148+
"source": [
149+
"### Image example"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": null,
155+
"metadata": {},
156+
"outputs": [],
157+
"source": [
158+
"#include <string>\n",
159+
"#include <fstream>\n",
160+
"\n",
161+
"#include \"xtl/xbase64.hpp\"\n",
162+
"#include \"xeus/xjson.hpp\"\n",
163+
"\n",
164+
"namespace im\n",
165+
"{\n",
166+
" struct image\n",
167+
" { \n",
168+
" inline image(const std::string& filename)\n",
169+
" {\n",
170+
" std::ifstream fin(filename, std::ios::binary); \n",
171+
" m_buffer << fin.rdbuf();\n",
172+
" }\n",
173+
" \n",
174+
" std::stringstream m_buffer;\n",
175+
" };\n",
176+
" \n",
177+
" xeus::xjson mime_bundle_repr(const image& i)\n",
178+
" {\n",
179+
" auto bundle = xeus::xjson::object();\n",
180+
" bundle[\"image/png\"] = xtl::base64encode(i.m_buffer.str());\n",
181+
" return bundle;\n",
182+
" }\n",
183+
"}"
184+
]
185+
},
186+
{
187+
"cell_type": "code",
188+
"execution_count": null,
189+
"metadata": {},
190+
"outputs": [],
191+
"source": [
192+
"im::image marie(\"images/marie.png\");\n",
193+
"marie"
194+
]
195+
},
196+
{
197+
"cell_type": "markdown",
198+
"metadata": {},
199+
"source": [
200+
"### Audio example"
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": null,
206+
"metadata": {},
207+
"outputs": [],
208+
"source": [
209+
"#include <string>\n",
210+
"#include <fstream>\n",
211+
"\n",
212+
"#include \"xtl/xbase64.hpp\"\n",
213+
"#include \"xeus/xjson.hpp\"\n",
214+
"\n",
215+
"namespace au\n",
216+
"{\n",
217+
" struct audio\n",
218+
" { \n",
219+
" inline audio(const std::string& filename)\n",
220+
" {\n",
221+
" std::ifstream fin(filename, std::ios::binary); \n",
222+
" m_buffer << fin.rdbuf();\n",
223+
" }\n",
224+
" \n",
225+
" std::stringstream m_buffer;\n",
226+
" };\n",
227+
" \n",
228+
" xeus::xjson mime_bundle_repr(const audio& a)\n",
229+
" {\n",
230+
" auto bundle = xeus::xjson::object();\n",
231+
" bundle[\"text/html\"] =\n",
232+
" std::string(\"<audio controls=\\\"controls\\\"><source src=\\\"data:audio/wav;base64,\")\n",
233+
" + xtl::base64encode(a.m_buffer.str()) +\n",
234+
" \"\\\" type=\\\"audio/wav\\\" /></audio>\";\n",
235+
" return bundle;\n",
236+
" }\n",
237+
"}"
238+
]
239+
},
240+
{
241+
"cell_type": "code",
242+
"execution_count": null,
243+
"metadata": {},
244+
"outputs": [],
245+
"source": [
246+
"au::audio drums(\"audio/audio.wav\");\n",
247+
"drums"
248+
]
249+
},
250+
{
251+
"cell_type": "markdown",
252+
"metadata": {},
253+
"source": [
254+
"### Display"
255+
]
256+
},
257+
{
258+
"cell_type": "code",
259+
"execution_count": null,
260+
"metadata": {},
261+
"outputs": [],
262+
"source": [
263+
"#include \"xcpp/xdisplay.hpp\""
264+
]
265+
},
266+
{
267+
"cell_type": "code",
268+
"execution_count": null,
269+
"metadata": {},
270+
"outputs": [],
271+
"source": [
272+
"xcpp::display(drums);"
273+
]
274+
},
275+
{
276+
"cell_type": "markdown",
277+
"metadata": {},
278+
"source": [
279+
"### Update-display"
280+
]
281+
},
282+
{
283+
"cell_type": "code",
284+
"execution_count": null,
285+
"metadata": {},
286+
"outputs": [],
287+
"source": [
288+
"#include <string>\n",
289+
"#include \"xcpp/xdisplay.hpp\"\n",
290+
"\n",
291+
"namespace ht\n",
292+
"{\n",
293+
" struct html\n",
294+
" { \n",
295+
" inline html(const std::string& content)\n",
296+
" {\n",
297+
" m_content = content;\n",
298+
" }\n",
299+
" std::string m_content;\n",
300+
" };\n",
301+
"\n",
302+
" xeus::xjson mime_bundle_repr(const html& a)\n",
303+
" {\n",
304+
" auto bundle = xeus::xjson::object();\n",
305+
" bundle[\"text/html\"] = a.m_content;\n",
306+
" return bundle;\n",
307+
" }\n",
308+
"}\n",
309+
"\n",
310+
"// A red rectangle\n",
311+
"ht::html rect(R\"(\n",
312+
"<div style='\n",
313+
" width: 90px;\n",
314+
" height: 50px;\n",
315+
" line-height: 50px;\n",
316+
" background-color: blue;\n",
317+
" color: white;\n",
318+
" text-align: center;'>\n",
319+
"Original\n",
320+
"</div>)\");"
321+
]
322+
},
323+
{
324+
"cell_type": "code",
325+
"execution_count": null,
326+
"metadata": {},
327+
"outputs": [],
328+
"source": [
329+
"xcpp::display(rect, \"some_display_id\");"
330+
]
331+
},
332+
{
333+
"cell_type": "code",
334+
"execution_count": null,
335+
"metadata": {},
336+
"outputs": [],
337+
"source": [
338+
"// Update the rectangle to be blue\n",
339+
"rect.m_content = R\"(\n",
340+
"<div style='\n",
341+
" width: 90px;\n",
342+
" height: 50px;\n",
343+
" line-height: 50px;\n",
344+
" background-color: red;\n",
345+
" color: white;\n",
346+
" text-align: center;'>\n",
347+
"Updated\n",
348+
"</div>)\";\n",
349+
"\n",
350+
"xcpp::display(rect, \"some_display_id\", true);"
351+
]
352+
},
353+
{
354+
"cell_type": "markdown",
355+
"metadata": {},
356+
"source": [
357+
"## Magics\n",
358+
"\n",
359+
"Magics are special commands for the kernel that are not part of the C++ language.\n",
360+
"\n",
361+
"They are defined with the symbol `%` for a line magic and `%%` for a cell magic.\n",
362+
"\n",
363+
"More documentation for magics is available at https://xeus-cling.readthedocs.io/en/latest/magics.html."
364+
]
365+
},
366+
{
367+
"cell_type": "code",
368+
"execution_count": null,
369+
"metadata": {},
370+
"outputs": [],
371+
"source": [
372+
"#include <algorithm>\n",
373+
"#include <vector>"
374+
]
375+
},
376+
{
377+
"cell_type": "code",
378+
"execution_count": null,
379+
"metadata": {},
380+
"outputs": [],
381+
"source": [
382+
"std::vector<double> to_shuffle = {1, 2, 3, 4};"
383+
]
384+
},
385+
{
386+
"cell_type": "code",
387+
"execution_count": null,
388+
"metadata": {},
389+
"outputs": [],
390+
"source": [
391+
"%timeit std::random_shuffle(to_shuffle.begin(), to_shuffle.end());"
392+
]
393+
},
394+
{
395+
"cell_type": "code",
396+
"execution_count": null,
397+
"metadata": {},
398+
"outputs": [],
399+
"source": []
400+
}
401+
],
402+
"metadata": {
403+
"kernelspec": {
404+
"display_name": "C++14",
405+
"language": "C++14",
406+
"name": "xeus-cling-cpp14"
407+
},
408+
"language_info": {
409+
"codemirror_mode": "text/x-c++src",
410+
"file_extension": ".cpp",
411+
"mimetype": "text/x-c++src",
412+
"name": "c++",
413+
"version": "-std=c++14"
414+
}
415+
},
416+
"nbformat": 4,
417+
"nbformat_minor": 2
418+
}

‎2019-02-01-europandas/notebooks/xtensor.ipynb

Lines changed: 50 additions & 198 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.