Giter Club home page Giter Club logo

pybind11-notebook's Introduction

Jupyter Notebook running pybind11 program

This document demonstrates how to run pybind11 programs in Jupyter notebook in the most simple way using Xeus-Cling kernel. Sample programs here include:

  • simple Hello world program
  • embedded module via PYBIND11_EMBEDDED_MODULE macro

Setup

conda install -c conda-forge xeus-cling
conda install -c conda-forge pybind11
conda install -c conda-forge notebook

Simple program

pybind11

The code

#pragma cling add_include_path("/home/jupyter/miniconda3/include/python3.9/")
#pragma cling add_library_path("/home/jupyter/miniconda3/lib")
#pragma cling load("python3.9")

#include <pybind11/embed.h>
#include <iostream>

namespace py = pybind11;

void py_print(py::object o)
{
    std::cout << py::str(o).cast<std::string>() << "\n";
};

py::scoped_interpreter python;

py::str s{"Hello world"};
py_print(s);

Embedded module

embed

The code

#pragma cling add_include_path("/home/jupyter/miniconda3/include/python3.9/")
#pragma cling add_library_path("/home/jupyter/miniconda3/lib")
#pragma cling load("python3.9")

#include <pybind11/embed.h>
#include <iostream>

namespace py = pybind11;

void py_print(py::object o)
{
    std::cout << py::str(o).cast<std::string>() << "\n";
};

PYBIND11_EMBEDDED_MODULE(hello, m)
{
    m.def("world", []() { return "Hello world"; });
}

py::scoped_interpreter python;

py_print(py::module::import("hello").attr("world")());

Remarks

  • The buildin function py::print() prints into the terminal where you're running jupyter notebook, hence using custom py_print(). Suggestions how to redirect output of py::print() into the notebook are welcomed.
  • Exception handling with catch(const py::error_already_set& e) sometimes works, sometimes not. Suggestions how to fix that are welcomed.
  • The compiler seems to compile in Release mode. Enabling Debug mode would be beneficial, but how?

Links

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.