Jupyter add markdown heading within loop - jupyter-notebook

I'm creating plots in a loop in Jupyter notebook. Is it possible to auto generate markdown heading within a loop before every figure so that the headings can show up in jupyter toc or vscode outline for easy navigation?
import matplotlib.pyplot as plt
import numpy as np
for i in range(1,5):
#add markdown heading here
plt.figure()
x = np.arange(10)*i
y = np.sin(x)
plt.plot(x, y)

You can change the cell type of any cell in Jupyter Notebook using the Toolbar. The default cell type is Code. To use the Keyboard Shortcuts, hit the esc key. After that, you can change a cell to Markdown by hitting the m key, or you can change a cell to Code by hitting the y key
see this image for reference

Related

Jupyter notebook, move cells from one notebook into a new notebook

Is it possible to move n cells from one notebook to a new notebook?
Programmatically move n cells from one notebook to a new one
Yes, you can use nbformat to take a notebook and limit the content of the new notebook to the a block of n cells of the original notebook.
"The nbformat package allows you to programmatically read and parse notebook files." - SOURCE, Tony Hirst's description
nbformat comes as part as Jupyter so it runs wherever you have your notebooks running.
Making a notebook from the first n cells of a notebook
I'm going to base this mainly on code adapted from my post at the Jupyter Discourse forum here. I have other examples with nbformat-related code you can get to from links below that post.
You can paste this code in a notebook that's running where your input notebook is located:
number_cells_to_keep = 5 # number of first n cells in the input notebook to keep
import nbformat as nbf
ntbk = nbf.read("old_notebook.ipynb", nbf.NO_CONVERT)
new_ntbk = ntbk
new_ntbk.cells = [cell for indx, cell in enumerate(ntbk.cells) if indx < number_cells_to_keep]
nbf.write(new_ntbk, "first_n_cells_of_input_notebook.ipynb", version=nbf.NO_CONVERT)
Edit the number on the first line to adjust the number of cells at the top of the original notebook that will be moved to the new notebook.
Making a notebook from a block of cells at a start point & spanning n number of cells
You could adapt the above code to select a specific internal range of cells.
This example illustrates starting the interval of cells at the fifth code cell and taking that and the following cells for a total of ten from the originating notebook to produce the new notebook:
cell_to_start_collecting_at = 5 # number of cell to start the span of cells to collect; first cell gets number 1 in bracket if run so use that numbering
length_of_cell_block_to_keep = 10 # length of sequential span of cells to keep
import nbformat as nbf
ntbk = nbf.read("old_notebook.ipynb", nbf.NO_CONVERT)
new_ntbk = ntbk
new_ntbk.cells = [cell for indx, cell in enumerate(ntbk.cells) if cell_to_start_collecting_at - 2 < indx < (cell_to_start_collecting_at + length_of_cell_block_to_keep - 1)]
nbf.write(new_ntbk, "has_interval_of_cells_from_input_notebook.ipynb", version=nbf.NO_CONVERT)
Edit the first two lines to adjust the starting cell number and length of the block of cells to move to the new notebook.
The collection of the cells for the new notebook using nbformat while iterating on the original notebook cells with for cell in ntbk.cells or a variant could be adapted & made to be complex. For example, if you had a list of certain cells to go into the new notebook or only wanted to count code cells for the point at which to start.
Use JupyterLab to drag by hand a sequence of cells to a new notebook
If you are trying to move a block of cells from one notebook to a new one using JupyterLab's graphical user interface, you open your new notebook and arrange it side-by-side next to the original notebook in your main pane by dragging the tabs with the notebook names to arrange them.
Then you can select the cells in the original notebook and then dragging from the top of that highlighted block, drag them over into the new notebook.
Save the edited version of the 'new' notebook.
Video illustrating the arranging and dragging approach is here.
It is featured under 'Drag cells between notebooks to quickly copy content' on the Notebooks page in the JupyterLab documentation.

How to capture a cell's output for use in another cell?

In org-mode, I can name the output of a code block and include it elsewhere in the document.
Is it possible to do this (or something similar) in a colab .ipynb file, or within a Jupyter notebook in general?
For example, I can make a cell with some Python code that generates a plot:
import matplotlib.pyplot as plt
plt.plot([0,2,1,4,9])
After executing, the plot appears below the code cell. So far so good.
But how do I capture this plot and to use it elsewhere in the notebook?
My hope is there is some syntax so that I can include the plot in a markdown cell, for example:
# this is my title
As you can see, the numbers go up and down in the plot:
![][some_magic_link_here]
Here is some more text to explain the plot.
Does such a feature exist in colab?
Good news - embedding an image in another markdown cell is self-service. Here's an example:
Full notebook:
https://colab.research.google.com/drive/1PF-hT-m8eZN2CBzFkMp9Bvzj9pSBQVYn
The key bits are:
Using IPython.display.Markdown is order to programmatically construct the markdown.
In addition to rendering, save the matplotlib figure using savefig.
Include the image data in the markdown after base64 encoding.

How to copy multiple input cells in Jupyter Notebook

