Add more color to the Python code of your Databricks notebook

As I'm doing more data processing with Databricks, I'm kind of getting annoyed with the syntax highlighting of the editor. The syntax highlighting is a bit off - there is not much distinction between variables, properties and functions. This can be partially attributed to the CodeMirror highlighter it uses, but Databricks could have used a different theme.

Anyway, we're engineers, so let's try to fix some of it.
I came up with this:

Before screenshot
After screenshot
In syntax highlighting color really makes a difference.

Databricks Power Tools Extension

Jesse Bouwman and me created the Databricks Power Tools Chrome extension to tweak the interface of Databricks. It renders a Table of contents that helps you to easier navigate your notebooks. It also contains an option to add CSS code to your Databricks environment, so you can basically tweak the entire interface.

A screenshot of the options provided by the Databricks Power Tools (June 2020).
The options provided by the Databricks Power Tools extension (June 2020).

Let's see what we can do with this.

A Better Font: Fire Code

I want to use the same font I use on my blog and Visual Studio Code: an open source font called Fira Code.

The font is crisp, mono-spaced and uses font ligatures to render common programming language constructs in a better way (as shown in the image).

Fortunately for us: it is a Google Font, so we can use it by importing it into our CSS.

Font ligatures in Fire Code
Font ligatures in Fire Code

The CSS

To uncomplicate things, I heavily used the !important construction. It is frowned upon by most front-enders, but it gets the job done.

/* replace font on all editors */
@import url('https://fonts.googleapis.com/css?family=Fira+Code&display=swap');

.codemirror-preview span,
.CodeMirror-code span {
    font-family: 'Fira Code', 'Droid Sans Mono', 'Consolas', 'Courier New', monospace;
    font-size: 13px;
    cursor:text;
    color: #777;
}
/* recolor @udf, method names and build in names */
.cm-builtin, .cm-meta, .cm-def {
    color: purple !important;
    font-weight: bold;
}
/* properties and function calls */
.cm-property {
    color: #ff0096 !important;
}
/* stuff like def, import, as */
.cm-keyword {
    color: #0096fa !important;
}
/* variables, parameters */
.cm-variable {
   box-shadow: inset 0 -3px 0 rgba(120, 120, 120, .076);
}
/* hovering over lines */
.CodeMirror-line:hover {
    background-color: rgba(200, 200, 200, 0.1)
}
.codemirror-preview span span:hover, 
.CodeMirror-code span span:hover {
    background-color: rgba(0, 150, 250, 0.076)
}
/* comments should not distract us from the code */
.cm-comment {
    color: #969696 !important;
}
/* green string */
.cm-string {
    color: green !important;
}
/* make bash scripts black */
.CodeMirror-line span {
    color:black;
}

Just copy the code into the Databricks Power Tools extension CSS field and you're good to go! Colors everywhere.

Magic CSS

In a previous version of this article I used Magic CSS with pinned styles. It loads a bit slower, but it is still a great tool for prototyping custom styling. The Databricks power tools will load your styles a bit faster.

Screenshot of the Magic CSS extension with pinned CSS
The Magic CSS extension with pinned CSS.

Improvements

2020-06-25: Added the Databricks Powertools extension to load the stylesheets faster.
2020-06-14: Added a fallback font.
2019-12-09: Added support for bash scripts by coloring all lines black. The lines looked like they were commented out.

  1. Chris says:

    Hi Kees,

    Thanks for the nice article, as always! Here’s a dark mode CSS for people who like dark GUI’s.

    /* make default text on the browser grey - to handle braces as they appear as normal text */

    *,
    html,
    body {
    color: grey !important;
    }

    /* Fix for cluster status indicator */

    .fa.status-indicator-icon.ok {
    color: #10b36b !important;
    }

    /* replace font on all editors */

    .codemirror-preview span,
    .CodeMirror-code span {
    font-family: 'consolas';
    font-size: 14px;
    color: #777;
    }

    /* recolor @udf, method names and build in names */

    .cm-builtin,
    .cm-meta,
    .cm-def {
    color: #ff0096 !important;
    font-weight: bold;
    }

    /* properties and function calls */

    .cm-property {
    color: orange !important;
    }

    /* stuff like def, import, as */

    .cm-keyword {
    color: #0096fa !important;
    }

    /* variables, parameters */

    .cm-variable {
    box-shadow: inset 0 -3px 0 rgba(185, 58, 658, .076);
    color: indianred !important;
    }

    /* hovering over lines */

    .CodeMirror-line:hover {
    background-color: rgba(200, 200, 200, 0.1);
    }

    .codemirror-preview span span:hover,
    .CodeMirror-code span span:hover {
    background-color: rgba(0, 150, 250, 0.076);
    }

    /* comments should not distract us from the code */

    .cm-comment {
    color: #969696 !important;
    }

    /* yellow string */

    .cm-string {
    color: #cebb34e0 !important;
    }

    .CodeMirror-lines {
    background-color: #0c0b0bd7 !important;
    }

    .cm-operator {
    color: lightcyan !important;
    }

    .CodeMirror-matchingbracket {
    background-color: #ffffff !important;
    }

    /* background color for errors */

    .compile-error {
    background-color: #b93a3a87 !important;
    }

    /* overwrite console colors */

    .ansiout {
    color: #a9cc81;
    background-color: #24231e
    }

    .ansiout .ansiblue {
    color: darkorange;
    }

    .ansiout .ansired {
    color: crimson;
    }

    /* make bash scripts black */

    .CodeMirror-line span {
    color: black;
    }

expand_less