545

I would like to increase the width of the ipython notebook in my browser. I have a high-resolution screen, and I would like to expand the cell width/size to make use of this extra space.

Thanks!

vgoklani
  • 10,685
  • 16
  • 63
  • 101

15 Answers15

1033

If you don't want to change your default settings, and you only want to change the width of the current notebook you're working on, you can enter the following into a cell:

from IPython.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
MaxNoe
  • 14,470
  • 3
  • 41
  • 46
tonyslowdown
  • 14,069
  • 2
  • 25
  • 20
  • 33
    Nice. This looks like it only changes the code/markdown cells. Is there a way to also have it do the output cells? – dreyco676 Sep 01 '16 at 20:29
  • 16
    This solution worked for me ! I did not have to install any lib or package to use it. – Ancalagon BerenLuthien Sep 26 '16 at 19:43
  • 2
    I am using Jupyter for Julia. Do you know what I need in this case? – a06e Nov 06 '17 at 13:55
  • 3
    @becko according to [julia discourse](https://discourse.julialang.org/t/widen-display-in-ijulia-jupyter-notebook/12470/4?u=liso) this works: `display("text/html", "")` – Liso Jul 19 '18 at 04:38
  • This code does change the cell's width, but when I print a long list, the width of the list doesn't really span as wide as the cell. I could drag the Chrome to wider span, but the printed list still keeps its original width, not changing with the Chrome window. Could anyone help? – Jason Apr 24 '19 at 19:21
  • 1
    This works but stretches to the right, not to the left. How do I stretch it to the left? – mLstudent33 Dec 27 '19 at 20:00
  • 7
    Holly Molly this has changed my life to a better... Thank you so much!!! Been so tired of looking into that screwed 600px width box while having a 2k 30'' monitor!! – Anatoly Alekseev Mar 04 '20 at 15:21
  • Can this also be used to change the background color for the notebook? Would be useful to be able to change other properties like this, without having to change any profile files – vmg Mar 13 '20 at 11:36
  • what if I'm using an R kernel? – Cactus Philosopher Apr 12 '20 at 05:07
  • 11
    add `display(HTML(""))` to change the output cells width! – onofricamila Jun 12 '20 at 12:47
  • @onofricamila Do you also know how to change the height of the output columns? :) – gies0r Sep 02 '20 at 21:31
  • 1
    @gies0r I would try `max-height:100% !important`. You can writte all the CSS you want inside those curly braces. Just out of curiosity, why do you need that? To avoid vertical scrolling? – onofricamila Sep 03 '20 at 14:29
  • 3
    @onofricamila Thats the reason, yes. But `display(HTML(""))` **did not work for me**. – gies0r Sep 06 '20 at 16:26
  • For a JupyterLab solution, please see: [How to change the style/width of cells in JupyterLab? (answer)](https://stackoverflow.com/a/66278615/6646912) – krassowski Apr 19 '21 at 11:39
  • I wanted to thank you. These 2 lines of code, changed my life forever. – Abdul Mukit Sep 25 '21 at 18:13
  • Is there something to run in the command line to make this the default setting? –  Jan 10 '22 at 13:40
  • 1
    For IPython >= 7.14 use ```from IPython.display import display, HTML``` to avoid the deprecation warning. – glpsx Mar 31 '22 at 11:12
  • @glpsx: 'from IPython.core.display import display, HTML' does not generate such warnings on "ipython==7.20" with "ipython-genutils==0.2.0". Importing as you suggest seems totally ineffectual on my Linux platform ("notebook==6.4.6"). – Cbhihe Apr 01 '22 at 17:16
249

That div.cell solution didn't actually work on my IPython, however luckily someone suggested a working solution for new IPythons:

Create a file ~/.ipython/profile_default/static/custom/custom.css (iPython) or ~/.jupyter/custom/custom.css (Jupyter) with content

.container { width:100% !important; }

Then restart iPython/Jupyter notebooks. Note that this will affect all notebooks.

Renaud
  • 16,073
  • 6
  • 81
  • 79
Gere
  • 12,075
  • 18
  • 62
  • 94
  • 10
    These little snippets of information should be posted some place, thanks again! – vgoklani Oct 30 '14 at 00:23
  • Note this applies to *any* notebook. You just need to reopen the notebook. I've modified the answer to reflect this fact... – watsonic Apr 10 '15 at 23:19
  • This seems to affect all notebooks. How can I change it just for a particular notebook, without having a non-standard setup for developing and testing other notebooks? – nealmcb Nov 04 '15 at 15:21
  • Aha - thanks to @jjinking for a way to do this for just the current notebook – nealmcb Dec 19 '15 at 13:49
  • 15
    Starting IPython 4.1 custom folder location has changed to `~/.jupyter/custom/`. – Romain Jan 24 '16 at 06:50
  • 8
    I had to restart Jupyter notebook (4.1.0) for it to work. I put the above css in `~/.jupyter/custom/custom.css` – Paul May 12 '16 at 17:49
  • @pavopax solution works for me. Someone with enough permissions should edit it to the answer. – Martín Fixman Jun 16 '16 at 15:36
  • Creating a ~/.jupyter/custom/custom.css file with .container { width:100% !important; } also works well with Jupyter 4.3.1. Many thanks – tagoma Feb 13 '17 at 11:27
  • By the same token, one can gain a little more space by reducing the width of the (call them) prompt boxes on the left. In ~/.jupyter/custom/custom.css gile, just add .prompt {min-width: 0ex; } – tagoma Feb 13 '17 at 11:43
  • 12
    100% doesn't look very nice, I changed it to 90% – liang Mar 30 '17 at 10:37
  • How do I "restart" Juypter when it's remote on a unix box? – yeliabsalohcin May 16 '17 at 10:20
  • Not sure why you want to restart or how to do it. But if you want to update the style, I believe a browser page refresh should do. – Gere May 16 '17 at 16:51
  • one way to center e.g. the 90%-wide cells on the page `.container { width:90% !important; margin-left:5% !important; }` although I'm sure there is a better way – gevra Jun 27 '17 at 14:37
  • 1
    As to restarting a Notebook (as @yeliabsalohcin was asking), what I had to do for this to work is go to 'Control Panel' in the right upper corner. Het 'Stop My Server' , wait a while, and your notebooks should load again (in the new wide style!). – Tom Apr 04 '18 at 09:52
  • 2
    If this isn't working after restarting the notebook, try restarting your browser, and still if not, try clearing cookies. That worked for me! – aeolus Nov 19 '18 at 20:29
  • **similar [to the solution here which modifies the built-in default css file](https://stackoverflow.com/a/45706993/52074). but this solution is a little better because you are mucking with the default css file**. – Trevor Boyd Smith Dec 13 '19 at 18:30
87

To get this to work with jupyter (version 4.0.6) I created ~/.jupyter/custom/custom.css containing:

/* Make the notebook cells take almost all available width */
.container {
    width: 99% !important;
}   

/* Prevent the edit cell highlight box from getting clipped;
 * important so that it also works when cell is in edit mode*/
div.cell.selected {
    border-left-width: 1px !important;
}
jvd10
  • 1,826
  • 17
  • 17
  • This works in Linux! But where do I need to save it in Windows? – crusaderky May 24 '16 at 09:29
  • 10
    Try running `jupyter --config-dir`, then create `custom\custom.css` in whatever location that command returns. – jvd10 May 25 '16 at 15:01
  • @jvd10 this did not work for me. Does `custom.css` have any other code besides the snippet above? Also how does Jupyter know to use it? – mLstudent33 Aug 23 '19 at 17:57
  • 1
    On linux and osx at least, by default jupyter looks for a hidden directory called `.jupyter` for configuration files including customizations like the above. The above should be the only contents of the custom.css. See here for some more info: https://jupyter.readthedocs.io/en/latest/projects/jupyter-directories.html#config-dir – jvd10 Aug 27 '19 at 15:32
  • The .container doesn't work with the version i have installed. This worked instead: .jp-Cell { width: 99% !important; } – tom1919 Jul 25 '23 at 00:05
32

It's time to use jupyterlab

Finally, a much-needed upgrade has come to notebooks. By default, Jupyter-lab uses the full width of your window, just like any other full-fledged native IDE (no tweaking needed).

You can install it either by pip or conda.

# Option 1: using pip
pip install jupyterlab

# Option 2: using conda
conda install -c conda-forge jupyterlab

To launch, just replace noteboook with lab

jupyter lab    # instead of "jupyter notebook"

Here is a screenshot from blog.Jupyter.org

Thamme Gowda
  • 11,249
  • 5
  • 50
  • 57
  • 6
    JupyterLab is still in beta and has some regressions from the regular notebooks (by design). One notable is is forbidding JS by default and needing extensions for every little JS visualisation – Ciprian Tomoiagă Jul 05 '18 at 12:07
  • 4
    And how can you change the width of a Jupyter Lab cell? – multigoodverse Feb 11 '19 at 22:21
  • 4
    I've struggled for an hour making jupyter lab to work. at first no graphics (pyplot / plotly) was showing. The docs don't mention you must install extensions for that. Tried to install the plotly extension. found out you need nodejs for that. installed both, now I get "Cannot find template: "index.html"" message. At this point I gave up, I'm staying with jupyter notebook. – Itamar Katz May 21 '19 at 07:29
  • `jupyter lab` has responsive UI => to change the width of the cell, simply resize your browser window. I have been using jupyter lab for about 3 years and counting, and I never had an issue. Suggestion: If you have an issue with jupyter lab, you probably have a messy environment with conflicting installations. Use conda or such and keep env cleans. – Thamme Gowda Jul 23 '20 at 06:04
  • how, when outputting to a html file, can the cell width be increased? – baxx Sep 07 '20 at 11:56
  • 1
    Every 6 months I try jupyter lab and it just does not work well. – Oren Oct 26 '20 at 02:55
  • 3
    @CiprianTomoiagă JS execution is no longer forbidden in JupyterLab (for quite a while now actually). – krassowski Apr 19 '21 at 11:34
  • My main stumbling block with jupyter lab has been that it requires nodejs version 12 for many extension features (also things jupyter notebook can do) – creanion Sep 10 '21 at 15:20
  • JupyterLab 3 has pre-built extensions now @creanion . No need for node and you can just use pip to install them. At lot of the minor glitches with JupyterLab have been worked out now (as of version 3) and it is way more feature-rich. – Wayne Feb 04 '22 at 20:45
  • JupyterLab only makes the space problem worse because of all the unnecessary things it adds (see screenshot) and because it adds its own tabbing system within a single browser tab – Bananach Feb 09 '22 at 16:57
  • @Bananach Jupyterlab allows hiding and/or resizing anything that seems unnecessary for your workflow! The screenshot is only intended to show full flexibility in the positioning of UI elements. – Thamme Gowda Feb 09 '22 at 19:01
  • @Thamme not the tabs within tabs though? – Bananach Feb 09 '22 at 19:39
  • 1
    I always feel like JupyterLab was written for the fun of writing something new but it has nothing useful for the average user. The one thing I was hoping it could do is display an output cell with permanently updated output in a pop up window separate from the rest of the notebook but it can't even do that despite so much of JupyterLabs promise being more flexible UI. (I know it can display output cells as tab-within-tab, but again, i don't want that, the browser already has perfectly good windows and tabs that i can put, e.g., on different monitors, which JupyterLab can't.) – Bananach Feb 09 '22 at 19:56
  • This doesn't answer the question. "Don't use it" is almost never a reasonable or useful response when someone asks a question about a tool. – L0tad Jan 12 '23 at 19:54
  • The answer does not say dont use jupyter notebook. It says, if you want to use full width of your windows/display, use "jupyter lab". It's like those iphone ads: they doesnt say dont use iphone 4, they say "if you want highest quality camera and faster performance, use the latest iphone". Clearly, jupyter organization is saying notebooks=classic UI, jupyterlab=new UI. – Thamme Gowda Jan 12 '23 at 22:09
27

What I do usually after new installation is to modify the main css file where all visual styles are stored. I use Miniconda but location is similar with others C:\Miniconda3\Lib\site-packages\notebook\static\style\style.min.css

With some screens these resolutions are different and more than 1. To be on the safe side I change all to 98% so if I disconnect from my external screens on my laptop I still have 98% screen width.

Then just replace 1140px with 98% of the screen width.

@media (min-width: 1200px) {
  .container {
    width: 1140px;
  }
}

enter image description here

After editing

@media (min-width: 1200px) {
  .container {
    width: 98%;
  }
}

enter image description here Save and restart your notebook


Update

Recently had to wider Jupyter cells on an environment it is installed, which led me to come back here and remind myself.

If you need to do it in virtual env you installed jupyter on. You can find the css file in this subdir

env/lib/python3.6/site-packages/notebook/static/style/stye.min.css
Gunay Anach
  • 1,193
  • 1
  • 13
  • 19
14

This is the code I ended up using. It stretches input & output cells to the left and right. Note that the input/output number indication will be gone:

from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
display(HTML("<style>.output_result { max-width:100% !important; }</style>"))
display(HTML("<style>.prompt { display:none !important; }</style>"))
AaronDT
  • 3,940
  • 8
  • 31
  • 71
  • 1
    cool! I would only exclude the last line since it makes the cells execution information go away(cells left part, that shows the excecution order and an * when execution is not finished yet) – onofricamila Jun 12 '20 at 14:56
13

You can set the CSS of a notebook by calling a stylesheet from any cell. As an example, take a look at the 12 Steps to Navier Stokes course.

In particular, creating a file containing

<style>
    div.cell{
        width:100%;
        margin-left:1%;
        margin-right:auto;
    }
</style>

should give you a starting point. However, it may be necessary to also adjust e.g div.text_cell_render to deal with markdown as well as code cells.

If that file is custom.css then add a cell containing:

from IPython.core.display import HTML
def css_styling():
    styles = open("custom.css", "r").read()
    return HTML(styles)
css_styling()

This will apply all the stylings, and, in particular, change the cell width.

Student
  • 1,197
  • 4
  • 22
  • 39
Ian
  • 1,483
  • 13
  • 14
  • This doens't work in the latest IPython (version > 2). Check this answer: http://stackoverflow.com/a/24207353/2108548 – rominf Oct 25 '14 at 10:21
  • Or if you just want to change it for the current notebook in recent ipythons and jupyter, see @jjinking's answer :) – nealmcb Dec 19 '15 at 13:54
9

(As of 2018, I would advise trying out JupyterHub/JupyterLab. It uses the full width of the monitor. If this is not an option, maybe since you are using one of the cloud-based Jupyter-as-a-service providers, keep reading)

(Stylish is accused of stealing user data, I have moved on to using Stylus plugin instead)

I recommend using Stylish Browser Plugin. This way you can override css for all notebooks, without adding any code to notebooks. We don't like to change configuration in .ipython/profile_default, since we are running a shared Jupyter server for the whole team and width is a user preference.

I made a style specifically for vertically-oriented high-res screens, that makes cells wider and adds a bit of empty-space in the bottom, so you can position the last cell in the centre of the screen. https://userstyles.org/styles/131230/jupyter-wide You can, of course, modify my css to your liking, if you have a different layout, or you don't want extra empty-space in the end.

Last but not least, Stylish is a great tool to have in your toolset, since you can easily customise other sites/tools to your liking (e.g. Jira, Podio, Slack, etc.)

@media (min-width: 1140px) {
  .container {
    width: 1130px;
  }
}

.end_space {
  height: 800px;
}
volodymyr
  • 7,256
  • 3
  • 42
  • 45
  • yes, Stylish is great! Can you please add the code needed for the extra space at the bottom ? I always have an empty cell with lots of blank lines in order to have the real last cell centred -_- . – Ciprian Tomoiagă Jul 05 '18 at 12:03
  • 4
    Stylish was taken down yesterday for stealing users' browsing history: https://gadgets.ndtv.com/internet/news/google-mozilla-remove-stylish-browser-chrome-firefox-extension-1878558 – stason Jul 06 '18 at 20:43
6

For Chrome users, I recommend Stylebot, which will let you override any CSS on any page, also let you search and install other share custom CSS. However, for our purpose we don't need any advance theme. Open Stylebot, change to Edit CSS. Jupyter captures some keystrokes, so you will not be able to type the code below in. Just copy and paste, or just your editor:

#notebook-container.container {
    width: 90%;
}

Change the width as you like, I find 90% looks nicer than 100%. But it is totally up to your eye.

bizi
  • 3,398
  • 2
  • 27
  • 28
  • Awesome suggestion for jupyter notebook but does so much more! – Robino Jan 15 '18 at 21:40
  • @bizi, do I click inside a code cell and then right click, select Stylebot, edit CSS and copy and paste what you have? If so, it did not work for me. I just got fatter margins on both sides. – mLstudent33 Aug 23 '19 at 17:16
5

For notebook 7 you can use:

from IPython.display import display, HTML
display(HTML("<style>:root { --jp-notebook-max-width: 100% !important; }</style>"))
Iliya Zhechev
  • 101
  • 2
  • 5
3

I made some modification to @jvd10's solution. The '!important' seems too strong that the container doesn't adapt well when TOC sidebar is displayed. I removed it and added 'min-width' to limit the minimal width.

Here is my .juyputer/custom/custom.css:

/* Make the notebook cells take almost all available width and limit minimal width to 1110px */
.container {
    width: 99%;
    min-width: 1110px;
}   

/* Prevent the edit cell highlight box from getting clipped;
 * important so that it also works when cell is in edit mode*/
div.cell.selected {
    border-left-width: 1px;
}
mongmong
  • 45
  • 3
3

Note that if you do this the old way, you'll now get a deprecation warning. This uses the newer submodule naming:

from IPython.display import HTML

HTML("<style>.container { width:100% !important; }</style>")
szeitlin
  • 3,197
  • 2
  • 23
  • 19
1

I tried everything and nothing worked for me, I ended up using displaying my data frame as HTML as follows

from IPython.display import HTML    
HTML (pd.to_html())
smishra
  • 3,122
  • 29
  • 31
0

For Firefox/Chrome users, a nice way to achieve 100% width is to use a custom TamperMonkey script.

The benefits are

  1. configure this once in your browser, no need to modify the server configuration.
  2. works with multiple jupyter servers.
  3. TamperMonkey is trusted, maintained, and stable.
  4. Lots of additional customization is possible via javascript.

This script works for me https://gist.githubusercontent.com/mrk-andreev/2a9c2538fad0b687c27e192d5948834f/raw/6aa1148573dc20a22fca126e56e3b03f4abf281b/jpn_tmonkey.js

Maksim
  • 74
  • 5
0

adding to answers by @jdv10 and @gerenuk

The best option is to add and tweak the custom.css file. Below I am sharing my CSS file contents which I use to squeeze out the maximum screen area in a Jupyter Notebook.

Since it targets the vanilla CSS codes of the rendered page, it is supposed to work for all types of language used for coding on the Notebook.

/* Notebook styling */

body, p, div.rendered_html { 
    color: #93a1a1;
    font-family: 'PT Serif', Georgia, Times, 'Times New Roman', serif;
    font-size: 11pt;
}

body { background-color: #eee8d5 !important; }

/* the following controls aspects which are around the cells */
#notebook { 
    background-color: #073642 !important;
    box-shadow: inset 20px 36px 20px -35px black !important;
    margin: 1px !important;
    padding: 1px !important;
}
#notebook-container {
    padding: 2px !important;
}

