Inserting comments into jupyter notebook - python

I hope this is not a redundant question, but I was not able to find a proper answer and tutorial. I am currently learning Data Science and therefore use kaggle competition for practice and jupyter notebook to tackle them (as it is visually more appealing to me). Now I stumbled upon an example kernel (see: http://nbviewer.jupyter.org/github/agconti/kaggle-titanic/blob/master/Titanic.ipynb#Data-Handling) is using comments, links and illustrations between his codes to give more context.
Is anybody aware of how to write such comments and remarks within jupyter? If so, do you have an online resource where I could learn this?
Thanks a bunch!

Yep - highlight a cell and click on the "Cell" dropdown menu and go to "Cell Type" and choose "Markdown". Then you can type any markdown text you'd like and it will render as such.
^^ Also there are shortcuts for changing cell types as well. Highlight a cell and press the esc key to change into "command" mode and press m. This changes it to a markdown cell. Press y to change it back to a code cell.
You can also do latex equations using dollar signs ($). There is documentation for this here

For markdown cells
[//]: # "Your comment in here."
allows to include a comment in jupyter notebook server version 6.0.1. See this discussion for more details on markdown comments.

Agree with #RSHAP. I just add the flow picture in the below.
select the active Cell > Cell Type > Markdown.

irrespective of the version of jupyter notebook you use, the following command should work on current cell
Pres ESC
then Press m

As mentioned in the above remarks, use markdown cells.
Once in a markdown cell, use #,##,###,#### for headings of different fonts. For bold, use **your_text_here**, and for italics use *your_text_here*. For writing equations and using latex commands, use $ symbol. Everything works just like in latex; you can even define your notations like you would normally do in Latex. For a more detailed and structured overview to format text In Jupyter, have a look at this website:
https://www.earthdatascience.org/courses/intro-to-earth-data-science/file-formats/use-text-files/format-text-with-markdown-jupyter-notebook/

Related

How to control result size when using jupyter on vsc and how to search for words in result

I'm currently using Jupytor Notebook on VSC. I found it very uncomfortable when very long result come out because I have to scroll down a long way to go the the next cell, like shown in the picture.
So I was wondering if there is a way to make the result like the original Jupyter Notebook, where they have a separate scroll for the result as shown below.
As an additional question, anyone know how to search for certain words in the result on VSC? ctrl+f only searches for words in the coding cells and not the result.
A work around for the scroll wheel if you don’t want to collapse the code is to add a print statement to the beginning and end of the code block as mentioned in Collapse Output in VS Code Jupyter Into Scrollable Window. I have not been at my computer to test this and this post is from a year ago, so I’m not sure if this solution is still working. EDIT: This solution is no longer working keep an eye on the linked issue for the resolution.
This was also brought up as an issue on GitHub and it was closed saying they have no plans to work on it currently.
#118117
As for the searching in outputs, this is an open issue currently on GitHub: #94239
Scrollable output regions would be handled in VS Code core. This function has not been decided on GitHub at present
For your additional question, I think the filter feature in the search widget might be what you're looking for - you can include outputs with that.

latex jupyter notebook it does not work

I started using python with the jupyter notebook and the problem is that the printing is not like the standard LaTeX
My cell:
%%latex
\begin{center}
$(a+b)^{2} = a^{2}+2ab+b^{2}$
\end{center}
The result in the notebook looks like in the following picture:
from jupyter docs:
The subset of latex which is supported depends on the implementation in
the client. In the Jupyter Notebook, this magic only renders the
subset of latex defined by MathJax
here.
You could use markdown to display your formula: use single $ to indicate latex, or double $$ to center it. You need to format your cell as markdown instead of code
Your cell becomes one of these two lines:
$$(a+b)^{2} = a^{2}+2ab+b^{2}$$
$(a+b)^{2} = a^{2}+2ab+b^{2}$
You must run the cell to display markdown.
I had a similar problem. A certain set of latex equations in my ipython notebook just stopped rendering properly. It turned out that my latex interpreter had gotten into a weird state because I executed a cell with the code \def in it, by accident.
To fix the problem, I just saved my notebook, stopped the python kernel, closed the notebook in the browser, then re-opened it again and re-executed all the cells. This fixed the problem, and the equations rendered correctly again.
I discovered from https://stackoverflow.com/a/32166264/4347428 that the MathJax will not render unless you go into your jupyter config and ensure mathjax is enabled:
## Whether to enable MathJax for typesetting math/TeX
#
# MathJax is the javascript library Jupyter uses to render math/LaTeX. It is
# very large, so you may want to disable it if you have a slow internet
# connection, or for offline use of the notebook.
#
# When disabled, equations etc. will appear as their untransformed TeX source.
c.NotebookApp.enable_mathjax = True
Unfortunately, this didn't get me all the way there, and neither did making sure the notebook was Trusted in the upper right, but perhaps it'll help someone else.
In my case (winpython), it looks like my firewall prevent Mathjax to be succesfully installed. I had to install Mathjax from a local file to get I to work :
download the latest Mathjax.zip
python -m IPython.external.mathjax C:\path\to\your\file\Mathjax.zip
I am now able to render latex in code cells and in markdown cells either
If you are going to use an environment like this:
\begin{equation}
(a+b)^{2} = a^{2}+2ab+b^{2}
\end{equation}
Notice I do not use the $$ env which is better used as inline math. Another thing to notice is that there are no spaces between the lines, if you add spaces between the lines, the notebook will display the latex code instead of rendering it when you run the cell.
Using jupyterlab installing this package: jupyterlab-mathjax3 solved this problem for me.

How to edit markdown cell in jupyter-notebook ( Could not edit markdown cell in Jupyter notebook)

I got a strange problem with jupyter-notebook.
I was practicing with notebook which has markdown and code cells.
When I save and reopen the notebook, I can edit code cells but not the
markdown cells.
Attempts:
reload the page.
Make the notebook trusted.
Try to change the cell type from markdown to code or raw but still can not edit.
NOTE:
I can delete some letters on markdown but I can not add any letters.
Also if I hit enter it will create new lines, but I can not write anything there.
Question
How can we edit the markdown cell of a jupyter-notebook ?
In case anybody also encountered the same problem, I am keeping this question
and my solution to it, instead of deleting the question.
What I did is :
a) First go to markdown cell.
b) Double click the cell, now we can only delete the letters, can not edit it.
c) Go to command mode (press esc) and again come back to edit mode (Enter).
d) Now we can edit the markdown cell.
This solved my problem.
Just double click on the markdown cell. Edit what you want to and Run. It will reflect the changes. Save your notebook.

