docker-stacks icon indicating copy to clipboard operation
docker-stacks copied to clipboard

Removing inkscape package from the minimal-notebook

Open Bidek56 opened this issue 3 years ago • 13 comments

Describe your changes

Removing inkscape package from the minimal-notebook as suggested by @benz0li in this issue.

Checklist (especially for first-time contributors)

  • [x ] I have performed a self-review of my code
  • [ x] If it is a core feature, I have added thorough tests
  • [x ] I will try not to use force-push to make the review process easier for reviewers
  • [ x] I have updated the documentation for significant changes

Bidek56 avatar Aug 08 '22 21:08 Bidek56

@Bidek56 For the r-notebook and the datascience-notebook you need to create file /opt/conda/lib/R/etc/Rprofile.site and add either

options(jupyter.plot_mimetypes = c('text/plain', 'image/svg+xml', 'application/pdf'))

or

options(jupyter.plot_mimetypes = c('text/plain', 'image/png', 'image/jpeg', 'image/svg+xml', 'application/pdf'))

depending on how many different formats the notebook file (.ipynb) should include.

benz0li avatar Aug 09 '22 05:08 benz0li

Any reason to use /opt/conda/lib/R/etc/Rprofile.site instead of ~/.Rprofile? Thx

Bidek56 avatar Aug 09 '22 12:08 Bidek56

I do not know much about R but based on this article we can set the plot_mimetypes right in the code. Why would we want to set them globally for all users? Thx

Bidek56 avatar Aug 09 '22 18:08 Bidek56

Why would we want to set them globally for all users? Thx

Setting c.InlineBackend.figure_formats = {"png", "jpeg", "svg", "pdf"} in jupyter_server_config.py sets it globally for the Python kernel (IPython).

Setting options(jupyter.plot_mimetypes = c('text/plain', 'image/png', 'image/jpeg', 'image/svg+xml', 'application/pdf')) in /opt/conda/lib/R/etc/Rprofile.site sets it globally for the R kernel (IRkernel).

benz0li avatar Aug 09 '22 19:08 benz0li

I am only using c.InlineBackend.figure_formats = {"svg", "pdf"} and options(jupyter.plot_mimetypes = c('text/plain', 'image/svg+xml', 'application/pdf')); no need for png and jpeg.

ℹ️ Every format specified is stored in the notebook file (.ipynb).

benz0li avatar Aug 09 '22 19:08 benz0li

Why set these at all? Why not add a documentation line showing users how to add this line to their notebook: options(jupyter.plot_mimetypes = "image/svg+xml")

Bidek56 avatar Aug 09 '22 19:08 Bidek56

Why set these at all? Why not add a documentation line showing users how to add this line to their notebook: options(jupyter.plot_mimetypes = "image/svg+xml")

Removing inkscape without setting these globally will result in errors by default. That should not be the case.

It must work with the default settings.

benz0li avatar Aug 09 '22 19:08 benz0li

  1. I don't like copy-pasting the profile file. I suggest we add it to minimal-notebook.

Are you saying that you are OK with adding /opt/conda/lib/R/etc/Rprofile.site, ~/.Rprofile or neither? Thx

Bidek56 avatar Aug 10 '22 13:08 Bidek56

Why set these at all? Why not add a documentation line showing users how to add this line to their notebook: options(jupyter.plot_mimetypes = "image/svg+xml")

Removing inkscape without setting these globally will result in errors by default. That should not be the case.

It must work with the default settings.

This code works without jupyter.plot_mimetypes:

# Create a plot with some normally distributed data
library("ggplot2")
set.seed(42)
n <- 1000
p <- ggplot(data.frame(x = rnorm(n), y = rnorm(n)), aes(x=x, y=y)) + 
        geom_point(alpha = 0.25, size = 1, colour = "blue") + geom_density2d(colour = "red")

p + ggtitle(sprintf("Mime type = '%s'", getOption("jupyter.plot_mimetypes")))

since by default it seems to be set to image/png and application/pdf according to the docs Can you please share a test case or code sample where it would fail without jupyter.plot_mimetypes? Thx

Bidek56 avatar Aug 10 '22 14:08 Bidek56

  1. I don't like copy-pasting the profile file. I suggest we add it to minimal-notebook.

Are you saying that you are OK with adding /opt/conda/lib/R/etc/Rprofile.site, ~/.Rprofile or neither? Thx

Yes, I'm ok with both options. I prefer /opt/conda/ version, it is unlikely to mess with the user mounts.

Why set these at all? Why not add a documentation line showing users how to add this line to their notebook: options(jupyter.plot_mimetypes = "image/svg+xml")

Removing inkscape without setting these globally will result in errors by default. That should not be the case. It must work with the default settings.

This code works without jupyter.plot_mimetypes:

# Create a plot with some normally distributed data
library("ggplot2")
set.seed(42)
n <- 1000
p <- ggplot(data.frame(x = rnorm(n), y = rnorm(n)), aes(x=x, y=y)) + 
        geom_point(alpha = 0.25, size = 1, colour = "blue") + geom_density2d(colour = "red")

p + ggtitle(sprintf("Mime type = '%s'", getOption("jupyter.plot_mimetypes")))

