I want to use ipywidgets.interact to show Pillow images similar to what someone already did here. However, in my case Jupyter would not render the image. Instead it looks like this:
If I output the image separately into a cell then it displays. Does anyone know how I can fix this?
I suspect you didn't have your environment set up quite right with the matching versions. Or maybe not imported everything needed.
If you go here and run that code it will work because the environment served via MyBinder.org specified here has ipywidgets and current versions of everything necessary. (It actually isn't listed there but voila has it as a dependency and so it's installed in the build; you'll see it listed if you run %pip list in a cell in the notebook.)
When that notebook shows up after a few seconds, execute the entire thing by selecting Run > Run All Cells from the toolbar menu. Importantly, You won't see the adjustable image when you first open the notebook, without running it actively there.
After running it, now the slider should work for adjusting.
The code that I used with ipywidgets.interact to show a Pillow image so that it's adjustable:
%matplotlib inline
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
from PIL import Image
img = Image.open('picture.png').convert('L')
#interact
def binarize(th: (0, 255, 1)):
return img.point(lambda p: 255 if p > th else 0)
I just tried it and that %matplotlib inline isn't needed at this time, consider it optional.
Related
I know that I can click on File -> Print Preview and let my browser save the result as a PDF. But that PDF will contain quite a bit of code.
Can I hide parts from the print preview or make a PDF with another tool?
Ideally, there would be magic functions:
%hide: Hide the cell and its output
%hide-code: Hide only the code of the cell, but show the output of the code
Print to PDF in the browser
In JupyterLab, you can hide the cell's input and output by clicking the side bar as demonstrated in the doc. After hiding, you can use the browser's printing menu to generate a PDF. I've tested it on FireFox and expect similar result on Chrome or Edge.
Export to PDF with nbconvert
If you aim at a native conversion from ipynb format to pdf, the nice old nbconvert tool is your friend. It usually comes together with Jupyter and can be invoked conveniently in the menu of JupyterLab File->Export Notebook As...->PDF or, in the classic Jupyter Notebook interface, File->Download as->PDF (via LaTeX).
Hiding input/output in cells can be realized by setting the cell's metadata hide_input=true and installing a nbextension as discussed in issue #155.
Print Preview menu in the classical Jupyter
Finally to your question
Can I hide parts from the print preview or make a PDF with another tool?
The "print preview" button in the classical Jupyter interface is a shortcut for calling nbconvert to generate a HTML file and redirect your browser to it. Therefore, similar configuration for nbconvert to hide input as discussed above (in issue #155) can be used. This feature is implemented by jupyter_contrib_nbextensions bundle and explained in the doc.
As the above-mentioned answers do not work for me; I found another solution:
I use an additional package notebook-as-pdf.
Read the tutorial here. It uses Chromium and is the only approach that worked for me so far reliably. The "print preview" does still not work. But it produces a clean PDF with no code input.
I do:
pip install -U notebook-as-pdf
pyppeteer-install
jupyter nbconvert --to PDFviaHTML --TemplateExporter.exclude_input=True PATH_TO_YOUR_FILE.ipynb
What did NOT work for me:
The solution above.
All the scripts mentioned in those answers
I am running Python 3.7 on Windows using pycharm. I have a jupyter notebook and I would like to embed an image into the notebook. I know all the ways of doing standard embedding with markdown language, BUT ideally what I want is:
a. Embed the image via markdown language, i.e. the notebook cell is in 'markdown' state, not 'Code' state, AND ALSO
b. Have it able to export to HTML and retain that image in the HTML file. i.e. on the notebook. I want to click File -> Download as -> HTML (.html), save the notebook file in .html format, and then when I send it to my buddy, the image that I attached is in the notebook, and he sees it.
I know i could do this in a cell ('code'):
from IPython.display import Image
Image(filename="myfile.jpg")
but I do not want to use 'Code', since when I send to my buddy, he will see the In [] code statement and the Out [] of the image in the notebook html file.
Note: This would be an image that was on my laptop that I would want in the html formatted exported notebook. It is NOT on the web where he could refer to it with a www type statement. Unless I'm crazy, there is no way to do this with markdown command in a cell, the only way to do it (with the image embedded 'permanently' into the .html format of the notebook), would be via a cell that was in 'Code' celltype.
When you use a code cell to show an image and then export the notebook to an HTML file, the image is converted to Base64 and the code directly used in the src attribute of the <img> tag. You can apply the same procedure with images included in markdown cells.
First, encode your image as Base64, e.g. by using one of the online enocders.
Create a markdown cell and include an <img> tag which uses your Base64 code, e.g.:
<img src="data:image/png;base64,CODE_FOLLOWS_HERE" />
Evaluate the cell and you should already see your image.
If you now export your notebook to HTML, the image should be included in the file the same way as images from code cells.
The only disadvantage with this approach is that your markdown cell gets cluttered with the (probably long) Base64 code. However, this is manageable by e.g. using a markdown cell dedicated solely to the image without other content.
You can install the Unofficial Jupyter Notebook Extensions.
It has some interesting extensions (e.g. spell checker, collapsible headings, ...). One of the extensions is Export HTML With Embedded Images which exactly does what you want.
To install Nbextensions using pip do the following:
$ pip install jupyter_contrib_nbextensions
$ pip install jupyter_nbextensions_configurator
$ jupyter contrib nbextension install --user
$ jupyter nbextensions_configurator enable --user
Then you will see in your Jupyter homepage a new tab (Nbextensions), where you can enable and configure different extension.
After enabling the "Export HTML With Embedded Images", you will see the corresponding option in the "File-Download as" menu.
My complete solution is based on Milania and
encoding-an-image-file-with-base64
how-to-base64-encode-an-image-using-python
BytesIO.getvalue
the code
import base64, io, IPython
from PIL import Image as PILImage
image = PILImage.open(image_path)
output = io.BytesIO()
image.save(output, format='PNG')
encoded_string = base64.b64encode(output.getvalue()).decode()
html = '<img src="data:image/png;base64,{}"/>'.format(encoded_string)
IPython.display.HTML(html)
For me, on Visual Studio Code, something like this did the trick (in a markdown cell, as you requested, and a image that you want to embed in your notebook and further be exported to the html output):
<figure>
<img src="./notebook_img/diptera_taxat_yes_no.jpg" width="200"/>
<figcaption>Limit the search on Diptera</figcaption>
</figure>
Where the image is located in "./notebook_img" relative to the location of the notebook (in this sense, the notebook is located in .)
Your buddy will not see the code from above when reading the HTML exported file, so that should satisfy the requested need as far as I understand. He will also not need the folder "notebook_img".
I'm using the Base16 Ocean dark theme on IPython Notebook.
The background color of selected text doesn't contrast very well, making it difficult to tell if it's selected:
On the default settings, this does not occur:
Please let me know how to change the background color of selected code on IPython Notebook to a more clear one.
Run the following code in a notebook to find out the jupyter config folder on your system, if you don't know it yet:
from jupyter_core.paths import jupyter_config_dir
jupyter_dir = jupyter_config_dir()
print(jupyter_dir)
In the jupyter config folder, find /custom/custom.css, modify the following line:
.cm-s-ipython div.CodeMirror-selected {background: #384151 !important;}
I use '#3366ff' here, you may use whatever background color that suits your need.
All is well with jimmyazrael answer, but if you're using Jupyter 5.0 from within Anaconda3 you will find your css file at:
C:\Users\YourUsername\Anaconda3\Lib\site-packages\notebook\static\cutom\custom.css
Hope that helped
Mine was I bit more complicated. Couldn't find it in any mentioned folder, thus after searching my system for "custom.css", I have found many of this! It is likely because I had upgraded/downgraded my environment multiple times.
So, in the end, I open the file in:
C:\Users\username\AppData\Local\Continuum\anaconda3\pkgs\notebook-6.1.5-py37haa95532_0\Lib\site-packages\notebook\static\custom\custom.css
Pasted the line:
.cm-s-ipython div.CodeMirror-selected {background: #384151 !important;}
Saved and restarted Jupyter. It worked!
I am trying a sample code that is supposed to use IPython and display a html file.
Here is part of the code that is relevant to my question:
from IPython.display import IFrame
from IPython.core.display import display
display(IFrame('myfile.html', '100%', '600px'))
But when I run it, nothing shows up. What could be the reason?
Do I need to install something related to d3 or d3js?
Your code works in a Jupyter notebook (it doesn't display anything when tested in a terminal running IPython) - It will not display anything if the file is empty or contains malformed HTML. If the file doesn't exist you'll see a 404 displayed in the IFrame.
After Googling for hours, I didn't find any answer for the following issue and so glad if anyone could help.
I use Ubuntu 12.04 LTS with Gnome-shell 3.4.1. Consider the following simple program in file $HOME/a.py which I have made it executable:
#!/usr/bin/env python
import gtk
w = gtk.Window()
w.set_size_request(250, 150)
w.set_title("test program")
w.connect('destroy', lambda x: gtk.main_quit())
w.show_all()
gtk.main()
I've used MenuLibre to define a_run0.desktop in $HOME/.local/share/applications as below:
[Desktop Entry]
Version=1.0
Type=Application
Name=a_run
Comment=comm.
Icon=applications-development
Exec=/home/vsop/a.py
Path=
Terminal=false
StartupNotify=true
Categories=
Name[en_US]=a_run
Comment[en_US]=comm.
I made a_run0.desktop to be executable and I see a file named “a_run” in $HOME/.local/share/applications with proper icon (Icon=applications-development
). Running the program and using alt-tab, the icon is also shown in alt-tab menu quite well.
The question is, when I copy “a_run” file (actually a_run0.desktop) to $HOME/Desktop, the file is shown with correct icon in Desktop but after running it, the icon shown in alt-tab menu is changed to unknown-red-circle-icon with the name of original file “A.py” underneath.
What is made this problem and how can I see the defined-icon (in .desktop file) correctly in the alt-tab menu after running the program from $HOME/Desktop?
Try to add icon to your window using python and gtk. I think that icon in .desktop file only specify how .desktop file is shown in system not application.
Certainly in more recent versions of Gnome shell (I'm using 3.14.1) the entry Icon should point to the actual icon you want to use, e.g
Icon=/home/vsop/a_icon.xpm