jupyter notebook安装/代码补全/支持golang 踩坑记

安装(不要用root)

安装anaconda3,然后ln -s bin目录下的jupyter命令到/usr/bin目录下

生成密码备用

ipython进入交互终端

In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:67c9e60bb8b6:xxxxxxxxxxxxxxxxxxxxxxxxx'

然后复制整个 sha1:67c9e60bb8b6:xxxxxxxxxxxxxxxxxxxxxxxxx

生成jupyter配置文件:

看一眼 ~/.jupyter/jupyter_notebook_config.py是否存在,默认情况下并不存在需要自行创建。使用jupyter notebook --generate-config命令生成配置文件:

设置配置文件,设置密码:

c.NotebookApp.ip='0.0.0.0'  #允许远程访问
c.NotebookApp.password = u'sha:ce...刚才复制的那个密文' 
c.NotebookApp.open_browser = False  #不打开浏览器
c.NotebookApp.port =8888           #可自行指定一个端口, 访问时使用该端口228行
c.NotebookApp.allow_origin = '*' # 允许跨域访问

参考自

https://www.cnblogs.com/wu-chao/p/8419889.html

配置代码自动提示

安装nbextensions

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

如果上面失败,可以试试:jupyter contrib nbextension install --user --skip-running-check

win和mac参考下面:

windows

  • 1:确定是已经安装好anaconda
  • 2:要在anaconda prompt模式下运行(jupyter notebook打开)
  • 3:pip install jupyter_contrib_nbextensions(安装成功后,关掉jupyter notebook及相关网页,并重新打开,观察是否安装成功)
  • 4:如果发现依旧失败,则可进行此步骤,注意(jupyter notebook关闭)            
    进行配置:jupyter contrib nbextension install --user --skip-running-check
  • 5:安装完成后,重新启动jupyter notebook,“Nbextensions”出现在导航栏中,勾选目录。

macos

  • 1:确定是已经安装好anaconda
  • 2:要在Mac终端下运行,如何找到终端(启动台-其他-终端)
  • 3:先安装nbextensions依次输入运行下面代码:
    pip install jupyter_contrib_nbextensions
    jupyter contrib nbextension install --user
  • 4:安装完成后,关终端,启动anaconda中的jupyter notebook,在主页中,可以看见Nbextensions标签页,选中Hinterland 就能使用代码补全了。
里面几个常用的插件
  • Table of Contents:更容易导航
  • Autopep8:轻轻一击就能获得简洁代码,相当于代码的格式化
  • variable inspector:跟踪你的工作空间
  • ExecuteTime:显示单元格的运行时间和耗时,后面在讲到magic里面有和他相似的内容功能

继续安装nbextensions_configurator

pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

如果提示缺少依赖,就使用pip安装对应依赖即可,conda环境不会报错。

重启jupyter,在弹出的主页面里,能看编辑-Nbextensions标签页,在这个页面里,勾选Hinterland即启用了代码自动补全

参考自

https://www.lefer.cn/posts/15473/

Notebook使用

  • 目录

  • 文件新建及导入导出

  • 菜单

  • 快捷键 [esc+h]
    jupyter常用的快捷键:
    选中单元格情况下:M----->markdown,Y------>code

    shift + Tab :查看方法中的参数

    双击D,删除当前的单元格(删除之后,Crtl + Z还原无效)

    插入一行:b(bellow),a(above)

    执行单元:Ctrl + Enter(运行本单元,选中本单元)

    shift + Enter(运行本单元,选中下一个单元)

    Alt + Enter(运行本单元,向下插入一个单元)

    更多魔法指令:%lsmagic查看

关于Magic命令

Notebook中的Magic开关

在jupyter的Code模式下,除了一般的python代码编辑外,jupyter还支持一些其他的编程语言,如R,Julia,bash等,不过需要使用magic单元。

  • 为实现一些快捷操作,提升效率。Notebook中提供了Magic开关,能极大得优化我们使用Notebook的体验。
  • Magic 开关分为两大类:%line magic(%表示magic只在本行有效) & %%cell magic(%%表示magic在整个cell单元有效)

例如:使用magic产生Linux下的shell环境,

%%bash
pwd && ls
## 表示一个可以修改内核选项ast_node_interactivity,使得jupyter对独占一行的所有变量都自动显示
%config ZMQInteractiveShell.ast_node_interactivity='all'
[In]
a = 2
n = 4
a
n
[Out]
2
4

查看代码运行时间

%time 一行
%%time 多行
%%timeit多行的平均时间

其他问题

  • 信任服务器笔记本需要点击件-信任代码(否则会404,无法执行),更多安全策略详见官方文档:

https://jupyter-notebook.readthedocs.io/en/latest/security.html#our-security-model

        location /api {
                proxy_pass http://xx.xx.xx.xx:xxxx;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
  • nginx还需要配置一个请求体大小,否则很容易超限:
    在http{}中配置一个狠的: client_max_body_size 50m;

golang支持

jupyter notebook支持多语言,golang语言的jupyter需要一个kernel支持: https://github.com/gopherdata/gophernotes

但是这个kernel又依赖zeromq,所以:

梦想这样666:

https://www.jianshu.com/p/c82a2a21afcc

实际有坑,这么干:

https://segmentfault.com/a/1190000015674501

安装zeromq:

https://github.com/zeromq/zeromq4-x/branches

流程:

  • 去下载zeromq
  • 然后解压,
  • configure 指定--prefix=/usr/local/zeromq4.0.8
  • make make install
  • 配置环境变量,让系统找到那个包export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/zeromq4.0.8/lib/pkgconfig
  • 配置系统参数,让系统可以找到so,看上面第二个博客就ok
posted @ 2019-02-28 16:45  大树的博客  Views(3273)  Comments(0Edit  收藏  举报