[英]How to wrap code/text in Jupyter notebooks
Find your configuration directory via jupyter --config-dir
(mine is ~/.jupyter
).通过
jupyter --config-dir
找到你的配置目录(我的是~/.jupyter
)。 Then edit or create nbconfig/notebook.json
to add the following:然后编辑或创建
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
In addition to Dan's answer, you can apply line wrapping for all cells (code or markdown) by specifying the top object as Cell.除了 Dan 的回答之外,您还可以通过将顶部对象指定为 Cell 来对所有单元格(代码或降价)应用换行。 Adding the code below to your
~/.jupyter/nbconfig/notebook.json
将以下代码添加到您的
~/.jupyter/nbconfig/notebook.json
{
"Cell": {
"cm_config": {
"lineWrapping": true
}
}
}
Ex: This is my cell config例如:这是我的单元格配置
{
"Cell": {
"cm_config": {
"lineNumbers": false,
"lineWrapping": true
}
}
}
Easiest for me was this, straightforward and does not require a pip install:对我来说最简单的是这个,简单明了,不需要 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
This may not be as satisfactory of an answer but while working Google Colab, I use the three single quote marks above and below the line of comments.这可能不是一个令人满意的答案,但在使用 Google Colab 时,我在评论行的上方和下方使用了三个单引号。 Once quote marks are in place, I can hit return where I see fit.
一旦引号到位,我可以在我认为合适的地方点击回车。
Original comment:原评论:
# Using the number of rows from the original concatenated dataframe and the trimmed dataframe, quantify the percent difference between the number of rows lost
Solution:解决方案:
''' Using the number of rows from the original concatenated dataframe and the trimmed dataframe, quantify the percent difference between the number of rows lost ''' ''' 使用原始连接数据帧和修剪数据帧的行数,量化丢失的行数之间的百分比差异 '''
I am working with Jupyter notebook ( .ipynb ) through VSC Visual Studio Code, and I did find out that setting line/word wrapping could be set as follows:我正在通过 VSC Visual Studio Code 使用 Jupyter notebook ( .ipynb ),我确实发现设置行/自动换行可以设置如下:
F1
F1
It works for code (Python cells).它适用于代码(Python 单元)。 Markdown cells work fine even without changing above setting.
即使不更改上述设置,Markdown 单元格也可以正常工作。
Since none of these solutions worked for me, I opted for a different approach and wrote a simple column-wrapping print function that you can use to manually guarantee that the lines of any string will remain in view, for simple output checking scenarios.由于这些解决方案都不适合我,我选择了一种不同的方法并编写了一个简单的列换行打印 function ,您可以使用它来手动保证任何字符串的行都将保留在视图中,用于简单的 output 检查场景。
def printwr( item, wrapCol=70 ):
""" wrap printing to column limit """
posit = 0
while True:
# if remaining legnth eq/less than wrapCol, print and rturn
if len(item[posit:]) <= wrapCol: print(item[posit:]); return
# else take wrapCol chars from last index
llim = posit+wrapCol+1
# if more than one item, drop last contiguous non-space sequence (word)
lineSpl = item[posit:llim].split(' ')
segment = ' '.join(lineSpl[:-1]) if len(lineSpl)>1 else lineSpl
# print segment and increment posit by length segment
posit += len(segment)+1
print(segment)
For example, for例如,对于
exampleStr = "populations tend to cluster in the foothills and periphery of the rugged Hindu Kush range; smaller groups are found in many of the country's interior valleys; in general, the east is more densely settled, while the south is sparsely populated"
printwr(exampleStr)
produces:产生:
populations tend to cluster in the foothills and periphery of the rugged Hindu Kush range;
人口倾向于聚集在崎岖的兴都库什山脉的山麓和外围; smaller groups are found in many of the country's interior valleys;
在该国的许多内陆山谷中发现了较小的群体; in general, the east is more densely settled, while the south is sparsely populated
总体来说,东部人烟稠密,南部人烟稀少
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.这允许您将代码拆分为不同的行并帮助它看起来更漂亮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.