於 Jupyter 中執行 C++

Seachaos
tree.rocks
Published in
4 min readMay 9, 2020

--

Jupyter Notebook 是一套基於網頁的互動程式,一般來說大家都是在上面編寫 Python, 實際上安裝 Cling 後也可以在 Jupyter 上面編寫 C++與 OpenCV。

本人已經將其打包成 Docker 並且測試成功可以在 amd64 ( Mac / Linux ) 與 Armv7 ( Raspberry Pi 樹莓派 ) 上執行

如下圖範例

C++ with OpenCV on Jupyter

此篇文章使用 Docker 來運行 ( 在電腦上直接安裝需要很多設定與編譯,如果有興趣的人歡迎留言討論,可另開文章教學,或是參考本人 GitHub內的 Dockerfile 指令)

先安裝 Docker

Docker 於 Linux / Mac / Windows / Raspberry Pi 均有提供安裝,在此不另做教學

複製專案

依照使用平台, x86/Amd 64 架構 :
https://github.com/Seachaos/docker-python-xeus-cling

Raspberry pi 樹莓派 / Armv7 架構 :
https://github.com/Seachaos/raspberry-jupyter-xeus-cling

範例:

git clone https://github.com/Seachaos/docker-python-xeus-cling.git

第一次執行

基本上 Docker 安裝完成, git clone 也完成,已經萬事俱備
首先執行 ./setup_notebook_password.sh 進行第一次 Jupyter Notebook 密碼設定
第一次執行 Docker 會去 pull image 回來,需要一點時間

docker 正在 pull juypter with c++ / opencv image
密碼設定範例

之後執行

之後每次執行只要使用 start_notebook.sh 這個 script 即可在本機電腦的 port 8888
( http://localhost:8888/ )

Jupyter Notebook 啟動成功
登入畫面需要輸入密碼

使用瀏覽器進入 Jupyter 並且選擇 C++14 或 C++11
( C++17 會有 kernel dead issue, 未解 )

就可以開始編寫程式了

如果需要細部設定可在 config.sh 找到 ( 例如 port / 筆記本儲存位置 )

使用 OpenCV C++

本人亦在 docker 預先載入可以在 Xeus-Cling 呈現影像的 header file
( 參考: https://github.com/Seachaos/opencv-cpp-for-xeus-cling )

只要使用 :

#include <jupyter/opencv>

OpenCV 的 imshow 就可以呈現在 Jupyter 網頁中

以上本人亦在 Google Compute Engine (GCE) 中試驗成功,使用 Ubuntu 18.04 LTS Image

之後有空再來說說怎麼從無到有安裝…

歡迎留言討論 : )

--

--