How to format text cells in Jupyter?

I would like to create a Jupyter notebook to make a tutorial.
When I go to the jupyter website and I try jupyter in the browser, there is already a nice python tutorial, with some cells used as text explanations and others as code:
Now I create a new notebook and I am able to create different cells and format them in 4 types:
Code
Markdown
Raw NBConvert
Heading
However none of them allow me to create the text explanations with white background as in the image above.
I tried formatting one cell as markdown and it's just keeping with the grey background.
Am I missing something? Thanks.
After creating those blocks you have to run them to give you that white background.
Press Ctrl + Enter inside the block.
For eg.
Click on a cell. Press Esc and then press 1
Now type this: "THIS IS A HEADING"
And then press Ctrl + Enter.
You will get this as an output with a white background (The image below is a bit dull):
In addition, by using the %%html magic command, you can type HTML in cells and get them rendered the way you want

Ipython Notebook: Elegant way of turning off part of cells?

In my ipython notebook, there is part of cells that serves as preliminary inspection.
Now I want to turn it off, since after running it I know the status of the dataset, but I also want to keep it, so other people using this notebook can have this functionality.
How can I do it? Is there any example of doing it?
I can comment out these cells, but then switching between on and off would be quite laborious. And may not be quite convinent for other people.
I can abstract it into a function, but that itself has some methods, so the code would be quite convoluted, and may be hard to read?
Using Jupyter notebook you can click on a cell, press esc and then r. That converts it to a "raw" cell. Similar thing can be done to convert it back, esc + y. No comments needed, just key presses.
Within Jupyer notebook, go to Help -> Keyboard shortcuts for more.
Here's a snippet:
Command Mode (press Esc to enable)
↩ : enter edit mode
⇧↩ : run cell, select below
⌃↩ : run cell
⌥↩ : run cell, insert below
y : to code
m : to markdown
r : to raw
In Jupyter notebooks one can use this magic preamble at the beginning of a cell to avoid its execution:
%%script false --no-raise-error
You can use a condition at the cost of one extra indentation.
cellEnabled = 0
#cellEnabled = 1
if cellEnabled:
doA()
doB()
I had the same kind of desire and I eventually found out about the nbextension called Freeze. When you enable it, you get a nice freeze button in your toolbar. When you click it, the cell you're currently in will become "frozen". This means it will turn green (making it visually clear) and it will be ignored by the Run All process. It's also locked for editing, so you do need to unfreeze it (unlock button, two over to the left of the freeze button) before editing or running the cell. That's really easy to do though because it's just one button.
Let me know if this wasn't super clear. Otherwise, I hope this helps!

Categories