Loading code directly from git (or anywhere actually) inside a Jupyter cell06 Apr 2019
I may be getting used to short posts, but here it comes: this will be another zippy one! The other day, I realized something quite interesting about the Jupyter notebook (in fact, it comes from IPython…) magic
%load. You can use it with an URL!
Sadly, it still doesn’t work with Google Colab, but it works with your local Jupyter like a charm. Also, the use of
%load is safe’ish considering it will only load the code without executing it unless you execute the cell again.
If you want to import from GitHub you need to use the link to the
raw file or it will load the source code of the html page. Here I give two examples based on a gist I created:
- If you type this
%load https://gist.github.com/ricardodeazambuja/4d789eb2933c0931bdb93cb8bb438b1cit will load the html source code of the page.
- Now, if you use the raw link
%load https://gist.githubusercontent.com/ricardodeazambuja/4d789eb2933c0931bdb93cb8bb438b1c/raw/11204f711559d5ba0fe8fd51636483afe0633cf7/test_jupyter_load.pyit will work as expected, but this link is way too long.
One easy solution for the ultra-long-ugly-link problem is the GitHub URL shortener. I used it with the raw link and now you just need this to load the code directly into a cell:
Ok, this was supposed to be a zippy post, so cheerio!