如何在 Jupyter 笔记本中包装 code/text
How to wrap code/text in Jupyter notebooks
我正在使用 jupyter-notebooks 进行 python 编码。有没有办法将 text/code 包装在 jupyter notebook 代码单元中?
图片如下。
换行表示"how text is wrapped in MS-word"
通过jupyter --config-dir
找到你的配置目录(我的是~/.jupyter
)。然后编辑或创建 nbconfig/notebook.json
以添加以下内容:
{
"MarkdownCell": {
"cm_config": {
"lineWrapping": true
}
},
"CodeCell": {
"cm_config": {
"lineWrapping": true
}
}
}
(如果您还有其他内容,请确保您有有效的 JSON,并且在 }
之后没有尾随逗号。)
重新启动 Jupyter 并重新加载您的笔记本。
史上最短答案
尝试在需要拆分的代码行之间添加“\”。
这使您可以将代码拆分成不同的行并使其看起来更漂亮。
除了 Dan 的回答之外,您可以通过将顶部对象指定为 Cell 来为所有单元格应用换行(代码或降价)。将下面的代码添加到您的 ~/.jupyter/nbconfig/notebook.json
{
"Cell": {
"cm_config": {
"lineWrapping": true
}
}
}
例如:这是我的手机配置
{
"Cell": {
"cm_config": {
"lineNumbers": false,
"lineWrapping": true
}
}
}
这可能不是一个令人满意的答案,但在 Google Colab 工作时,我在评论行的上方和下方使用了三个单引号。一旦引号就位,我可以在我认为合适的地方点击 return。
原评论:
# Using the number of rows from the original concatenated dataframe and the trimmed dataframe, quantify the percent difference between the number of rows lost
解决方案:
'''
使用原始连接数据框中的行数和
修剪数据框,量化数量之间的百分比差异
行丢失
'''
这是解决方案的屏幕截图:
我正在通过 VSC Visual Studio 代码使用 Jupyter notebook (.ipynb),我确实发现可以设置 line/word 包装如下:
- 命中
F1
- 选择 首选项:打开设置 (UI)
- 开始输入 wrap
- 编辑器:Word Wrap 控制换行方式 弹出,改为On
它适用于代码(Python 个单元格)。即使不更改上述设置,Markdown 单元格也能正常工作。
对我来说最简单的是这个,简单明了,不需要 pip 安装:
from textwrap import wrap
long_str = 'I rip wrap unravel when I time travel, with beats in my head'
lines = wrap(long_str, 20) #wrap outputs a list of lines
print('\n'.join(lines)) #so join 'em with newline
#prints:
#I rip wrap unravel
#when I time travel,
#with beats in my
#head
我正在使用 jupyter-notebooks 进行 python 编码。有没有办法将 text/code 包装在 jupyter notebook 代码单元中?
图片如下。
换行表示"how text is wrapped in MS-word"
通过jupyter --config-dir
找到你的配置目录(我的是~/.jupyter
)。然后编辑或创建 nbconfig/notebook.json
以添加以下内容:
{
"MarkdownCell": {
"cm_config": {
"lineWrapping": true
}
},
"CodeCell": {
"cm_config": {
"lineWrapping": true
}
}
}
(如果您还有其他内容,请确保您有有效的 JSON,并且在 }
之后没有尾随逗号。)
重新启动 Jupyter 并重新加载您的笔记本。
史上最短答案
尝试在需要拆分的代码行之间添加“\”。
这使您可以将代码拆分成不同的行并使其看起来更漂亮。
除了 Dan 的回答之外,您可以通过将顶部对象指定为 Cell 来为所有单元格应用换行(代码或降价)。将下面的代码添加到您的 ~/.jupyter/nbconfig/notebook.json
{
"Cell": {
"cm_config": {
"lineWrapping": true
}
}
}
例如:这是我的手机配置
{
"Cell": {
"cm_config": {
"lineNumbers": false,
"lineWrapping": true
}
}
}
这可能不是一个令人满意的答案,但在 Google Colab 工作时,我在评论行的上方和下方使用了三个单引号。一旦引号就位,我可以在我认为合适的地方点击 return。
原评论:
# Using the number of rows from the original concatenated dataframe and the trimmed dataframe, quantify the percent difference between the number of rows lost
解决方案:
''' 使用原始连接数据框中的行数和 修剪数据框,量化数量之间的百分比差异 行丢失 '''
这是解决方案的屏幕截图:
我正在通过 VSC Visual Studio 代码使用 Jupyter notebook (.ipynb),我确实发现可以设置 line/word 包装如下:
- 命中
F1
- 选择 首选项:打开设置 (UI)
- 开始输入 wrap
- 编辑器:Word Wrap 控制换行方式 弹出,改为On
它适用于代码(Python 个单元格)。即使不更改上述设置,Markdown 单元格也能正常工作。
对我来说最简单的是这个,简单明了,不需要 pip 安装:
from textwrap import wrap
long_str = 'I rip wrap unravel when I time travel, with beats in my head'
lines = wrap(long_str, 20) #wrap outputs a list of lines
print('\n'.join(lines)) #so join 'em with newline
#prints:
#I rip wrap unravel
#when I time travel,
#with beats in my
#head