Skip to main content

Jupyter kernel for C++ (cpp, cplusplus) with no external deps!

Project description

ipycpp

Simple and hackable jupyter kernel for running c++ (c plus plus) codes inside a python notebook.

Rather than providing a true interactive experience, this kernel would just extract the cpp code from the notebook, compile it on the fly and print the result.

Install with:

pip install ipycpp

The package itself is in pure python, but you have to install a cpp compiler, such as g++ in order to make it work

Usage

Note: you may find a complete example here, example.ipynb

First, you have to provide some configuration.

  • $$ipycpp_file: this is the location where your code would be extracted
  • $$ipycpp_build: this is the command ipycpp should use to compile your code
  • $$ipycpp_run: this is the command ipycpp should use to compile your code

Create a cell with:

// $$ipycpp_file: src/main.cpp
// $$ipycpp_build: g++ src/main.cpp -o bin/main
// $$ipycpp_run: bin/main


/* you may also declare global variables and include directives */

#include <stdio.h>

auto hello = "hello world!";

Then, you may add other cells with some shared functions, for example:

void printHelloWorld(){
	printf("%s\n", hello);
}

Finally, when you want to show some output value, you may declare the usual main function, just remembed to surround it with the special #ifdef IPYCPP_MAIN guard

#ifdef IPYCPP_MAIN
#define IPYCPP_MAIN_METHOD
int main(){
	printHelloWorld();
}
#endif

Keep in mind that the notebook is stateless. Everything will be recompiled EVERY time. If you change a variable inside a main function and then run another cell, the change will be overwritten.

Special formatting

You may display an image by printing the special tag $$$ipycppr_image$$$ followed by the image path. For example:

printf("$$$ipycppr_image$$$%s\n", "myfolder/myimage.png");

Authors and license

Main author: Luca Fabbian luca.fabbian.1999@gmail.com

Freely distributed under MIT license.

Feel free to open a github issue to report bugs or submit feature requests!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ipycpp-0.0.2.tar.gz (5.4 kB view hashes)

Uploaded Source

Built Distribution

ipycpp-0.0.2-py3-none-any.whl (5.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page