since by default it seems to be set to image/png and application/pdf according to the docs Can you please share a test case or code sample where it would fail without jupyter.plot_mimetypes? Thx

Unfortunately, I can't help with the example here. @benz0li maybe you could provide one?

mathbunnyru avatar Aug 10 '22 14:08 mathbunnyru

The default for IPython is definitively c.InlineBackend.figure_formats = {"png"} only – plus text/plain, the non-removable default for IPython.

Tested with jupyter/scipy-notebook and

import numpy as np
import matplotlib.pyplot as plt

r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r
fig, ax = plt.subplots(
  subplot_kw = {'projection': 'polar'} 
)
ax.plot(theta, r)
ax.set_rticks([0.5, 1, 1.5, 2])
ax.grid(True)
plt.show()

output
ℹ️ Image size: 277 x 269 pixels, Image DPI: 72 pixels/inch (Retina would be Image size: 555 x 538 pixels, Image DPI: 144 pixels/inch)

Notebook file: No application/pdf in data; "text/plain": ["<Figure size 432x288 with 1 Axes>"]

benz0li avatar Aug 10 '22 19:08 benz0li

The default for IRkernel is jupyter.plot_mimetypes = c('text/plain', 'image/png') with jupyter.plot_scale = 2.

Tested with jupyter/r-notebook and

library(ggplot2)

ggplot(airquality, aes(Temp, Ozone)) + 
  geom_point() + 
  geom_smooth(method = "loess"
)

output

ℹ️ Image size: 840 x 840 pixels, Image DPI: 120 pixels/inch (Due to jupyter.plot_scale = 2 displayed as 480 x 480 pixels, 240 pixels/inch [aka retina] in JupyterLab)

getOption("jupyter.plot_mimetypes")
'text/plain' 'image/png'
getOption("jupyter.plot_scale")
2

Notebook file: No application/pdf in data; "text/plain": ["plot without title"]

benz0li avatar Aug 10 '22 19:08 benz0li

Hope this helps in understanding the current IPython and IRkernel defaults.

And as mentioned before: IJulia is a whole different story...

benz0li avatar Aug 10 '22 19:08 benz0li

What's the status of this PR?

mathbunnyru avatar Aug 16 '22 06:08 mathbunnyru

What's the status of this PR?

I am trying to create tests as per your request but unfortunately I do not know enough about it.

Bidek56 avatar Aug 17 '22 11:08 Bidek56

@benz0li @mathbunnyru I have added basic R tests, unfortunately I do not know enough about ggplot2 to add more advance tests. Feel free to add more sophisticated tests or share them so I can add them. Thanks

Bidek56 avatar Aug 17 '22 16:08 Bidek56

Could you please remove code duplication as well?

mathbunnyru avatar Aug 17 '22 16:08 mathbunnyru

Which code duplication? In the tests?

Bidek56 avatar Aug 17 '22 16:08 Bidek56

The test file, Rprofile.site config file and some lines in Dockerfiles.

mathbunnyru avatar Aug 17 '22 16:08 mathbunnyru

They are used in different stacks, how would you suggest we re-use them across multiple stacks?

Bidek56 avatar Aug 17 '22 16:08 Bidek56

They are used in different stacks, how would you suggest we re-use them across multiple stacks?

As we discussed above, for the config file, let's put it to minimal-notebook. With the test file, just create a test file somewhere with all the code and then import then create two small files in the notebook test folders importing this file content. I think this will work well. Or, put the code in one of two test files and import it in another one.

mathbunnyru avatar Aug 17 '22 16:08 mathbunnyru

They are used in different stacks, how would you suggest we re-use them across multiple stacks?

As we discussed above, for the config file, let's put it to minimal-notebook. With the test file, just create a test file somewhere with all the code and then import then create two small files in the notebook test folders importing this file content. I think this will work well. Or, put the code in one of two test files and import it in another one.

The jupyter_server_config.py config file is in base-notebook, why would we move it to minimal? Rprofile.site belongs to datascience-notebook and r-notebook so it does not make senses to move to move to minimal, what I am missing here? Thx

Bidek56 avatar Aug 17 '22 16:08 Bidek56

Sorry for not being clear.

I propose the following:

  1. jupyter_server_config.py stays in the same place - I do not propose to change it's location.
  2. Rprofile.site should be in minimal-notebook. There are 2 good reasons for this - code duplication and also that this change might break the R users who for whatever reason decided to have their images based on minimal-notebook. This way we will try not to break these users.
  3. minetypes_R.ipynb and test_R_notebook.py should not be duplicated as well - I described how we can avoid the duplication quite easily.

mathbunnyru avatar Aug 17 '22 20:08 mathbunnyru

With the current changes, IPython and IRkernel are not in sync regarding figure formats/plot mimetypes.

Either reduce c.InlineBackend.figure_formats to {"svg", "pdf"} or extend jupyter.plot_mimetypes to c('text/plain', 'image/png', 'image/jpeg', 'image/svg+xml', 'application/pdf'). 👉 Either https://github.com/jupyter/docker-stacks/pull/1765#issuecomment-1209768706 or https://github.com/jupyter/docker-stacks/pull/1765#issuecomment-1209773517.

ℹ️ Every format specified is stored in the notebook file (.ipynb).

benz0li avatar Aug 19 '22 05:08 benz0li