Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple example Jupyter notebook (for Binder) #127

Merged
merged 8 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .binder/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
README.md
66 changes: 66 additions & 0 deletions .binder/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- mode: dockerfile -*-
# vi: set ft=dockerfile :

# TODO(eric.cousineau): Figure out how to make JupyterLab work with this setup:
# https://github.com/binder-examples/jupyterlab

# TODO(eric.cousineau): See if it's easier to use a conda-based workflow, or a
# simpler Docker base image, to use Eigen headers, rather than doing a custom
# Docker image:
# https://mybinder.readthedocs.io/en/latest/using/config_files.html

FROM ubuntu:18.04

ARG NB_USER=jovyan
ARG NB_UID=1000
ARG NB_GID=100
EXPOSE 7000/tcp
EXPOSE 8888/tcp

RUN export DEBIAN_FRONTEND=noninteractive \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
-o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confnew \
-o Dpkg::Use-Pty=0 \
locales \
python3-pip \
python3-setuptools \
wget \
&& rm -rf /var/lib/apt/lists/* \
&& locale-gen en_US.UTF-8

# Install later version of castxml.
# See README here: https://github.com/CastXML/CastXMLSuperbuild/tree/75ec9ef4ad48ddab605627d783bfdee57fd7bcbf
# This is v0.3.4 for Linux: https://data.kitware.com/#item/5ee7eb659014a6d84ec1f25c
RUN wget https://data.kitware.com/api/v1/file/5ee7eb659014a6d84ec1f25e/download -O /tmp/castxml.tar.gz \
&& tar xfz /tmp/castxml.tar.gz -C /usr/local --strip-components 1 \
&& rm /tmp/castxml.tar.gz

# Install common C++ libraries for experimenting. These are not necessary to
# use pygccxml itself.
RUN export DEBIAN_FRONTEND=noninteractive \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
-o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confnew \
-o Dpkg::Use-Pty=0 \
libeigen3-dev \
libstdc++-7-dev \
&& rm -rf /var/lib/apt/lists/*

RUN useradd -d "/home/$NB_USER" -G $NB_GID -mU -s /bin/bash "$NB_USER"
ENV HOME="/home/$NB_USER" \
LANG=en_US.UTF-8 \
LANGUAGE=en_US.UTF-8 \
LC_ALL=en_US.UTF-8 \
SHELL=/bin/bash \
USER="$NB_USER" \
PATH="/home/$NB_USER/.local/bin:/usr/local/bin:/usr/bin:/bin"

WORKDIR $HOME
RUN mkdir pygccxml
COPY ["/", "pygccxml/"]
RUN chown -R $NB_UID:$NB_GID \
"$HOME/pygccxml"
USER "$NB_USER"
RUN pip3 --no-cache-dir install -e ./pygccxml[examples]
CMD ["jupyter", "lab", "--ip", "0.0.0.0", "pygccxml/docs/examples/notebook/example.ipynb"]
30 changes: 30 additions & 0 deletions .binder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Docker Image for Binder

<!--
This is derived from the following setup:
https://github.com/RobotLocomotion/drake/tree/dc2a9394d/.binder
-->

*Note that due to Binder conventions, this directory MUST always be in the root
of the repository and named either `binder` or `.binder`. This image is NOT
intended for use by most developers or users.*

To create a Docker image and run a Docker container similar to those used by
[Binder](https://mybinder.org) for local debugging purposes, execute the
following `pull`, `build`, and `run` commands from the top level of this Git
EricCousineau-TRI marked this conversation as resolved.
Show resolved Hide resolved
repository:

```bash
cd pygccxml
EricCousineau-TRI marked this conversation as resolved.
Show resolved Hide resolved
docker build -f .binder/Dockerfile -t binder .
docker run --rm -it --name mybinder -p 8888:8888 binder
```

Copy and paste the URL (including the login token) that is displayed in the
terminal into the web browser of your choice.
EricCousineau-TRI marked this conversation as resolved.
Show resolved Hide resolved

To stop the running container, simply exit it from the terminal with Ctrl+C.

*Note*: If you want to test the Docker image with the current source tree
(without copying, so you can modify source files), add the arguments
`-v ${PWD}:/home/jovyan/pygccxml` to mount it directly.
EricCousineau-TRI marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions .dockerignore
14 changes: 14 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,23 @@ Documentation and examples
--------------------------

The documentation can be found `here <http://pygccxml.readthedocs.io>`_, examples can be found `here <http://pygccxml.readthedocs.io/en/master/examples.html>`_.
You can also run an example JupyterLab Notebook using Binder, or view it using
``nbviewer``:

..
Developers: See `.binder/README.md` for more information.

.. image:: https://mybinder.org/badge_logo.svg
:target: https://mybinder.org/v2/gh/EricCousineau-TRI/pygccxml/feature-py-notebook-example?urlpath=tree/pygccxml/docs/examples/notebook/
:alt: Binder
.. image:: https://img.shields.io/badge/view%20on-nbviewer-brightgreen.svg
:target: https://nbviewer.jupyter.org/github/EricCousineau-TRI/pygccxml/tree/feature-py-notebook-example/docs/examples/notebook/
:alt: nbviewer

If you want to know more about the API provided by pygccxml, read the `query interface <http://pygccxml.readthedocs.io/en/develop/query_interface.html>`_ document or the `API documentation <http://pygccxml.readthedocs.io/en/develop/apidocs/modules.html>`_.



A `FAQ <http://pygccxml.readthedocs.io/en/master/faq.html>`_ is also available and may answer some of your questions.

License
Expand Down
127 changes: 127 additions & 0 deletions docs/examples/notebook/example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example pygccxml notebook on Binder\n",
"\n",
"Running this notebook on Binder allows you to execute the code online:",
"<a target=\"_doc\" href=\"https://mybinder.org/v2/gh/EricCousineau-TRI/pygccxml/feature-py-notebook-example?urlpath=tree/pygccxml/docs/examples/notebook/example.ipynb\">\n",
" <img src=\"https://mybinder.org/badge_logo.svg\"/>\n",
"</a>\n",
"\n",
"Please note that provisioning may take about 1-2 minutes.\n",
"\n",
"\n",
"The following example shows an example usage of `pygccxml` on Ubuntu\n",
"Bionic, from within a Docker container, for a simple toy C++ API that\n",
"uses both `std::vector` and `Eigen::Matrix<>`. The code is defined inline."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pygccxml import declarations\n",
"from pygccxml import utils\n",
"from pygccxml import parser"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Find out the c++ parser. This should resolve to the castxml\n",
"# version installed in Docker.\n",
"generator_path, generator_name = utils.find_xml_generator()\n",
"\n",
"# Configure the xml generator\n",
"config = parser.xml_generator_configuration_t(\n",
" xml_generator_path=generator_path,\n",
" xml_generator=generator_name,\n",
" include_paths=[\"/usr/include/eigen3\"],\n",
" compiler_path=generator_path,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"code = r\"\"\"\n",
"#include <vector>\n",
"\n",
"#include <Eigen/Dense>\n",
"\n",
"namespace ns {\n",
"\n",
"template <typename T, typename U = int>\n",
"class ExampleClass {\n",
"public:\n",
" std::vector<T> make_std_vector() const;\n",
" Eigen::Matrix<U, 3, 3> make_matrix3();\n",
"};\n",
"\n",
"// Analyze concrete instantiations of the given class.\n",
"extern template class ExampleClass<int>;\n",
"extern template class ExampleClass<float, float>;\n",
"\n",
"} // namespace ns\n",
"\"\"\"\n",
"\n",
"(global_ns,) = parser.parse_string(code, config)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"ns = global_ns.namespace(\"ns\")\n",
"declarations.print_declarations([ns])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Retrieve an instantiation and show template parameters.\n",
"cls, = ns.classes('ExampleClass<float, float>')\n",
"declarations.templates.split(cls.name)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"sphinx",
"sphinx_rtd_theme",
}
requirements_examples = {
"jupyterlab",
EricCousineau-TRI marked this conversation as resolved.
Show resolved Hide resolved
}

setup(name="pygccxml",
version=version,
Expand All @@ -38,6 +41,7 @@
extras_require={
"test": list(requirements_test),
"docs": list(requirements_docs),
"examples": list(requirements_examples),
},
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down