{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\"Jupyter\n", "\n", "
A Jupyter kernel for C++ based on the C++ interpreter cling and the native implementation of the Jupyter protocol xeus.
\n", "\n", "
\n", " This live demo is powered by\n", "
\n", "
\"Binder
\n", "
\"Jupyter
\n", "
\"Cling
\n", "
\n", "
\n", "
\n", " \n", " \n", "
\n", " This live demo may not be runnable from behind certain corporate proxies that block the websocket protocol.\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Usage\n", "\n", "
\n", " \n", " \n", "
\n", " To run the selected code cell, hit
Shift + Enter
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Output and error streams\n", "\n", "`std::cout` and `std::cerr` are redirected to the notebook frontend." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "std::cout << \"some output\" << std::endl;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "std::cerr << \"some error\" << std::endl;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "throw std::runtime_error(\"Unknown exception\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Omitting the `;` in the last statement of a cell results in an output being printed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "int j = 5;" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "j" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Interpreting the C++ programming language\n", "\n", "`cling` has a broad support of the features of C++. You can define functions, classes, templates, etc ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "double sqr(double a)\n", "{\n", " return a * a;\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "double a = 2.5;\n", "double asqr = sqr(a);\n", "asqr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Classes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Foo\n", "{\n", "public:\n", "\n", " virtual ~Foo() {}\n", " \n", " virtual void print(double value) const\n", " {\n", " std::cout << \"Foo value = \" << value << std::endl;\n", " }\n", "};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Foo bar;\n", "bar.print(1.2);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Polymorphism" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Bar : public Foo\n", "{\n", "public:\n", "\n", " virtual ~Bar() {}\n", " \n", " virtual void print(double value) const\n", " {\n", " std::cout << \"Bar value = \" << 2 * value << std::endl;\n", " }\n", "};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Foo* bar2 = new Bar;\n", "bar2->print(1.2);\n", "delete bar2;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Templates" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "template \n", "class FooT\n", "{\n", "public:\n", " \n", " explicit FooT(const T& t) : m_t(t) {}\n", " \n", " void print() const\n", " {\n", " std::cout << typeid(T).name() << \" m_t = \" << m_t << std::endl;\n", " }\n", " \n", "private:\n", " \n", " T m_t;\n", "};\n", "\n", "template <>\n", "class FooT\n", "{\n", "public:\n", " \n", " explicit FooT(const int& t) : m_t(t) {}\n", " \n", " void print() const\n", " {\n", " std::cout << \"m_t = \" << m_t << std::endl;\n", " }\n", " \n", "private:\n", " \n", " int m_t;\n", "};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "FooT foot1(1.2);\n", "foot1.print();" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "FooT foot2(4);\n", "foot2.print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## C++11 / C++14 support" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Foo11\n", "{\n", "public:\n", " \n", " Foo11() { std::cout << \"Foo11 default constructor\" << std::endl; }\n", " Foo11(const Foo11&) { std::cout << \"Foo11 copy constructor\" << std::endl; }\n", " Foo11(Foo11&&) { std::cout << \"Foo11 move constructor\" << std::endl; }\n", "};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Foo11 f1;\n", "Foo11 f2(f1);\n", "Foo11 f3(std::move(f1));" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "std::vector v = { 1, 2, 3};\n", "auto iter = ++v.begin();\n", "v" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "*iter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... and also lambda, universal references, `decltype`, etc ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Documentation and completion" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "?std::vector" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Using the `display_data` mechanism" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For a user-defined type `T`, implementing the function `xeus::xjson mime_bundle_repr(const T* im)` returning the json mime bundle representation for that type enables the rich rendering in the notebook." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Image example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "#include \n", "\n", "#include \"xtl/xbase64.hpp\"\n", "#include \"xeus/xjson.hpp\"\n", "\n", "namespace im\n", "{\n", " struct image\n", " { \n", " inline image(const std::string& filename)\n", " {\n", " std::ifstream fin(filename, std::ios::binary); \n", " m_buffer << fin.rdbuf();\n", " }\n", " \n", " std::stringstream m_buffer;\n", " };\n", " \n", " xeus::xjson mime_bundle_repr(const image& i)\n", " {\n", " auto bundle = xeus::xjson::object();\n", " bundle[\"image/png\"] = xtl::base64encode(i.m_buffer.str());\n", " return bundle;\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "im::image marie(\"images/marie.png\");\n", "marie" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Audio example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "#include \n", "\n", "#include \"xtl/xbase64.hpp\"\n", "#include \"xeus/xjson.hpp\"\n", "\n", "namespace au\n", "{\n", " struct audio\n", " { \n", " inline audio(const std::string& filename)\n", " {\n", " std::ifstream fin(filename, std::ios::binary); \n", " m_buffer << fin.rdbuf();\n", " }\n", " \n", " std::stringstream m_buffer;\n", " };\n", " \n", " xeus::xjson mime_bundle_repr(const audio& a)\n", " {\n", " auto bundle = xeus::xjson::object();\n", " bundle[\"text/html\"] =\n", " std::string(\"\";\n", " return bundle;\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "au::audio drums(\"audio/audio.wav\");\n", "drums" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Display" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \"xcpp/xdisplay.hpp\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xcpp::display(drums);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Update-display" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "#include \"xcpp/xdisplay.hpp\"\n", "\n", "namespace ht\n", "{\n", " struct html\n", " { \n", " inline html(const std::string& content)\n", " {\n", " m_content = content;\n", " }\n", " std::string m_content;\n", " };\n", "\n", " xeus::xjson mime_bundle_repr(const html& a)\n", " {\n", " auto bundle = xeus::xjson::object();\n", " bundle[\"text/html\"] = a.m_content;\n", " return bundle;\n", " }\n", "}\n", "\n", "// A red rectangle\n", "ht::html rect(R\"(\n", "
\n", "Original\n", "
)\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xcpp::display(rect, \"some_display_id\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Update the rectangle to be blue\n", "rect.m_content = R\"(\n", "
\n", "Updated\n", "
)\";\n", "\n", "xcpp::display(rect, \"some_display_id\", true);" ] } ], "metadata": { "kernelspec": { "display_name": "C++14", "language": "C++14", "name": "xeus-cling-cpp14" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "c++", "version": "14" } }, "nbformat": 4, "nbformat_minor": 2 }