PyCharm: Show .png in markdown cell in Jupyter notebook file - jupyter-notebook

I am using PyCharm 2019.2 Professional and I am trying to figure out how to display a .png file within a markdown cell. My file is located in a subfolder called screenshots.
I tried the following line of code; however, PyCharm will not display the image. I just see an empty rectangle.
![test.png](screenshots/test.png]

After 4 short years, JetBrains finally fixed this issue in PyCharm 2021.3 EAP. The patch will not be ported to 2021.2 or earlier versions. See this thread.
If you're using an older PyCharm version and don't feel like updating right now, here is a dirty hack to print images on a grid in your notebook in PyCharm using matplotlib:
import matplotlib.pyplot as plt
f, ax = plt.subplots(1, 3)
ax[0].imshow(plt.imread('iguanas1.png'))
ax[1].imshow(plt.imread('iguanas2.png'))
ax[2].imshow(plt.imread('iguanas3.png'))

Related

Shift Tab for showing the documentation of a command in Jupyter Notebook is not working

I have a problem in using the shortcut 'shift tab' in order to get more informations of the package or command I am typing in in a cell. I installed Jupyter notebook via anaconda very recently, I am using python 3.7 and Ubuntu 18.04.
Do you know how to fix this problem ? I googled a lot but could not find a solution.
Many thanks.
Let's say you wrote the below code and trying to get the signature/documentation of function read_csv() with Shift+Tab (It may not work some times)
Code:
import pandas as pd
pd.read_csv()
-> First type only below code
pd.read_csv? ## Execute this code with Shift+Enter
-> Now when you type pd.read_csv and type Shift+Tab, this will show u signature/documentation... This is just a workaround...
Follow two steps:
Step 1: Run that cell first (shift + enter)
Step 2: After running the cell, press the shift + tab.
It worked for me. I hope it will work for you too :)
In the Google Colab environment, if fixed it as follows:
Tools | Settings | Editor |uncheck Automatically trigger code inspection.
Then, Tab and Shift-Tab worked as expected.
I also faced a similar problem but can you confirm that you imported the library in the jupyter notebook and then were calling one of the methods of the library?
What I observed is that if the library wasn't imported into the notebook then the documentation wasn't also showing using Shift+Tab. Once I imported it, then the shortcut was working to show the documentation.
Scenario 1:
import numpy as np #Pressed Enter for next line
a=np.random.randint #Shift + Tab not working
Scenario 2:
import numpy as np #Shift + Enter
a=np.random.randint #Shift + Tab working
Just Run the tab of your code
Then bring your curser in the parenthesis and press shift + tab
press shift + tab + tab for more info
On Google colab:
clicking on the function after running,
move the cursor away and then
bring the mouse and hover it over causes it to pop up.
Note: colab Tools | Settings | Editor | check Automatically trigger code inspection (first setting)
Run again your line of importing the libraries. Now, having loaded your modules you should be able to see the command documentation.

Persistent plotly plots in jupyter notebooks

My jupyter notebooks that have plotly plots do not retain the plots between sessions.
This is running on a Singularity container based on the official jupyter/datascience-notebook docker image with plotly pip installed on top.
I am using the new renderer framework with the notebook renderer.
My notebooks are trusted.
My plots show up during the session without issues.
They persist across refreshes and reloads of the same notebook, even if I restart the kernel.
They disappear either when I restart the jupyter server or sometimes when I reboot the client machine and come back with a new browser session.
The output cells persist with the correct dimensions, but they are blank.
I can see that a whole bunch of js is embedded in the notebook but it does not render in the browser.
At this point, even if I nbconvert to html, they still do not show up.
Tried with Chromium and Firefox.
import plotly.graph_objects as go
import plotly.io as pio
import plotly.express as px
pio.renderers.default='notebook'
then later I plot a bunch of things like:
go.Figure(go.Scattergl(x = var1, y= var2, mode='markers', marker_size=1))
and
go.Figure(go.Histogram2dContour(x = var1, y= var2))
My understanding is that I am set up to retain these figures in offline (non-running) notebooks; the js generated for the plots and the entirety of plotly.js library appears to be embedded in each notebook adding up to 10s of MBs, but they are not rendered.
Due to this issue I end up having to re-run (sometimes expensive) notebooks when all I need is to take a look at a previous plot.
As a recent matplotlib/seaborn convert I absolutely love the interactivity but this is quickly becoming a showstopper at this point.
I feel like I'm missing something.
Any advice is appreciated.
For me, the following solution worked :
in the environment you're using : install orca :
conda install -c plotly plotly-orca
at the beginning of your notebook : override default renderer as 'notebook' :
import plotly.io as pio
pio.renderers.default='notebook'
plotly graphs were persistent between sessions (ie : after restart of kernels) with these modifications.

