簡體   English   中英

如何在 Jupyter 筆記本中包裝代碼/文本

[英]How to wrap code/text in Jupyter notebooks

我正在使用 jupyter-notebooks 進行 python 編碼。 有沒有辦法將文本/代碼包裝在 jupyter 筆記本代碼單元中?

下面提供的圖片。

文字不換行

通過換行文本表示“文本如何在 MS-word 中換行”

通過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 ),我確實發現設置行/自動換行可以設置如下:

  1. F1
  2. 選擇首選項:打開設置 (UI)
  3. 開始輸入wrap
  4. 編輯器: Word Wrap控制彈出的行應該如何換行,更改為On

它適用於代碼(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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM