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

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.

Related

How can I include cells from another notebook?

I am looking for a way to include one notebook in another, similar to LaTeX's \include directive.
My goal is to split up one large notebook, that mixes Markdown and code cells, into smaller chunks for easier maintainability.
What I've tried
I tried using %run "other.ipynb" command, but that only shows output of code cells and does not include the Markdown cells. So if I have:
In [1]: %run "other.ipynb"
In [1]: # This is a markdown cell
In [2]: Print("Simple string")
Out[2]: Simple string
The output when running main.ipynb is only Simple string, while I expected it to show the Markdown cell as well.

Changing keywords to highlight in an RMarkdown document

I have been writing a document in bookdown where within the *.Rmd file I call a figure by using the following syntax
\#ref(fig:MyFigureName)
This differs slightly from the notation that you would use in a normal RMarkdown file exporting to latex which would be
\ref{fig:MyFigureName}
The issue I am running into is that when I write something in bookdown the function calling the Figure is not being highlighted properly (see image below).
I have imported my own rsTheme (which is from my understanding, basically a .css file) but I don't see an option to add keywords to highlight.
But I would like for the entire function to be colored differently from the inline text (Photoshop version of desired output shown below)
Does anyone know how I would edit my *rsTheme file in order to accomplish this?
Thanks!

Paned plots in R notebook export

When editing an R notebook inside RStudio, if I create multiple graphical outputs in one R block, I get an icon for each plot, which I can click on to select which plot to look at:
I like that behavior, it's especially handy to click from one plot to another to compare changes between them.
However, when I render the notebook to HTML (e.g. by hitting the "Preview" button in the editor), the plots simply cascade down the page:
Is there a way I can get the former behavior in an exported document? Some option I can set, or a chunk of Javascript I can include, or something?

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:

Is there a way to export rMaps output to image?

I'm starting to work with rMaps (following this example), and it's a great package. I'd like to include the results in a document, either by exporting the result to a .jpeg, .png or other graphical format, or by rendering the output directly into a LaTeX document (using sweave or knitr). However, after searching a lot, I've found no way to do it.
So, the question is: Is there a (preferibly simple) way to export rMaps result (ichoropleth, crosslet and/or Leaflet) directly to an image file (or set of files)?

Resources