Sunday, January 31, 2021

Blog with Colab

jupy

Open In Colab

What is Google Colab?

Google Colab (or Colaboratory or Colab) is an online notebook running on Google Cloud which allows you write and execute Python on browser without using resources of your computer. It can be considered as an online Jupyter Notebook, i.e. it allows multi programming languages: Markdown, HTML, Latex, Python, R, … . You may visit the origin of this post in Colab by simply clicking on the badge Open in Colab at the begining of the post. Hope you enjoy it!

  1. You may type maths expression 01xdx\int_0^1 \sqrt{x} dx or run your Python code
2+2
4
  1. You may plot
import numpy as np
from matplotlib import pyplot as plt

ys = 200 + np.random.randn(100)
x = [x for x in range(len(ys))]

plt.plot(x, ys, '-')
plt.fill_between(x, ys, 195, where=(ys > 195), facecolor='g', alpha=0.6)

plt.title("Sample Visualization")
plt.show()

png

  1. You may import probably all kinds of packages, e.g. torch. However, in Binder you sometimes can't even import numpy.
import torch
print("import torch successfully")
import torch successfully

How to bring your notebook to blog post?

The naive idea is to use Jupyter Notebook opens your notebook and export it as .html file. However, you will encounter the size-inconsistency between the post and your website. How to fix it?

Solution 1.

The following is a temporary solution, it's not very elegant, but at least it works. If you already have a notebook go straightforward to step 2. The strategy is to convert your notebook being an .ipynb to .md and then to .html a format suitable for our blog post.

  1. Go to this Google Colab change the contents and the codes as you want. In the tab File, download it as an .ipynb file.
  2. Go to Github and upload it to your publish repository.
  3. Go to Binder, past the link of your repository and the press the launch button. Open the .ipynb file. A Jupyter interface will be appear. In tab File, download it as a .md file.
  4. Copy the content of the .md file (you may copy it by uploading and then opening it using Google Drive).
  5. Go to StackEdit and past its content to a new file created in StackEdit. Export the file as an .html file.
  6. Go to Blogger, copy the content of your .html file and past it to your Blog post. Note that you have to manage the images by hand.

I know that it is quite complicated as it requires a lot of steps as Colab does not support export .md and .html files. Of course, we can make the steps shorter by creating a notebook in Jupyter directly and then export it to a .md. Above general process is useful in some cases, e.g. cannot import torch in Jupyter. I hope I will find a simpler method in the comming time.

Solution 2.

... to be updated

Popular Posts