How to Hide Code cells When converting a Jupyter Notebook into Sphinx Doc using nbsphinx

I'm new to Sphinx documentation, and I am trying to convert a Jupyter Notebook into Sphinx docs using Nbsphinx. Especially, I want to hide the code cells (not their outputs), but I could not make it using templates that I found for Jupyter Nbconvert.
I expect the outputs from converted notebook on the Sphinx documentation, but no input.
I added the following code to the first cell and it works well.
import IPython.core.display as di # Example: di.display_html('<h3>%s:</h3>' % str, raw=True)
di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True)'

Atom and Hydrogen: Output and console

I'm a Python beginner and recently came across the Atom editor and the package Hydrogen, that implements the Jupyter notebook. I did so after realizing that running the notebook in Chrome consumed way too many resources and also seemed to be a bit slower.
However, the Atom editor and Hydrogen always output prints within a little frame in the code (see image). Unfortunately, it doesn't use the full window width. I also don't see any console/terminal for installing pip libraries.
Is there a way to have the output in a console below the code, just as in Jupyter, and to have a terminal?
this comes most certainly too late, but in case someone else searches for this:
hit control+shift+P / cmd+shift+P and type "toggle output area".

Jupyter Notebook save to PDF without Code

This has been an ongoing issue for me, as I would love to use Jupyter Notebook to write my research reports, but have found it very difficult to export my Jupyter Notebooks to PDF without code and without large formatting errors.
I am able to download the notebooks as PDF, but have not found a way to hide the code, or have the PDF resemble the formatting of the notebook.
No solution I've found on SO has been sufficient for my issue, so it may be possible that this is not the intended functionality of Jupyter Notebook.
Have you tried this:
jupyter nbconvert path/to/your/ipynb --to=pdf --TemplateExporter.exclude_input=True
For more on the flags, you can refer to nbconvert config options
I have successfully converted .ipynb to .html exclude the code blocks
I followed the suggestion by 0xffff above but the PDF output did not behave as intended (code blocks were still included).
However, their suggestion inspired me to try converting to html first using the following call:
jupyter nbconvert path/to/your/ipynb --to=html --TemplateExporter.exclude_input=True
This behaved as intended, and from there, it was straightforward to print the output to PDF in a browser.
Regarding the issue with the --to=pdf flag, I've opened up an issue on the Jupyter Notebook git repo: https://github.com/jupyter/notebook/issues/3804. Will report back once I get a response.
nbconvert
nbconvert is an official package, and other answers have mentioned it. However, they did not mention there are two ways to convert to PDF.
The default option uses LaTeX, and it's often a pain to install:
jupyter nbconvert notebook.ipynb --to=pdf
A newer option is to use the webpdf converter, this does not require LaTeX
jupyter nbconvert notebook.ipynb --to=webpdf
Quarto
Quarto is the newest option. The defaults are prettier than nbconvert, and it has advanced features to customize the output PDF. However, it uses pandoc (and pandoc uses LaTeX), so you must install both first.
quarto render notebook.ipynb --to pdf
Quarto is a new project, and I often run into problems (I couldn't convert the sample notebook); however, the project is in active development and I'm sure it will get better.
Online converters
The last option is to use one of the many online services. The two main caveats are 1) You might not want to upload your notebooks to some random website, and 2) It's often unclear what engine they use to convert the notebooks. I've tried many of them with mostly negative results.
I created one online converter that uses nbconvert and deletes your notebook as soon as it's converted.
If you manually create the PDF, there is a simpler solution: add the following code block & execute it
from IPython.core.display import HTML
HTML('<style>.input, .jp-InputArea {display: none !important}</style>')
Code will disappear both in the "normal" UI and in print preview / export to HTML. Open print preview, generate the PDF - it looks great now, clear the output of this one cell to bring the code back, done

Resources