Easiest way to replace placeholders (variables) in markdown text using Jupyter Notebook? - jupyter-notebook

I am using Jupyter Notebook. I have a .md file in which I want all the placeholders to be replaced by values defined in another file.
Theorems.md (the reference file)
Theorem 1 = theorem blah blah.
mynotes.md (file where the placeholders should be replaced)
I like [[Theorems.md_theorem1]]
mynotes.md preview should be:
I like theorem blah blah.
I have looked at this answer already. That answer asks something for pandoc. I was wondering if there is some easy way to do this inside jupyter notebook.

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.

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.

Change formatting behavior for a URL in nbconvert

I want to convert a Jupyter notebook to LaTeX using nbconvert. The default exporting behavior is to convert Jupyter hypertext links
[a link](http://some.website.com)
to a LaTeX string that can be rendered as a link in a PDF document:
\href{http://some.website.com}{a link}
I would like to change this behavior, so that links are rendered instead as footnotes:
a link\footnote{http://some.website.com}
What do I have to modify to do this? I've looked at the documentation for nbconvert but haven't been able to figure it out. Is it possible to do this within a .tplx template file? I've looked at the standard template files and don't see anything defining the URL behavior, so I'm guessing it's handled by pandoc somehow, but I'm confused about where I need to change something.
You can use LaTeX to redefine the \href command. Put the following in your template or using header-includes:
\let\oldhref=\href
\renewcommand{\href}[2]{\footnote{\oldhref{#1}{#2}}}
Alternatively, you could write a pandoc filter to rewrite the actual output to a RawInline "latex" "\footnote"...

Using Output In markdown Jupyter notebook

I am using Jupyter Notebook to write my report and it would be convenient to include an output in my markdown.
The question below is a simillar question.
Jupyter notebook output in markdown
For example, I have a code cell with the code
In[1]: import random
a = random.randint(1,4);a
and the output was
Out[1]: 2
in my report, I would realy like to include this output just like
'the chosen number was 2'
however, as the 2 is a random number, it would be very convenient to have a way to include the variation a in my markdown like;
'the chosen number was %a'
kind of way.
Is there any way to achieve this?
It's not possible in the main notebook yet (although there are discussions about it) but there is an extension which should suit your purposes:
http://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/python-markdown/readme.html
It's contained within the ipython-contrib-extensions package, for which the install instructions are here:
http://jupyter-contrib-nbextensions.readthedocs.io/en/latest/install.html

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