Importing Jupyter Notebooks as Modules

1,302 views
Skip to first unread message

D Stubz

unread,
Mar 21, 2016, 3:45:43 PM3/21/16
to Project Jupyter
I'm thinking this would be the place to ask some general questions on getting things done ...

I managed to get the notebook listed in the subject to do what it says it is supposed to ... I was able to import a known .ipynb from a known directory


Wonderful, but now I basically have the code from that notebook, inside my 'examples##.ipynb' ...

I'd like to be able to just import desiredNotebook.ipynb into a working notebook, 
which is what I thought it would do, but without all the code present ...

I going to guess I need to build a package / register the python code some where ... or

I figure I'm just missing a step, or several, to actually achieving this 

I'm not really a programmer, but I'm working at it ...

The general jist of what I'm trying to do is access classes/functions from one notebook into another 

D Stubz

unread,
Mar 21, 2016, 3:57:15 PM3/21/16
to Project Jupyter
I guess I also should mention, I'm using Conda and have imported jupyter notebook 

I'm thinking this is what caused me some grief getting the tutorial to run at all
( the path=None was altered to the Top Lvl cwd path=/home/user/Projects/ )

Carol Willing

unread,
Mar 21, 2016, 4:48:16 PM3/21/16
to Project Jupyter
> --
> 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.
> 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/41f5a61a-f704-4d1d-9a4a-f9f8777421e2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

*** Jupyter Documentation —> Nbviewer website to view and download the
notebook ***


Clicking this link will open the example notebook as a documentation
test file:

https://jupyter-notebook.readthedocs.org/en/latest/examples/Notebook/rstversions/Importing%20Notebooks.html


At the top there is a link on the page that says “View the Original
Notebook on nbviewer”. When you click that link it will take you here:

http://nbviewer.jupyter.org/github/jupyter/notebook/blob/master/docs/source/examples/Notebook/Importing%20Notebooks.ipynb

to the nbviewer that let’s you view the notebook and download the
notebook to your computer. To download the notebook to your computer,
click the button in the upper right that looks like a “down arrow”.

The notebook (Importing Notebooks.ipynb) will be saved in your Downloads
directory

*** Opening and running the notebook on your computer ****

Now that the file is on your computer, you should open the Jupyter
notebook on your computer (jupyter notebook is the command). At the far
right of the notebook screen is a button called “Upload”. Clicking
the Upload button will allow you to select the (Importing
Notebooks.ipynb) file from your Downloads directory. To complete the
Upload you must click the blue Upload button to the right of the
notebook file name.

You will now see the notebook in the list of folders and files. Click on
the file name to open the notebook file. To run the cells in the
notebook, you have a few options:

- a) you may click SHIFT-ENTER on each cell and the cell will run the
code inside it
- b) from the menu CELL -> RUN ALL will run all the notebook cells and
display output

While you can’t automatically execute code from another notebook in
this notebook, you may copy cell from one notebook to your notebook.

As an FYI, you can download many notebooks from GitHub too. If you have
more questions, please do ask.

Warmly,

Carol


Carol Willing
Research Software Engineer, Project Jupyter @ Cal Poly
Director, Python Software Foundation

Carol Willing

unread,
Mar 21, 2016, 4:51:56 PM3/21/16
to Project Jupyter
On 21 Mar 2016, at 12:57, D Stubz wrote:

> I guess I also should mention, I'm using Conda and have imported
> jupyter
> notebook
>
> I'm thinking this is what caused me some grief getting the tutorial to
> run
> at all
> ( the path=None was altered to the Top Lvl cwd
> path=/home/user/Projects/ )
>
>

The directions that I sent in my last message should be very similar
when using Conda.

Hope the last email helps :-)
> --
> 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.
> 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/dc54bf89-47a2-420b-9d68-fa2e3530f770%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


Peter Parente

unread,
Mar 21, 2016, 5:02:11 PM3/21/16
to Project Jupyter
Check out https://github.com/jupyter-incubator/contentmanagement which codifies the Importing Notebooks approach in two forms: importable notebooks and notebooks as snippet libraries.


Pete

D Stubz

unread,
Mar 21, 2016, 5:21:45 PM3/21/16
to Project Jupyter
Thanx Carol, I am a newbie, so bare with me ...

