Skip to content

Commit ac45e1f

Browse files
authored
Add xeus-cpp-lite specific demo for deployment example (#207)
* Add xeus-cpp-lite specific demo for deployment example
1 parent e6f4012 commit ac45e1f

File tree

2 files changed

+371
-1
lines changed

2 files changed

+371
-1
lines changed

.github/workflows/deploy-github-page.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ jobs:
7777
micromamba create -n xeus-lite-host jupyterlite-core
7878
micromamba activate xeus-lite-host
7979
python -m pip install jupyterlite-xeus jupyter_server
80-
jupyter lite build --XeusAddon.prefix=${{ env.PREFIX }} --contents notebooks --output-dir dist
80+
jupyter lite build --XeusAddon.prefix=${{ env.PREFIX }} --contents notebooks/xeus-cpp-lite-demo.ipynb --output-dir dist
8181
cp $PREFIX/bin/xcpp.data dist/extensions/@jupyterlite/xeus/static
8282
cp $PREFIX/lib/libclangCppInterOp.so dist/extensions/@jupyterlite/xeus/static
8383

notebooks/xeus-cpp-lite-demo.ipynb

Lines changed: 370 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,370 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "aa9d4f33-5bd8-42f2-918a-e654d4363577",
6+
"metadata": {},
7+
"source": [
8+
"<center>\n",
9+
" <h1>C++ kernel based on xeus</h1>\n",
10+
"</center>\n",
11+
"\n",
12+
"A Jupyter kernel for C++ based on the `CppInterOp`, a clang based C++ Interoperability Library and the `xeus` native implementation of the Jupyter protocol, xeus.\n",
13+
"\n",
14+
"- GitHub repository: https://github.com/compiler-research/xeus-cpp\n",
15+
"- Documentation: https://xeus-cpp.readthedocs.io/en/latest/"
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"id": "9af8aa44-7786-47b6-b94b-8579470acb3a",
21+
"metadata": {},
22+
"source": [
23+
"## Usage\n",
24+
"\n",
25+
"<div style=\"background: #efffed;\n",
26+
" border: 1px solid grey;\n",
27+
" margin: 8px 0 8px 0;\n",
28+
" text-align: center;\n",
29+
" padding: 8px; \">\n",
30+
" <i class=\"fa-play fa\" \n",
31+
" style=\"font-size: 40px;\n",
32+
" line-height: 40px;\n",
33+
" margin: 8px;\n",
34+
" color: #444;\">\n",
35+
" </i>\n",
36+
" <div>\n",
37+
" To run the selected code cell, hit <pre style=\"background: #efffed\">Shift + Enter</pre>\n",
38+
" </div>\n",
39+
"</div>"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"id": "be1c8c6c-3fbe-4f53-9deb-8496c43d26ad",
45+
"metadata": {},
46+
"source": [
47+
"## Output and error streams\n",
48+
"\n",
49+
"`std::cout` and `std::cerr` are redirected to the notebook frontend."
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 1,
55+
"id": "9bd7f767-6c22-4a1b-a1e2-cd4184fd0367",
56+
"metadata": {
57+
"vscode": {
58+
"languageId": "c++"
59+
}
60+
},
61+
"outputs": [
62+
{
63+
"name": "stdout",
64+
"output_type": "stream",
65+
"text": [
66+
"some output\n"
67+
]
68+
}
69+
],
70+
"source": [
71+
"#include <iostream>\n",
72+
"\n",
73+
"std::cout << \"some output\" << std::endl;"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 2,
79+
"id": "9622f20f-5925-4544-a97b-aada3a14209a",
80+
"metadata": {
81+
"vscode": {
82+
"languageId": "c++"
83+
}
84+
},
85+
"outputs": [
86+
{
87+
"name": "stderr",
88+
"output_type": "stream",
89+
"text": [
90+
"some error\n"
91+
]
92+
}
93+
],
94+
"source": [
95+
"std::cerr << \"some error\" << std::endl;"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 5,
101+
"id": "7af0c962-17dc-47d4-9772-b8a06e2bda3a",
102+
"metadata": {
103+
"vscode": {
104+
"languageId": "c++"
105+
}
106+
},
107+
"outputs": [
108+
{
109+
"name": "stdout",
110+
"output_type": "stream",
111+
"text": [
112+
"5\n"
113+
]
114+
}
115+
],
116+
"source": [
117+
"int j = 5;\n",
118+
"std::cout << j << std::endl;"
119+
]
120+
},
121+
{
122+
"cell_type": "markdown",
123+
"id": "526a7dba-4001-47d5-a116-95423118e100",
124+
"metadata": {},
125+
"source": [
126+
"# Interpreting the C++ programming language\n",
127+
"\n",
128+
"You can define functions, classes, templates, etc ..."
129+
]
130+
},
131+
{
132+
"cell_type": "markdown",
133+
"id": "e5b116ce-ced1-4aa4-b14e-ef7d2606202e",
134+
"metadata": {},
135+
"source": [
136+
"## Functions"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": 6,
142+
"id": "86b08f22-e16c-4eac-917d-ae6eeb7ec7cb",
143+
"metadata": {
144+
"vscode": {
145+
"languageId": "c++"
146+
}
147+
},
148+
"outputs": [],
149+
"source": [
150+
"double sqr(double a)\n",
151+
"{\n",
152+
" return a * a;\n",
153+
"}"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 7,
159+
"id": "5aff6711-54bf-4280-a496-c9f7c683eee5",
160+
"metadata": {
161+
"vscode": {
162+
"languageId": "c++"
163+
}
164+
},
165+
"outputs": [
166+
{
167+
"name": "stdout",
168+
"output_type": "stream",
169+
"text": [
170+
"6.25\n"
171+
]
172+
}
173+
],
174+
"source": [
175+
"double a = 2.5;\n",
176+
"double asqr = sqr(a);\n",
177+
"std::cout << asqr << std::endl;"
178+
]
179+
},
180+
{
181+
"cell_type": "markdown",
182+
"id": "5b3959b0-9dc7-41a4-bba1-e20abd0765f7",
183+
"metadata": {},
184+
"source": [
185+
"## Classes"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": 8,
191+
"id": "d981a53b-8185-49c5-8a30-02453cc1b9e9",
192+
"metadata": {
193+
"vscode": {
194+
"languageId": "c++"
195+
}
196+
},
197+
"outputs": [],
198+
"source": [
199+
"class Foo\n",
200+
"{\n",
201+
"public:\n",
202+
"\n",
203+
" virtual ~Foo() {}\n",
204+
" \n",
205+
" virtual void print(double value) const\n",
206+
" {\n",
207+
" std::cout << \"Foo value = \" << value << std::endl;\n",
208+
" }\n",
209+
"};"
210+
]
211+
},
212+
{
213+
"cell_type": "code",
214+
"execution_count": 9,
215+
"id": "195d513f-d5cb-4e3d-a6cb-ae99dfcd9aab",
216+
"metadata": {
217+
"vscode": {
218+
"languageId": "c++"
219+
}
220+
},
221+
"outputs": [
222+
{
223+
"name": "stdout",
224+
"output_type": "stream",
225+
"text": [
226+
"Foo value = 1.2\n"
227+
]
228+
}
229+
],
230+
"source": [
231+
"Foo bar;\n",
232+
"bar.print(1.2);"
233+
]
234+
},
235+
{
236+
"cell_type": "markdown",
237+
"id": "9ecc1588-cb6e-4676-bb16-b9938e980b06",
238+
"metadata": {},
239+
"source": [
240+
"## Polymorphism"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": 10,
246+
"id": "4df90bea-5c9e-462e-bd20-d80fd169b7b6",
247+
"metadata": {
248+
"vscode": {
249+
"languageId": "c++"
250+
}
251+
},
252+
"outputs": [],
253+
"source": [
254+
"class Bar : public Foo\n",
255+
"{\n",
256+
"public:\n",
257+
"\n",
258+
" virtual ~Bar() {}\n",
259+
" \n",
260+
" virtual void print(double value) const\n",
261+
" {\n",
262+
" std::cout << \"Bar value = \" << 2 * value << std::endl;\n",
263+
" }\n",
264+
"};"
265+
]
266+
},
267+
{
268+
"cell_type": "code",
269+
"execution_count": 11,
270+
"id": "f7dbbcc2-0f00-48eb-8bb9-94e871afa2a7",
271+
"metadata": {
272+
"vscode": {
273+
"languageId": "c++"
274+
}
275+
},
276+
"outputs": [
277+
{
278+
"name": "stdout",
279+
"output_type": "stream",
280+
"text": [
281+
"Bar value = 2.4\n"
282+
]
283+
}
284+
],
285+
"source": [
286+
"Foo* bar2 = new Bar;\n",
287+
"bar2->print(1.2);\n",
288+
"delete bar2;"
289+
]
290+
},
291+
{
292+
"cell_type": "markdown",
293+
"id": "094f4ca7-0aa5-4121-bfff-bf5db1d42c5d",
294+
"metadata": {},
295+
"source": [
296+
"## Templates"
297+
]
298+
},
299+
{
300+
"cell_type": "code",
301+
"execution_count": 12,
302+
"id": "0df4f3a5-25a1-4548-ba63-54887c770dad",
303+
"metadata": {
304+
"vscode": {
305+
"languageId": "c++"
306+
}
307+
},
308+
"outputs": [],
309+
"source": [
310+
"#include <typeinfo>\n",
311+
"\n",
312+
"template <class T>\n",
313+
"class FooT\n",
314+
"{\n",
315+
"public:\n",
316+
" \n",
317+
" explicit FooT(const T& t) : m_t(t) {}\n",
318+
" \n",
319+
" void print() const\n",
320+
" {\n",
321+
" std::cout << typeid(T).name() << \" m_t = \" << m_t << std::endl;\n",
322+
" }\n",
323+
" \n",
324+
"private:\n",
325+
" \n",
326+
" T m_t;\n",
327+
"};"
328+
]
329+
},
330+
{
331+
"cell_type": "code",
332+
"execution_count": 13,
333+
"id": "e7bcab70-b9db-409c-aa04-9c64b413e266",
334+
"metadata": {
335+
"vscode": {
336+
"languageId": "c++"
337+
}
338+
},
339+
"outputs": [
340+
{
341+
"name": "stdout",
342+
"output_type": "stream",
343+
"text": [
344+
"d m_t = 1.2\n"
345+
]
346+
}
347+
],
348+
"source": [
349+
"FooT<double> foot(1.2);\n",
350+
"foot.print();"
351+
]
352+
}
353+
],
354+
"metadata": {
355+
"kernelspec": {
356+
"display_name": "C++20",
357+
"language": "cpp",
358+
"name": "xcpp20"
359+
},
360+
"language_info": {
361+
"codemirror_mode": "text/x-c++src",
362+
"file_extension": ".cpp",
363+
"mimetype": "text/x-c++src",
364+
"name": "C++",
365+
"version": "20"
366+
}
367+
},
368+
"nbformat": 4,
369+
"nbformat_minor": 5
370+
}

0 commit comments

Comments
 (0)