简体   繁体   English

如何在 Docker 中运行的 Jupyter notebook 中完成代码

[英]How to get code completion in a Jupyter notebook running in Docker

I followed the instructions on the official Tensorflow Docker hub to be able to use a GPU-ready docker image with an exposed port for local Jupyter notebook development;我遵循官方Tensorflow Docker hub上的说明,以便能够使用 GPU-ready docker 镜像和公开端口进行本地 Jupyter notebook 开发; including installation of the required nvidia-docker包括安装所需的nvidia-docker

I can import packages no problem, but code completion doesn't work when hitting tab.我可以导入包没问题,但点击选项卡时代码完成不起作用。 Eg:例如:

import tensorflow as tf       # works fine
tf. <tab>                     # nothing happens

I can use the code if I type I manually.如果我手动输入我可以使用代码。 I can execute the following:我可以执行以下操作:

import numpy as np            # no complaints
np.arange(0, 10)              # returns expected result

Interestingly, if I type tf.有趣的是,如果我输入tf. and hit shift-<double-tab> I get the usually full docstring:并点击shift-<double-tab>我得到通常完整的文档字符串:

在此处输入图片说明


I used the following command to pull the docker image and run it:我使用以下命令拉取 docker 镜像并运行它:

docker run -u $(id -u):$(id -g) -it --runtime=nvidia --rm \
    -v $(realpath ~/Documents/jupyter_notebooks):/tf/notebooks \
    -p 8888:8888 tensorflow/tensorflow:nightly-gpu-py3-jupyter

On top of binding a local folder to the container, this maps my user ID to the running container, so I am not working from root 's home folder (my docker installation is made to belong to root . I can open the notebook directory in the browser and start a new Python3 notebook, as usual.除了将本地文件夹绑定到容器之外,这会将我的用户 ID 映射到正在运行的容器,因此我不是从root的主文件夹工作(我的 docker 安装属于root 。我可以在浏览器并像往常一样启动一个新的 Python3 笔记本。

I am running Ubuntu 16.04, opening Jupyter notebooks in Chrome (I use vimium extension, but it is switched off and I restarted the container after figuring it might be messing with keyboard commands sent to Chrome).我正在运行 Ubuntu 16.04,在 Chrome 中打开 Jupyter 笔记本(我使用vimium扩展程序,但它已关闭,并在确定它可能与发送到 Chrome 的键盘命令混淆后重新启动容器)。

$  docker --version
Docker version 18.09.1, build 4c52b90

Wenmin Wu's tabnine plugin is definitely a better auto-completion service than the built-in.吴文敏的tabnine插件绝对是比内置更好的自动补全服务。

I want to point out that IPython uses Jedi to do auto-completion and some other magics.我想指出的是,IPython 使用 Jedi 来进行自动补全和其他一些魔法。 However, the compatibility between the duo is problematic every now and then.然而,两人之间的兼容性时不时出现问题。 Besides waiting for an IPython update, an easy fix is this magic:除了等待 IPython 更新之外,还有一个简单的修复方法:

%config IPCompleter.use_jedi=False

I came across a quick and easy solution here - just run one of the Jupyter magic commands:我在这里遇到了一个快速简单的解决方案- 只需运行一个 Jupyter 魔术命令:

%config IPCompleter.greedy=True

Also worked in a Jupyter notebook running within PyCharm.还在 PyCharm 中运行的 Jupyter 笔记本中工作。 As soon as I ran that command, PyCharm began indexing the project, meaning code completion worked in scripts as well as in the notebook (which also hadn't been working!)我一运行该命令,PyCharm 就开始为项目建立索引,这意味着代码完成在脚本和笔记本中都有效(这也没有奏效!)

It should be possible to add the config parameter to a global Jupyter config file.应该可以将 config 参数添加到全局 Jupyter 配置文件中。


There is a second possible solution on the same thread, using a notebook extension, but I haven't tested it.在同一个线程上有第二种可能的解决方案,使用笔记本扩展,但我没有测试过。

I wrote a plugin for jupyter notebook which provide code autocompletion based on deep learning model.我为 jupyter notebook 编写了一个插件,它提供基于深度学习模型的代码自动完成。 It's C/S model, and can be run everywhere.它是 C/S 模型,可以在任何地方运行。 You can try this: https://github.com/wenmin-wu/jupyter-tabnine This tool is now available on pypi .你可以试试这个: https : //github.com/wenmin-wu/jupyter-tabnine这个工具现在在pypi 上可用。 Simply issue following four lines command and enjoy it:)只需发出以下四行命令,并享受它:)

pip3 install jupyter-tabnine
jupyter nbextension install --py jupyter_tabnine
jupyter nbextension enable --py jupyter_tabnine
jupyter serverextension enable --py jupyter_tabnine

演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM