Python 如何在从docker容器运行的jupyter笔记本中获取黑色代码格式?
我可以让Jupyter从docker容器中正常工作,甚至可以让Jupyter扩展从docker容器中正常工作(如果它们是的一部分),但是我不能让扩展从docker容器中正常工作 这是我正在尝试的。我有一个Python 如何在从docker容器运行的jupyter笔记本中获取黑色代码格式?,python,docker,jupyter,code-formatting,python-black,Python,Docker,Jupyter,Code Formatting,Python Black,我可以让Jupyter从docker容器中正常工作,甚至可以让Jupyter扩展从docker容器中正常工作(如果它们是的一部分),但是我不能让扩展从docker容器中正常工作 这是我正在尝试的。我有一个Dockerfile,看起来像这样: FROM python:3.8-slim-buster WORKDIR /usr/src/app RUN pip install black jupyter # Jupyter black installation as mentioned at the
Dockerfile
,看起来像这样:
FROM python:3.8-slim-buster
WORKDIR /usr/src/app
RUN pip install black jupyter
# Jupyter black installation as mentioned at the bottom of
# https://github.com/drillan/jupyter-black
RUN jupyter nbextension install https://github.com/drillan/jupyter-black/archive/master.zip --user
RUN jupyter nbextension enable jupyter-black-master/jupyter-black
# Add Tini. Tini operates as a process subreaper for jupyter. This prevents
# kernel crashes.
ENV TINI_VERSION v0.6.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/bin/tini
RUN chmod +x /usr/bin/tini
ENTRYPOINT ["/usr/bin/tini", "--"]
EXPOSE 8888
CMD ["jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--allow-root"]
从与Dockerfile
相同的目录中,我运行dockerbuild-t myjupyter
,然后docker run-p 8888:8888-it--rm myjupyter
,然后打开它提供的包含令牌的jupyter笔记本链接。当我打开一个新的笔记本电脑时,我希望看到这个黑色按钮,当我直接在我的机器上安装这个软件包时,我会看到这个按钮,但是当我从docker运行时,正如我所描述的,这个按钮不见了
从一个简单的docker容器中为jupyter笔记本启用黑色格式的最佳方法是什么?是否有一个完全不同的图书馆我应该考虑,或者只是一种不同的方式安装和启用我已经尝试过的库? < p>我想创建一个类似的设置。我使用Jupyter实验室和Jupyter实验室代码格式化程序,并将其配置为黑色。以下是我的基本步骤:
jupyter/scipy笔记本
Dockerfile
:
FROM jupyter/scipy-notebook
RUN jupyter labextension install @ryantam626/jupyterlab_code_formatter && \
pip install jupyterlab_code_formatter black && \
jupyter serverextension enable --py jupyterlab_code_formatter
这是我的docker compose.yml
:
version: '3'
services:
jupyter:
build: .
volumes:
- ./user-settings:/home/jovyan/.jupyter/lab/user-settings
- .:/home/jovyan/work
ports:
- 8888:8888
command: "start.sh jupyter lab --LabApp.token= --NotebookApp.notebook_dir=work"
这是必要的。要保持此设置,将使用第一个卷。按照其文档中的说明,通过UI进行配置,或者在您的主机上添加以下文件,该文件将在更改设置时创建:
user settings/@ryantam626/jupyterlab\u code\u formatter/settings.jupyterlab settings
{
"preferences": {
"default_formatter": {
"python": ["black"]
}
}
}
当您在用户设置
文件夹旁边有两个文件时,您可以运行docker compose up
(或docker compose up-d
和docker compose down
),打开http://localhost:8888/
在主机上,查看右侧的最后一个按钮以格式化代码: