A Jupyter kernel for C++ based on the cling C++ interpreter and the xeus native implementation of the Jupyter protocol, xeus.

Usage

To run the selected code cell, hit
Shift + Enter

Output and error streams

std::cout and std::cerr are redirected to the notebook frontend.


In [ ]:
#include <iostream>

std::cout << "some output" << std::endl;

In [ ]:
std::cerr << "some error" << std::endl;

In [ ]:
#include <stdexcept>

In [ ]:
throw std::runtime_error("Unknown exception");

Omitting the ; in the last statement of a cell results in an output being printed


In [ ]:
int j = 5;

In [ ]:
j

Documentation and completion


In [ ]:
?std::vector

Using the display_data mechanism

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.

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

Image example


In [ ]:
#include <string>
#include <fstream>

#include "xtl/xbase64.hpp"
#include "xeus/xjson.hpp"

namespace im
{
    struct image
    {   
        inline image(const std::string& filename)
        {
            std::ifstream fin(filename, std::ios::binary);   
            m_buffer << fin.rdbuf();
        }
        
        std::stringstream m_buffer;
    };
    
    xeus::xjson mime_bundle_repr(const image& i)
    {
        auto bundle = xeus::xjson::object();
        bundle["image/png"] = xtl::base64encode(i.m_buffer.str());
        return bundle;
    }
}

In [ ]:
im::image marie("images/marie.png");
marie

Audio example


In [ ]:
#include <string>
#include <fstream>

#include "xtl/xbase64.hpp"
#include "xeus/xjson.hpp"

namespace au
{
    struct audio
    {   
        inline audio(const std::string& filename)
        {
            std::ifstream fin(filename, std::ios::binary);   
            m_buffer << fin.rdbuf();
        }
        
        std::stringstream m_buffer;
    };
    
    xeus::xjson mime_bundle_repr(const audio& a)
    {
        auto bundle = xeus::xjson::object();
        bundle["text/html"] =
           std::string("<audio controls=\"controls\"><source src=\"data:audio/wav;base64,")
           + xtl::base64encode(a.m_buffer.str()) +
            "\" type=\"audio/wav\" /></audio>";
        return bundle;
    }
}

In [ ]:
au::audio drums("audio/audio.wav");
drums

Display


In [ ]:
#include "xcpp/xdisplay.hpp"

In [ ]:
xcpp::display(drums);

Update-display


In [ ]:
#include <string>
#include "xcpp/xdisplay.hpp"

namespace ht
{
    struct html
    {   
        inline html(const std::string& content)
        {
            m_content = content;
        }
        std::string m_content;
    };

    xeus::xjson mime_bundle_repr(const html& a)
    {
        auto bundle = xeus::xjson::object();
        bundle["text/html"] = a.m_content;
        return bundle;
    }
}

// A red rectangle
ht::html rect(R"(
<div style='
    width: 90px;
    height: 50px;
    line-height: 50px;
    background-color: blue;
    color: white;
    text-align: center;'>
Original
</div>)");

In [ ]:
xcpp::display(rect, "some_display_id");

In [ ]:
// Update the rectangle to be blue
rect.m_content = R"(
<div style='
    width: 90px;
    height: 50px;
    line-height: 50px;
    background-color: red;
    color: white;
    text-align: center;'>
Updated
</div>)";

xcpp::display(rect, "some_display_id", true);

Magics

Magics are special commands for the kernel that are not part of the C++ language.

They are defined with the symbol % for a line magic and %% for a cell magic.

More documentation for magics is available at https://xeus-cling.readthedocs.io/en/latest/magics.html.


In [ ]:
#include <algorithm>
#include <vector>

In [ ]:
std::vector<double> to_shuffle = {1, 2, 3, 4};

In [ ]:
%timeit std::random_shuffle(to_shuffle.begin(), to_shuffle.end());

In [ ]: