Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap code/text in Jupyter notebooks

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

Picture provided below.

Text not wrapping

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

like image 526
Anuj Gupta Avatar asked Apr 05 '16 07:04

Anuj Gupta


People also ask

How do you wrap text in Python?

wrap(text, width=70, **kwargs): This function wraps the input paragraph such that each line in the paragraph is at most width characters long. The wrap method returns a list of output lines. The returned list is empty if the wrapped output has no content.

How do I get beautify code in Jupyter notebook?

a keyboard shortcut for reformatting the current code-cell (default shortcut is Ctrl-L , can also be configured not to add the keyboard shortcut). a keyboard shortcut for reformatting the whole notebook (default shortcut is Ctrl-Shift-L , can also be configured not to add the keyboard shortcut).

What is code folding in Jupyter notebook?

This extension adds codefolding functionality from CodeMirror to a codecell. In edit mode, clicking on the triangle in the gutter (left margin of codecell) or typing the codefolding hotkey (default is Alt+F ), folds the code. In command mode, the folding hotkey relates to the first line of the codecell.

What is %% Writefile in Jupyter notebook?

%%writefile magic command in regular Python I just read what this does. It writes the contents of a Jupyter Cell to the specified file.


1 Answers

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

{   "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.)

Restart Jupyter and reload your notebook.

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

like image 68
Dan Avatar answered Sep 21 '22 18:09

Dan