notebook

security 安全 与 远程登录

  • 使用 https 协议
  • tutorials

  • commands

    1
    2
    3
    4
    5
    6
    7
    8
    
    # to change a password
    jupyter notebook password
    
    # to use https://
    # * generate a certificate
    openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mykey.key -out mycert.pem
    # * run with the certificate
    jupyter notebook --certfile=mycert.pem --keyfile mykey.key
  • to run notebook as a server to edit file ~/.jupyter/jupyter_notebook_config.py

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    # Set options for certfile, ip, password, and toggle off
    # browser auto-opening
    c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/fullchain.pem'
    c.NotebookApp.keyfile = u'/absolute/path/to/your/certificate/privkey.pem'
    # Set ip to '*' to bind on all interfaces (ips) for the public server
    c.NotebookApp.ip = '*'
    c.NotebookApp.password = u'sha1:bcd259ccf...<your hashed password here>'
    c.NotebookApp.open_browser = False
    
    # It is a good idea to set a known, fixed port for server access
    c.NotebookApp.port = 9999
  • 解决配置正确,不能远程上网问题

jupyter c++

https://github.com/jupyter-xeus/xeus-cling

  • using cling

    • an interactive shell for c++, based on clang.
  • 注意

    • 完善的安装源只有 conda-forge 源

      • 但是,这个暂时不支持 windows
      • 有 gouarin 源,但是总是安装失败
  • 安装命令

    1
    
    conda deactivate; conda env remove -n cling; conda create -n cling python=3.7 ; conda activate cling; conda install -c conda-forge xeus-cling jupyter jupyterlab
  • 使用 gouarin

    1
    2
    3
    
    conda deactivate; conda env remove -n cling; conda create -n cling python=3.7 ; conda activate cling;
    conda install -c guarin xeus-cling  # 到这里就会失败
    conda install -c conda-froge jupyter jupyterlab

使用 docker

参考:

1
docker run --rm -p 8889:8888 ssarnev/xeus-cling-opencv-xwidgets:opencv_4.5.5

使用 cling 命令行,类似 python shell

xeus-python

1
2
3
conda install -c conda-forge xeus-python
# or
pip install xeus-python

ipython

autoreload 扩展

参考:

说明: importlib.reload 有时不好使,autoreload 扩展效果更好

  1. 调用方法

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    %load_ext autoreload            # 加载 autoreload
    
    %autoreload                     # reload 所有模块(单次)
    
    %autoreload 0                   # 停止使用 autoreload 功能
    
    %autoreload 1                   # 每次执行 ``code cell`` 时都做 reload,
                                    # 只是针对 %aimport 设置的模块
    
    %autoreload 2                   # 类似 autoreload 1, 但是不执行 %aimport 排除掉(exclude)的模块
    
    %autoreload 3                   # 贪婪,reload 所有已有模块,后续新代码,也会被自动加入reload 队列
    
    
    %aimport                        # list 列举手动设置的 autoreload
    
    
    %aimport mymodule, mymodule2    # 设置 %autoreload 1 会被reload 的模块
    %aimport -mymodule              # 设置 %autoreload 排除掉的模块

Jupyter Lite

参考:

特点:

  • 完全运行在浏览器中

依赖:

  • Pyodide

    • 通过 js 实现的 python 解释器

jupyter 中的 markdown 和 latex 语法

参考:

三种方法比较:

  1. IPython.display 不能嵌套使用,局限性大,不灵活
  2. ipywidgets

    • 使用灵活,功能全面
    • 不过对于大批量数据,渲染时间长,慢
  3. pandas 内置功能

    • 只要把对应文本使用 latex 语法整理好就可以
    • 简单高效,速度快

使用 IPython.display 库

1
2
3
4
5
6
from IPython.display import display, Math, Markdown

display(Math(r'Dims: {}x{}m \\ Area: {}m^2 \\ Volume: {}m^3'.format(a, round(b,2), P, V)))


display(Markdown(r'$x^2$'))

使用 ipywidgets 库

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import ipywidgets as widgets


# layouts
layouts = {col: widgets.Layout(flex_flow='column')
          for col in df.columns}


cols = []
for col in df.columns:
    col_elements = [widgets.Label(col)]
    col_elements += df[col].apply(
        lambda cell: widgets.Label(f'$${cell}$$') if isinstance(cell, str) else widgets.Label('NaN')
    ).tolist()

    col_box = widgets.Box(children=col_elements, layout=layouts[col])


    cols.append(col_box)


widgets.Box(children=cols, flex_flow='row')

直接使用 pandas

pandas 内部文本支持在 jupyter 中展示 latex 公式

1
2
3
4
import pandas as pd

df.loc[:, ['symbol', 'unit']].applymap(
    lambda cell: f'$${cell}$$' if isinstance(cell, str) else cell)