I think I'm missing a little something ... In as much as I know I can download notebooks, 
I simply copied and pasted the bits of code into a notebook of my own,
and trouble shot things until it was happy after a Ctrl-Enter

I've been some what successful in using pyCharm and importing modules, then accessing code/classes/functions/dictionaries from a different module
(This is what I thought this particular notebook was supposed to do ... )

I've been less successful working in a IPython/Jupyter notebook and having code run successfully ...
Sometimes when I fire things up, the notebook I had created, doesn't run without grief ... It ran, last time I was in it ¿
The 2 environments seem to act some what differently to when/where/how variables are created 
and what a Run command in PyCharm does vs Ctrl-Enter or Cells->Run all does in Jupyter ...

With respect to the notebook -> importing notebook, I managed to get the code to work ...

Now all I want to do, is be able to import a notebook, I don't want all that code for importing a notebook in the same notebook ...
(Which is what I thought the whole point of creating that notebook and code was for, so I can reuse it, without copy and pasting all over again ...
Like importing a module in PyCharm )

Am I missing something ?





On Monday, 21 March 2016 13:45:43 UTC-6, D Stubz wrote:

Thomas Kluyver

unread,
Mar 21, 2016, 5:30:47 PM3/21/16
to Project Jupyter
Hi,

While it's possible to import one notebook from another, we don't really recommend doing it - that's why we haven't turned this example code into a convenient package for people to use. In our experience, the way people write notebooks doesn't tend to lead to code that works well as a module.

The way most people do it is to start with code in a notebook, and then copy functions and classes into regular Python modules as they become more stable. This is a bit of an inconvenience, but there's a new interface in the works that will hopefully facilitate it by making it possible to have notebooks and text editors in the same page.

Best wishes,
Thomas


--
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.
To post to this group, send email to jup...@googlegroups.com.

Carol Willing

unread,
Mar 21, 2016, 5:59:36 PM3/21/16
to Project Jupyter
On 21 Mar 2016, at 14:21, D Stubz wrote:

> Thanx Carol, I am a newbie, so bare with me ...

No problem. We’re all new at something :-)

>
> I think I'm missing a little something ... In as much as I know I can
> download notebooks,
> I simply copied and pasted the bits of code into a notebook of my own,
> and trouble shot things until it was happy after a Ctrl-Enter
>
> I've been some what successful in using pyCharm and importing modules,

Let’s clarify what you mean by modules. Are you trying to use multiple
packages or libraries? Like requests with pandas. Or scipy and
matplotlib together?

For now I will assume that you are wishing to use two or more libraries
together, i.e. scipy and matplotlib. Using these together in a notebook
is a two step process:

1) Verify that scipy and matplotlib are installed in your conda
environment or using pip. To do this the notebook does not need to be
open or running.

2) Now that step 1 is done, you will run the Jupyter notebook in a web
browser. To use the scipy and matplotlib libraries and the functions
within them, you will need to import them in a cell:

For example,

import scipy
import matplotlib
import requests
import pandas

could all be executed in the same cell or multiple cells.

In later cells, you can call different functions. Here are a number of
example notebooks:
https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks
to get a flavor of how things work.
Personally, I find Anaconda and conda the easiest way to get started,
and it’s what I recommend when teaching the Notebook use.

PyCharm, while a nice product, is not what I would recommend a new
Notebook user to begin with. Once, you have the Notebook workflow down;
then, it’s a reasonable tool.

Good luck!

Please feel free to ask follow up questions since it helps us improve
our user materials for teaching :D
> --
> 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.
> 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/a38c5067-f3b8-4785-8ab3-23c444978e4e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


D Stubz

unread,
Mar 21, 2016, 6:02:35 PM3/21/16
to Project Jupyter
Thanx Thomas,

This seems to be the behaviour I've been experiencing ... 

My general idea:

I have a bunch of constant data and functions, that I would like to access 
and basically apply a scalar(s) to this given data, 
creating new data sets, that know exactly where and what it was, before it was scaled ...

By placing data/data sets in different .py/ipynb modules and using descriptive variables and namespaces,
the underlining code should tell about self while being inspected.
Different datatypes should get assigned to different data sets, at creation or time of scaling,
such that when blaze/pandas is used to inspect scaled results and then plots it,
I can have a visual of the data set.

I was kinda hoping to wrap all this up in a notebook, as well as get access to it an interactive python session ...

