how to control display output size in ipython - jupyter-notebook

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).

Related

Jupyter add markdown heading within loop

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

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 do I import data via code in R (Instead of using the import in the menu bar) from code typed into an R notebook?

Every time I type in the file name in this case "labelled edited.xlsx" (perfectly - it was copied from the import box when using the import function from the menu into an R notebook), then try to run it, it says 'Error: path does not exist'. However using the import menu works. If I copy and paste the exact same thing from the import box:
labellededited <- read_excel("labelled edited.xlsx", col_names = TRUE, .name_repair="minimal")
into the notebook and run it immediately, it works perfectly. However, when I close R, open it again, set the working directory (without changing a single thing in the directory folder so the file names are the same), it returns the error even though absolutely nothing has changed - I just restarted R.
In addition to this, copying the code from the notebook into the import box on the bottom right will import the dataset perfectly, as does copying the line of code into the console. It only happens when I press cmd+enter directly from the notebook.
Any tips on fixing this? I know it's not a big deal, but ideally, i'd like to create a code, set the directory and then just let it run.
The problem has to do with RStudio and file types. In order to use the keyboard shortcuts (Ctrl+Enter) the commands have to be saved as an R script file. So start a new one (Ctrl+Shift+N) and copy the commands from the .Rmd file, and try again.
Hi you can use this i guess,
set working directory using setwd("your Path/") then
library(readxl)
if you want to import xlsx use read_xlsx , if you want to import xls use read_xls
labellededited <- read_xlsx("labelled edited.xlsx",sheet = "select sheet number"(default it will consider as first sheet)
more better way you can keep path inside the code and import the file(if you don't move the file it will import without any error)
labellededited <- read_xlsx("yourpath/labelled edited.xlsx",sheet = "select sheet number")
Hope it helps

Using Bokeh, Jupyter Notebook and Pelican for interactive plots

I created a jupyter notebook with interactive plots using Bokeh.
An example notebook looks like this:
import pandas as pd
import numpy as np
from bokeh.plotting import figure, show
from bokeh.charts import ColumnDataSource
from bokeh.io import output_file
from bokeh.models import HoverTool
df = pd.DataFrame(np.random.normal(0,5,(100,2)),columns=['x','y'])
output_notebook()
source = ColumnDataSource(df)
hover = HoverTool(
tooltips=[
("x", "#x"),
("y", "#y"),
]
)
p = figure(plot_width=800, plot_height=500, tools=[hover])
p.circle('x', 'y', size=7, fill_alpha=0.5,source=source)
show(p)
Things work on the notebook itself and the figure is interactive.
I'm using pelican static website generator with the pelican-ipynb plugin (https://github.com/danielfrg/pelican-ipynb) in order to convert the notebook to html. When the html is created the Bokeh plots don't show up. I can't seem to figure out how to get an html with the interactive Bokeh plots. I inspected the html and there is nothing after the show(p) line.
How do I get the Bokeh plot work with pelican?
Bokeh uses JavaScript for the client-side of the interactive part (BokehJS) and that JS code is not embedded when you export the notebook to HTML ouside Bokeh.
You need to export the Bokeh code to HTML using Bokeh’s own functions to generate standalone HTML.
To generate the HTML, simply add to your code:
from bokeh.resources import CDN
from bokeh.embed import file_html
p_html = file_html(p, CDN)
For an example, see this Jupyter Notebook with your original code and the generated HTML (too long to embed it here in SO, about 45 K characters for your simple example).

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