"Whenever you can, share. You never know who all will be able to see far away standing upon your shoulders!"
I write mainly on topics related to science and technology.
Sometimes, I create tools and animation.
An improved version of the code shown below is now available as a github package here.
The code given on this page will be useful in quickly understanding what we are trying to do here, and it is sufficient for the basic functionality.
Jupyter, and Interactive Python in VS Code, have markdown cells that can be used to show documentation in very interesting ways (e.g. we can have formatting, equations, images, videos etc.) that is not possible using normal commenting in any programming language. This is one of the greatest selling points of Jupyter.
However, it becomes slightly annoying to keep adding a markdown cell for every small piece of documentation you want to display.
For example, we may want to keep these little comments in the code block below in a single cell for the sake of avoiding clutter:
But if we, at the same time, wanted these suggestive comments to be displayed as markdown in the cell output, that will not happen, unless we put them in a separate markdown cell, a little too much of a hassle and clutter.
This workaround will let us display markdown from within code cells in IPython output, while ignoring it when run in normal Python (like usual comments) . The only constraint is that we have to use strings in place of comments.
Note: I used forbiddenfruit just for aesthetics, that is not a requirement.
The code above can reside in a separate file, and which can be imported anywhere needed. Thus it becomes a mini-extension.
After this, the following code (e.g.), when run as a normal script, ignores the string-comments:
but when run in IPython, displays this (alongside executing the python code):
Thus we have calculated the volume of the cylinder by using the formula
Now we'll calcualte the area as per
Considering that triple quoted strings are often used as multiline comments, this solution may be useful in many scenarios, and will not be a significant deviation from using normal comments.
On the plus side, there are two advantages:
An improved version of the code (with extra options) shown above is now available as a github package here.
This package can be used for two quite different purposes: