![](/img/trans.png)
[英]VS Code Jupyter Notebooks - How to stop result text wrapping?
[英]How to wrap code/text in Jupyter notebooks
通過jupyter --config-dir
找到你的配置目錄(我的是~/.jupyter
)。 然后編輯或創建nbconfig/notebook.json
以添加以下內容:
{
"MarkdownCell": {
"cm_config": {
"lineWrapping": true
}
},
"CodeCell": {
"cm_config": {
"lineWrapping": true
}
}
}
(如果您有其他內容,請確保您有有效的 JSON,在}
s 之后沒有尾隨逗號。)
重新啟動 Jupyter 並重新加載您的筆記本。
來源: https ://github.com/jupyter/notebook/issues/106
除了 Dan 的回答之外,您還可以通過將頂部對象指定為 Cell 來對所有單元格(代碼或降價)應用換行。 將以下代碼添加到您的~/.jupyter/nbconfig/notebook.json
{
"Cell": {
"cm_config": {
"lineWrapping": true
}
}
}
例如:這是我的單元格配置
{
"Cell": {
"cm_config": {
"lineNumbers": false,
"lineWrapping": true
}
}
}
對我來說最簡單的是這個,簡單明了,不需要 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
這可能不是一個令人滿意的答案,但在使用 Google Colab 時,我在評論行的上方和下方使用了三個單引號。 一旦引號到位,我可以在我認為合適的地方點擊回車。
原評論:
# 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 Code 使用 Jupyter notebook ( .ipynb ),我確實發現設置行/自動換行可以設置如下:
F1
它適用於代碼(Python 單元)。 即使不更改上述設置,Markdown 單元格也可以正常工作。
由於這些解決方案都不適合我,我選擇了一種不同的方法並編寫了一個簡單的列換行打印 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)
例如,對於
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)
產生:
人口傾向於聚集在崎嶇的興都庫什山脈的山麓和外圍; 在該國的許多內陸山谷中發現了較小的群體; 總體來說,東部人煙稠密,南部人煙稀少
嘗試在需要拆分的代碼行之間添加“\”。
這允許您將代碼拆分為不同的行並幫助它看起來更漂亮。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.