/* Make the notebook cells take almost all available width */
.container {
    width:99.5% !important;
    /*margin:.5% !important;*/
    /*color: #93a1a1 !important;*/
    color: black !important;
    background-color: lightblue !important;
}



/* Cell output */

.rendered_html pre, .rendered_html code {
    color: inherit !important;
    background-color: inherit !important;
}

.rendered_html table, .rendered_html td, .rendered_html th {
    border: 1px solid #586e75 !important;
}

div.cell {
    width:100% !important;
    margin: 5px !important;
    /* margin-left:2px !important; */
    /* margin-right:2px !important; */
    padding: 2px !important;
    /* the following overrides the background color of the input area */
    /* background-color: yellow !important;  */
    /* border-color: black !important; */
    
}

/* Prevent the edit cell highlight box from getting clipped;  * important so that it also works when cell is in edit mode*/
div.cell.selected {
    border-left-width: 5px !important;
    border-right-width: 1px !important;
    border-top-width: 2px !important;
    border-bottom-width: 2px !important;
    border-color: red !important;
}
/*this is for the area to the left of the editor or input area*/

div.run_this_cell {
    width: auto !important;
    color: green !important;
    padding: 0 !important;  
    padding-top: 5px !important;
    padding-left: 5px !important;
    font-weight: bold !important;
    font: 2em sans-serif;
}

div.input_area { 
    border-color: green !important; 
    background-color: #ffffdd !important; 
}

.prompt { 
    line-height: 1em !important;
}
div.prompt {
    min-width: auto;
    background-color: white;
}

div.input_prompt { 
    color: #268bd2 !important;
    color: #000000 !important;
    font-weight: bold !important;
    border: 1px solid #ff9900 !important;
    background-color: greenyellow;
    padding-right: 0px !important;
    text-align: center !important;
    width: auto !important;
    font-size: 10px !important;
}
div.output_area {
    color: #000000 !important;
    background-color: #e2e2ff !important;
    font-size: 0.9em !important;
}


/* Syntax highlighting */
.cm-s-ipython span.cm-comment { 
    /*color: #6c71c4 !important;*/
    color: midnightblue !important;
    color: rgb(100, 100, 170) !important;
    font-style: italic !important;
}

.cm-s-ipython span.cm-string { 
    color: rgb(100, 20, 29) !important;
}