IPython Jupyter Notebook Trick – Auto Module Reload and Inline Chart Plotting

Two really handy IPython Jupyter Notebook Tricks – Auto Module Reload and Inline Chart Plotting.

Auto Module Reload

Say in our main script we import an external module (that we own). By deafult if we’ve edited that module, it is not automatically reloaded (and therefore won’t take effect in the main script). To enable rapid iteration, it would be very handy to add this IPython magic command at the top – so whenever we’ve updated our module, it’s auto-reloaded in our main script.

Inline Chart Plotting

Another very handy magic command is inline chart plotting (via matplotlib). By default if we do a plot in Jupyter Notebook, a new window will pop up with the chart. If we include the following IPython magic command at the top, our chart may get rendered directly inline within the Jupyter Notebook. (Note that qtconsole by default do the plot inline so we won’t need to worry about that there. Note also that Jupyter Console is text based only and a chart can only be plotted in a new window – i.e. no inline).

Happy rapid iteration!

Sublime Text 3 – How to set different tab size based on file type?

Recently I’ve been switching back-and-forth developing in multiple languages, in particular, Python (for backend and data science stuff) and JavaScript (for front-end web demos and ReactJS stuff). As I use Sublime Text 3 to do the code editing, I very often find myself in a situation where I had to manually change the tab size to 4 spaces when coding in Python, and 2 spaces when coding in JavaScript. The manual process eventually got quite painful and tedious so I decided to find out if Sublime Text 3 is clever enough to automatically set the desirable tab sizes based on the file type?

Fortunately the answer is yes – this post documents how (once you know how it only takes less than a minute to setup – Read on!)

What we want to Achieve

  • If we have a Python file, Sublime Text auto set tab size to 4.
  • If we have a JavaScript file, Sublime Text auto set tab size to 2.

(and some other desirable tab sizes for other languages – concept is the same).

The Solution

Follow the step-by-step guide – should take about a minute.

Step 1

First, make sure the Python file is opened in editor and active.

On Mac: Sublime Text -> Preferences -> Settings - Syntax Specific.

(On Windows and Linux I presume it would be something similar)

Step 2

A blank Python.sublime-settings file will open. Simply copy and paste this JASON text to it:

Save and close the file.

Step 3

Repeat Step 1, but this time do it for the JavaScript file.

A blank JavaScript.sublime-settings file will open. Simply copy and paste this JASON text to it:

Save and close the file.

Finished!

Now, whenever a Python file is opened, tab size will be set to 4 automatically. And whenever a JavaScript file is opened, tab size will be set to 2 automatically. (Try this out for other languages as you need).

Reference: This Stackoverflow forum