Basically I want to copy (Ctrl+C) only the code portions from multiple cells without also copying the output or the In[1]: and Out[1]:
What is the easiest way to do so?
When you are on a cell in Command mode(blue color mode), simply press Shift + DownArrow or Shift + UpArrow to select multiple cells. Press ctrl + C. And that's it. You have copied your entire selected code at once. It doesn't affect whether you have cell outputs.
Command mode: The Jupyter Notebook has two different keyboard input modes. Edit mode allows you to type code or text into a cell and is indicated by a green cell border. Command mode binds the keyboard to notebook level commands and is indicated by a grey cell border with a blue left margin.
In jupyter you can copy several cells or the content of one cell. If you follow #BenWS comment you can copy several cells, and if you do kernel > restart & clear outputs beforehand you woult not get the [out]. Shortcut is C for copy cell and V shift + V to paste below / above.
However if you intend to copy several cells content, you should merge then before by select them and shift + M and then you can copy paste with ctrl + C.
What worked for me is the following:
update jupyter notebook within a cell using:
pip install -U jupyter notebook
go in command mode by clicking to the left of a cell. If you click inside of a cell, it will be green.
Use shift+down/up to select the cells you want to copy and use ctrl+c
Now the most important one: make sure the jupyter file you want to copy the cells into is ALSO in blue/command mode. If this is not the case, you will copy all the cells into a single cell.
Just do:
File > Export Notebook As > Export Notebook to Asciidoc
and it will be easy to copy paste.
This is what an Asciidoc file looks like:
+*In[ ]:*+
[source, ipython3]
----
import pandas as pd
df = pd.read_csv("data/survey_results_public.csv")
df.tail(10)
df.shape
pd.set_option("display.max_columns", 85)
pd.set_option("display.max_rows", 85)
schema_df = pd.read_csv("data/survey_results_schema.csv")
schema_df.head(10)
----
In the latest version of JupyterLabs:
File > Export Notebook As > Executable Script
Gives you the code as a text file.
Open notebook dir as project in PyCharm, and then open the wanted ipynb file, select and copy all the source code, past into notepad++, replace "\r\n#%%\r\n\r\n" by null with extended search mode.
For jupyterlab after Shift + UpArrow or Shift + select with mouse on multiple cells. Right click on cells for copy(C) and paste(P).

how to control display output size in ipython

I have an ipython notebook and I want to write a function to display matrixes:
from IPython.display import display
import sympy
sympy.init_printing()
def print_matrix(a):
display(sympy.Matrix(a))
import numpy as np
print_matrix(np.random.random((20, 20)))
This is producing a mathml output which is very large on my screen. How to control the size? I don't want to truncate numbers, I want to decrease the size of the fonts / size of the output image.
I have accomplished it with
sympy.init_printing(use_latex='png', fontsize='5pt')
but I don't like the png output, I would like the default mathjax
I won't save with the notebook (you'll probably need to execute some Javascript in a cell to do that), but if you right click on an equation there is an option to scale it up or down (this is a MathJax feature, which has nothing to do with the IPython notebook).

Inserting a Link to a Webpage in an IPython Notebook

How is this done? I'd like to have the link be in a markdown cell.
For visual learners:
[blue_text](url_here)
In case it is not a markdown cell, that is with what I went:
from IPython.core.display import display, HTML
display(HTML("""text"""))
Just another tip, using magic expression.
%%html
Showing Text
Improved. Thanks to the comment of calocedrus.
Here is the code I use in my python notebook when I want to insert a link to a webpage inside a markdown cell (in a python notebook).
[Clickable_visible_hyperlink](Hidden_landing_URL)
--note Here is the clickable hyperlink, you can change the value
This might help too, if you're looking to display a link programmatically.
from IPython.display import display, Markdown
display(Markdown("[google](https://www.google.com)"))
I also tried
display(HTML("""<a href="https://www.google.com>google</a>"""))
But somehow I was getting the object printed out, instead of the rendered version.
For programming in R, do the following when using Jupyter Notebook or Jupyter Lab - (using the R kernel). These steps will display a web link and an image in a Notebook markdown cell. The following shows a real-life example of some study notes using Jupyter Lab and R.
First open a markdown cell in Jupyter - can be a new markdown cell or an existing markdown cell. Then copy and paste the actual web address into a markdown cell. This will provide an active link to that website from the Notebook.
Step 2, from that website, copy the image that you want to view in the Notebook. This image should be in a standard image format (.png, .jpg, etc ). Paste this image into the same folder on the computer where the Jupyter notebook file is located. Note: if the image is later deemed too large or small, then resize using any graphics software available - and then save the changed image into this same folder. Note: it is important to know the name of this image file.
Next, paste the name of the image file between the quotation marks in the following code: . If this file in not within your existing jupyter notebook working directory, then a path to the image file will need to be placed inside the quotation marks.
Step 3, also included is an example of the line of code (also used in Notebook markdown cell) to create colored text in markdown cells. In this line of code, the double ## character results in the second largest font being used in Jupyter. Smaller text using more of these characters - with #### being the smallest. One # results in the largest font output.
Last, be sure to close and run the markdown cell to view the output. The code for the markdown cell follows, and further below shows the output from the Notebook.
Code in Markdown cell:
"https://www.tensorflow.org/images/colab_logo_32px.png" # link to website
<img src="tidyflow.png" /> # The image file (This path is the same folder as Notebook file)
## <font color = cyan> Some Colored Text in Notebook Markdown Cell </font> # colored text
Output:

Resources