It kinda sounds like I'll have to do this one data set/function at a time
and wrap it all up in PyCharm as I get the individual pieces working ...





On Monday, 21 March 2016 13:45:43 UTC-6, D Stubz wrote:

Thomas Kluyver

unread,
Mar 21, 2016, 6:12:17 PM3/21/16
to Project Jupyter
On 21 March 2016 at 22:02, D Stubz <stub...@gmail.com> wrote:
I have a bunch of constant data and functions, that I would like to access 
and basically apply a scalar(s) to this given data, 
creating new data sets, that know exactly where and what it was, before it was scaled ...

I'm not exactly clear on what you're going for here, but it sounds like you're talking about provenance - tracking which code and data are used to generate particular results. That's not an area I know much about, but two tools I've come across are NoWorkflow and Sumatra, and I know NoWorkflow has some IPython integration. You might want to look into those.

Best wishes,
Thomas

D Stubz

unread,
Mar 21, 2016, 6:26:20 PM3/21/16
to Project Jupyter
Thanx again Carol,

I am a newbie, but I do have some programing experience (if this makes any sense)
I'm mainly having issues the minor, little bits of stuff, that would seem to be, we'll say 'are a given',
that I completely over look ... 

Like a notebook called "Importing Jupyter Notebooks as Modules", that does just that, 
but it isn't meant to used like Pandas or Scipy,
which is exactly what I expected it to do ...

I was getting PyCharm to do what I asked, for the most part, I think ...
The notebook seemed like a great way to actually visualize, what I think I was generating.

My idea gets some what complicated and I sorta have a plan as to what I want to do ...
I have some knowledge of the complicated processes and procedures I want to create/access.
I keep getting hung on all the little pieces that have to be in place, that I may or may not have been aware of
and trying to get all the ducks lined up, arranging the simple bitz, with the more complicated bitz,
since my application is not 'EXACTLY" like example(s) I run across ...
And I get some sort of error messsage that I have no idea what it means or how to change to code to make it happy ..

(IE:/ That import module code runs for me, but it tells me nbformat.current is depricated

/home/user/anaconda3/envs/Proj01/lib/python3.5/site-packages/nbformat/current.py:19: UserWarning: nbformat.current is deprecated.

- use nbformat for read/write/validate public API
- use nbformat.vX directly to composing notebooks of a particular version

D Stubz

unread,
Mar 21, 2016, 7:10:34 PM3/21/16
to Project Jupyter
The idea is kinda like a GIS ... A data set exists, this is the constant(s) ... 
Examining the data, like say in a esri.shp file and getting familiar with all the data that's buried inside.

My idea is more about geometry, like a polygon(2D) and a polyhedra(3D), based on that polygon 
and then parametrically scaling/transforming/rotating the original data (unit data), (And I created all the data types)
and recording the information as t(h) provides a list of values to use, at different intervals over the stated range

( Here's what I have, in 10 steps, modify point/line/plane data, record the data at each step, 
how the scaled formula describing the original data changed and any other info one wants to track)

By running several different scenarios, what are the differences in the data sets, with respect to some desirable out come ...

IE:/ Not quite, but ...

I have numerous errands to run, depending on which route I take.
I can accomplish certain things, at certain times, given other items on the list have already been gathered ...
What's are the possible route options ...
What are the differences between the route options and why ...

It's several layers of data mangling, but the data types are what they are ... 
And I don't know before hand, which functions are going to do what, to which entity, when ...

No black box shit ... Show me what you did, how you did it and why you thought that was a good idea ... 
(Not a trait esri has going for it ...)

I don't think this is something that can be completely automated, 
but if I can generate some code/class/function that acts on my initial data,
I want a record of everything that affected it and how ...

D Stubz

unread,
Mar 21, 2016, 7:42:17 PM3/21/16
to Project Jupyter
Thanx Pete,

I was thinking a notebook and a module were pretty similiar things ... That doesn't seem to be the case

If I understand correctly, a notebook is a great place to look at functionality and data,
while working thru a process and developing an inter related series of processes ...

I'll look into the links you sent, tho ... 

Cheerz

D Stubz

unread,
Mar 23, 2016, 12:05:19 AM3/23/16
to Project Jupyter
Thanx again Pete,

Watched that video, the jupyter cms would seem to be what I was on about and much more :)

Cheerz
Reply all
Reply to author
Forward
0 new messages