Inserting a Link to a Webpage in an IPython Notebook - jupyter-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:

Related

Export certain jupyter notebook cells to python file

I'm looking for a library/tool that can generate a .py file from a notebook, with the added feature that I want to control which cells (both code and markdown cells) get exported and/or excluded from the .py file. For example adding to cells something like %exclude or %include and have control at export time how to work with these tags. I found jupytext that may do this, but got me confused with the version control part / link of .ipynb/.md.

Can't print jupyter notebook properly

I am working on my office computer, which due to security restriction do not allow me to install program (like miktex, ....). So i decided to export my notebook to .html.
As you can see the rendering is not good:
Some code is cutted
A lot of space is spoiled, there is large blank margin that i would like to see disapear completely, and i do not need the "IN[1]" in front of cell to allow the code to be more visible
How would you do to get an html whose printing display is good ?
I'm not sure if following steps meet your requirements.
(Open the terminal in the corresponding path and then execute the command)
Following command exports the .ipynb file to .html file, and by specifying args(TemplateExporter.exclude_input_prompt and TemplateExporter.exclude_out_prompt), the in[] and out[] is ommitted.
jupyter nbconvert --to html --TemplateExporter.exclude_input_prompt=True --TemplateExporter.exclude_output_prompt=True <your_file>.ipynb
TemplateExporter.exclude_input_prompt=True removes the in[] while TemplateExporter.exclude_output_prompt=True removes the out[] from the generated .html file.

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.

R Notebook: Include figures in report and save plots

I'm using an R Notebook and I'd like my plots to automatically save to disk when the code is run, as well as display inline.
knitr: include figures in report *and* output figures to separate files addresses this for R Markdown but the solution given doesn't work for an R Notebook. Is there a similar option for R Notebooks?
Try setting the knitr fig.path option:
knitr::opts_chunk$set(fig.path = "path/to/figures/")
Where path/to/figures/ is the path to a subdirectory where your figures will be saved. The trailing slash is necessary. This should be a relative path, either relative to the RNotebook file or to the project directory. See here::here() for a handy way to locate the project directory.
This will put each figure into that directory; figure names will be based on the chunk name (so name your chunks!)
This is what eventually worked for me (see #TCZhang 's answer to my question here):
In addition to setting the knitr chunk fig.path="figures/" option suggested by #DonJ, try setting output: html_document, or just press the dropdown next to the Preview [Notebook] button at the top and press Knit to HTML. I think the reason this isn't working is that your output is set to output: html_notebook.
I don't know why this doesn't work specifically when the doc is in R Notebook format. I would also prefer if this worked for output: html_notebook, so it might be an issue we need to open with RStudio or knitr.

how to upload a text file into ipython notebook

I want a whole file as a text file instead of just a cell in IPython notebook.
I write some codes in IPython notebook and now I want to test them ,so I tried to upload some text file into IPython notebook as the raw data.But the files' extension are always ".ipynb" and the format of the text files have changed so my code can't read it correctly.
How could I upload a text file into Ipython notebook? thanks in advance
Can you be more elaborate?
Are you trying to read data out of the text file? In that case regular python open and read functions can get you the data into variables. You can even do Unix "cat" the contents of the file in a variable:
var=!cat file
Or are you trying to get the text in the ipynb files as content in an input cell so you can edit them and run them? The ipynb files are usually created by the notebook engine and they are JSON formatted. So if you just copy paste the text from them into a cell, they won't be usable as is. Use some JSON parsing to interpret their content. They are not regular python code so "%run file" wont work to get their code executed by ipython.
Elaborating the use cases may help others to give solutions...
The following lines open a text file and display the content in a jupyter notebook cell.
text_file = open(full/path/to/text/file)
file_content = text_file.read()
print(file_content)
text_file.close()
I hope this helps.
It may be late, you can do this
%%pycat full/path/to/text/file

Resources