Explain Codes LogoExplain Codes Logo

How do I increase the cell width of the Jupyter/ipython notebook in my browser?

html
responsive-design
css
jupyter-notebook
Alex KataevbyAlex Kataev·Oct 20, 2024
TLDR

Expand Jupyter Notebook's cell width to the edge of your browser:

from IPython.display import HTML HTML("<style>.container { width:100% !important; }</style>")

_execute the above snippet in a cell to stretch the notebook's layout to leverage your screen's entire width. Adjust the width:100% property as per your needs.

Responsive design: Adapt the width to screen resolution

Fixating the cell width's percentage can turn troublesome while switching devices. For the same, you can include a media query in your CSS to create a responsive layout:

@media screen and (min-width: 1200px) { .container { width:90% !important; } /*Desktop-sized width!*/ } @media screen and (max-width: 1199px) { .container { width:100% !important; } /*Smartphone-sized screen, full cell view!*/ }

This piece of CSS adjusts the cell width dynamically based on the browser window size, providing an overall consistent and adaptive user experience.

Keeping visuals neat: Monitor cell highlights

When you modify the cell width, ensure that the editing box and cell highlight remain visible. Retain this visual feature by adjusting the border-left-width via CSS:

.CodeMirror-focused .container .CodeMirror-lines { border-left-width: 2px !important; } /*Super-thick border for those pixel perfect edits!*/

Persistent cell width: Using custom CSS

Create permanent changes in cell width across all your Jupyter notebooks. Write your CSS in a custom.css file and place it in the .jupyter/custom/ (.ipython/profile_default/static/custom/ for iPython) directory:

/* .jupyter/custom/custom.css */ .container { width:98% !important; } /*An eternal solution to an eternal problem! */

Restart the Jupyter server post the modifications in custom.css. Global customization applies to all Jupyter notebooks.

The future is now, new tech: JupyterLab and themes

Upgrade to JupyterLab for an enhanced interactive experience. Launch JupyterLab by triggering the command:

jupyter lab

Use jupyterthemes to stylize your notebooks. They offer both themes and cell width adjustments.

pip install jupyterthemes jt -t chesterish -cellw 90% /*Ohh, I see you like it dark!*/

Pick the best: Choose based on requirements

Different users, different requirements. Weigh all potential solutions and opt for the one that suits your preferences and needs. Be it a temporary fix, a responsive design, or a persistent change, there is something for everyone!