put 3 lines in default or startup

75 views
Skip to first unread message

Ryan Krauss

unread,
Aug 22, 2016, 10:08:20 AM8/22/16
to Project Jupyter
I am having a hard time getting a good answer to what I think is a simple question.  I want my students to try the jupyter notebook and I think we will pretty much always need these 3 lines at the start of every notebook file:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

What is the easiest way to prevent them from having to re-type those lines every time?  Keep in mind that many of my students will be learning python from scratch as we start my class.

Thanks,

Ryan

Adrian Petrescu

unread,
Aug 22, 2016, 10:57:55 AM8/22/16
to jup...@googlegroups.com
You can add it to the Jupyter server's profile. See this Stack Overflow answer for pretty much your exact scenario.

Cheers!
-Adrian

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/e7047efc-7a68-4ee2-9aa9-aa912ee81df5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ryan Krauss

unread,
Aug 22, 2016, 11:21:40 AM8/22/16
to Project Jupyter
It feels like these things were well documented and well understood under ipython and are now less clear under jupyter.  I have the following lines in a config file ~/.jupyter/jupyter_notebook_config.py

c = get_config()
# ... Any other configurables you want to set
c.InteractiveShellApp.matplotlib = "inline"
c.InteractiveShellApp.exec_lines = ['import matplotlib.pyplot as plt',
    'import numpy as np',
]

They seem to have no effect.  By that I mean that I get a NameError when trying to use np without an explicit import:

t = np.arange(0,1,0.01)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-0268b93691c7> in <module>()
----> 1 t = np.arange(0,1,0.01)

NameError: name 'np' is not defined


On Monday, August 22, 2016 at 10:57:55 AM UTC-4, Adrian Petrescu wrote:
You can add it to the Jupyter server's profile. See this Stack Overflow answer for pretty much your exact scenario.

Cheers!
-Adrian
On Mon, Aug 22, 2016 at 10:08 AM, Ryan Krauss <ryan...@gmail.com> wrote:
I am having a hard time getting a good answer to what I think is a simple question.  I want my students to try the jupyter notebook and I think we will pretty much always need these 3 lines at the start of every notebook file:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

What is the easiest way to prevent them from having to re-type those lines every time?  Keep in mind that many of my students will be learning python from scratch as we start my class.

Thanks,

Ryan

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.

Doug Blank

unread,
Aug 22, 2016, 11:43:06 AM8/22/16
to Project Jupyter
Perhaps having these loaded automatically is not the right approach. I appreciate that those three lines have nothing to do with what you want to teach. But magically having "np" and "plt" available without doing anything can also be confusing, especially if the students move to a different environment, or if you want to share your materials with others that have a standard environment.

I would reconsider, and perhaps just import those when you need them. Or, create a standard way of loading them (maybe a standard magic, or create your own). Maybe you want to create your own python module around matplotlib, to make it even easier?

-Doug
 

Thanks,

Ryan

Fernando Perez

unread,
Aug 22, 2016, 12:06:59 PM8/22/16
to Project Jupyter

On Mon, Aug 22, 2016 at 8:43 AM, Doug Blank <doug....@gmail.com> wrote:
I would reconsider, and perhaps just import those when you need them. Or, create a standard way of loading them (maybe a standard magic, or create your own). Maybe you want to create your own python module around matplotlib, to make it even easier?

I concur... In a pinch, `%load /path/to/snippet.py` can do the job, though one could imagine a `%snippet` magic that would not leave the original command in the cell and would work off a system-wide snippet store rather than explicit paths on disk.

Using %load as a guideline, writing such a `%snippet` magic is left as an exercise for the reader :)

--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

Ryan Krauss

unread,
Aug 22, 2016, 8:53:34 PM8/22/16
to Project Jupyter
I more or less agree that having np and plt magically available can be confusing.  That is why I like the spyder template approach.  It is super easy to have every new script that you create automatically start with

import matplotlib.pyplot as plt
import numpy as np

or whatever you want. 

Is there anything like a user configurable template for ipython notebooks?

There seem to be many answers on stackexchange or wherever talking about how people use to do the automatic importing for IPython before Jupyter.  Is this functionality that has been deliberately removed?

Thanks,

Ryan

Thomas Kluyver

unread,
Aug 23, 2016, 4:42:17 AM8/23/16
to Project Jupyter
On 23 August 2016 at 02:53, Ryan Krauss <ryan...@gmail.com> wrote:
Is there anything like a user configurable template for ipython notebooks?

Not yet, there's an issue where we're discussing it.
https://github.com/jupyter/notebook/issues/332
 
There seem to be many answers on stackexchange or wherever talking about how people use to do the automatic importing for IPython before Jupyter.  Is this functionality that has been deliberately removed?

It hasn't been removed. IPython, as the Python kernel for Jupyter, still provides the same config settings to run bits of code when it starts, including a directory of 'startup files'.
http://ipython.readthedocs.io/en/stable/interactive/tutorial.html#startup-files

Ryan Krauss

unread,
Aug 23, 2016, 8:46:25 AM8/23/16
to Project Jupyter
Thank you.  That is helpful.  I now have my magical imports working all the time.  The only thing that isn't working is having %matplotlib inline called only for the notebook.  My normal use cases are running from a terminal or using spyder where I do not want inline.

I have put these lines in ~/.jupyter/jupyter_notebook_config.py but it doesn't work. 

c = get_config()
# ... Any other configurables you want to set
c.InteractiveShellApp.matplotlib = "inline"

Why doesn't that work or what is the right approach?

Thanks again,

Ryan

Thomas Kluyver

unread,
Aug 23, 2016, 9:40:12 AM8/23/16
to Project Jupyter
On 23 August 2016 at 14:46, Ryan Krauss <ryan...@gmail.com> wrote:
I have put these lines in ~/.jupyter/jupyter_notebook_config.py but it doesn't work. 

c = get_config()
# ... Any other configurables you want to set
c.InteractiveShellApp.matplotlib = "inline"

Setting matplotlib to load on startup is a Python specific thing, so it's configured as part of IPython, not Jupyter. jupyter_notebook_config.py is for configuring the notebook server, and it doesn't directly affect kernels started by Jupyter.

You could create a second kernelspec for IPython (http://ipython.readthedocs.io/en/stable/install/kernel_install.html ), and manually add to the command line args it uses to set the matplotlib option.

Ryan Krauss

unread,
Aug 24, 2016, 11:45:39 AM8/24/16
to Project Jupyter
OK.  Thanks.
Reply all
Reply to author
Forward
0 new messages