Skip to content

Instantly share code, notes, and snippets.

@whophil
Last active November 27, 2021 09:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whophil/6e7f8fe891a06772c5fa5ab69e8cee73 to your computer and use it in GitHub Desktop.
Save whophil/6e7f8fe891a06772c5fa5ab69e8cee73 to your computer and use it in GitHub Desktop.
Pretty style for Jupyter notebooks using Google web-fonts. Apply to all your notebooks using %run magic.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Phillip Chiu](mailto:philchiu@ucla.edu) / UCLA / March 28, 2016\n",
"* * *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# IPython Notebook Style File"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notebook containing styling CSS for improved readability. This notebook (and these styles) can be imported into other notebooks.\n",
"The fonts are imported from Google's web fonts."
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import IPython.core.display\n",
"import matplotlib"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `clean()`"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def clean():\n",
" # set mpl defaults for nice display\n",
"# matplotlib.rcParams['font.size'] = 12\n",
"# matplotlib.rcParams['figure.figsize'] = (18, 6)\n",
"# matplotlib.rcParams['lines.linewidth'] = 1\n",
"\n",
" return IPython.core.display.HTML(\"\"\"\n",
"<style type=\"text/css\">\n",
"@import url('http://fonts.googleapis.com/css?family=Open+Sans');\n",
"\n",
"div.input {\n",
"width: 105ex; /* about 80 chars + buffer */\n",
"}\n",
"\n",
"div.text_cell {\n",
"width: 60em; /* instead of 100%, */\n",
"}\n",
"\n",
"div.text_cell_render {\n",
"font-family: 'Open Sans', 'Computer Modern', 'Helvetica Neue', 'Arial', 'Helvetica', 'Geneva', sans-serif;\n",
"line-height: 145%; /* added for some line spacing of text. */\n",
"width: inherit; /* instead of 'inherit' for shorter lines */\n",
"}\n",
"\n",
"/* Set the size of the headers */\n",
"div.text_cell_render h1 {\n",
"font-size: 20pt;\n",
"font-weight: bold;\n",
"}\n",
"\n",
"div.text_cell_render h2 {\n",
"font-size: 18pt;\n",
"}\n",
"\n",
"div.text_cell_render h3 {\n",
"font-size: 16pt;\n",
"}\n",
"\n",
"div.text_cell_render h4 {\n",
"font-size: 14pt;\n",
"}\n",
"\n",
".CodeMirror {\n",
" font-family: Consolas, monospace;\n",
"}\n",
"\n",
".rendered_html ol {list-style:decimal; margin: 1em 2em;}\n",
"\n",
"/* Table of contents */\n",
"div#toc-wrapper {\n",
" max-width: 300px\n",
"}\n",
"div#toc {\n",
" font-family: 'Open Sans', 'Computer Modern', 'Helvetica Neue', 'Arial', 'Helvetica', 'Geneva', sans-serif;\n",
"}\n",
"\n",
"/* Code */\n",
"code {\n",
" font-family: Consolas, monospace;\n",
"}\n",
"</style>\n",
"\"\"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Usage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To use these styles, import them into your IPython or Jupyter notebook. For IPython newer than version 2.0:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```python\n",
"%run '/path/to/style.ipynb'\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, call any of the named styles, e.g.,\n",
"\n",
"```\n",
"clean()\n",
"```\n",
"\n",
"An example cell is shown below. (Copy/paste this into the header of any notebook to apply the `clean()` style defined above)."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"```python\n",
"%run '~/Notebooks/style.ipynb'\n",
"clean()\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"## uncomment to test\n",
"clean()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Examples of headings\n",
"* * *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Heading 1\n",
"## Heading 2\n",
"### Heading 3\n",
"#### Heading 4\n",
"##### Heading 5\n",
"###### Heading 6"
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
},
"toc": {
"toc_cell": false,
"toc_number_sections": false,
"toc_threshold": 4,
"toc_window_display": false
},
"toc_position": {
"left": "1626.22px",
"right": "20px",
"top": "114px",
"width": "215px"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment