Python 如何在 Jupyter 笔记本中包装代码/文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36419342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:50:50  来源:igfitidea点击:

How to wrap code/text in Jupyter notebooks

pythonjupyter-notebook

提问by Anuj Gupta

I am using jupyter-notebooks for python coding. Is there a way to wrap text/code in a jupyter notebook code cell?

我正在使用 jupyter-notebooks 进行 python 编码。有没有办法在 jupyter notebook 代码单元中包装文本/代码?

Picture provided below.

下图提供。

Text not wrapping

文字不换行

By wrap text means "how text is wrapped in MS-word"

换行文本表示“文本如何在 MS-word 中换行”

回答by Dan

Find your configuration directory via jupyter --config-dir(mine is ~/.jupyter). Then edit or create nbconfig/notebook.jsonto add the following:

通过jupyter --config-dir(我的是~/.jupyter)找到您的配置目录。然后编辑或创建nbconfig/notebook.json以添加以下内容:

{
  "MarkdownCell": {
    "cm_config": {
      "lineWrapping": true
    }
  },
  "CodeCell": {
    "cm_config": {
      "lineWrapping": true
    }
  }
}

(If you have something else in it, ensure you have valid JSON with no trailing commas after }s.)

(如果您有其他内容,请确保您有有效的 JSON,}s后没有尾随逗号。)

Restart Jupyter and reload your notebook.

重新启动 Jupyter 并重新加载您的笔记本。

Source: https://github.com/jupyter/notebook/issues/106

来源:https: //github.com/jupyter/notebook/issues/106

回答by eden

In addition to Dan's answer, you can apply line wrapping for all cells(code or markdown) by specifying the top object as Cell. Adding the code below to your ~/.jupyter/nbconfig/notebook.json

除了 Dan 的回答之外,您还可以通过将顶部对象指定为 Cell 来为所有单元格(代码或降价)应用换行。将下面的代码添加到您的~/.jupyter/nbconfig/notebook.json

{
  "Cell": {
    "cm_config": {
      "lineWrapping": true
    }
  }
}

Ex: This is my cell config

例如:这是我的单元格配置

{
  "Cell": {
    "cm_config": {
      "lineNumbers": false,
      "lineWrapping": true
    }
  }
}

回答by Robert Ben Parkinson

Shortest Answer Ever

有史以来最短的答案

Try adding a ' \ ' in between the lines of code you need to split.

尝试在需要拆分的代码行之间添加一个“\”。

This allows you to split your code over different lines and helps it look prettier.

这允许您将代码拆分为不同的行并使其看起来更漂亮。