Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在从docker容器运行的jupyter笔记本中获取黑色代码格式?_Python_Docker_Jupyter_Code Formatting_Python Black - Fatal编程技术网

Python 如何在从docker容器运行的jupyter笔记本中获取黑色代码格式?

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

我可以让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 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实验室代码格式化程序,并将其配置为黑色。以下是我的基本步骤:

  • 提供了许多准备好的Docker图像,我总是使用
    jupyter/scipy笔记本
  • 安装其他库和黑盒
  • 配置Jupyter实验室代码格式
  • 添加Docker Compose以简化配置和运行
  • 这是我的
    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/
    在主机上,查看右侧的最后一个按钮以格式化代码: