[IPython-User] How to create notebook (.ipynb) from iPython console (no browser)?

Matthias BUSSONNIER bussonniermatthias@gmail....
Sat Oct 12 10:57:40 CDT 2013


Hi T.B, 


Le 12 oct. 2013 à 12:43, baruchel@gmx.com a écrit :

> I discovered iPython and iPython notebook yesterday evening, and I find it
> great for my needs (I am a teacher in computer science); I would like to use it
> for writing some python computing exercises.

Great to hear that. 

> But I work with no graphical server, and I would like to create my .ipynb
> notebooks from the console and not from a graphical browser.
> 
> Would it be easy to implement some macros or whatever for the very simple
> following purposes:
> 
>  * use IPython from the interactive console
>  * create a notebook .ipynb file for recording
>  * have some macros like :
>     --> add 3-15 (eg.) to the a .ipynb file according to the rule:
>         as long as not output cell occurs, use the same cell;
>         each time that an output occur, start a new cell (after the output)

Here you problem will be that al,oct every line produce an output value. 
Sometime it might be none. But basically every time you send a bunch of code to the kernel 
the AST is sliced, and the value of the last expression is returned and becomes the "output".

>     --> add a H1 header with text "Hellow world" in a new cell
>     --> add a H2 header with text "Hellow world" in a new cell
>     --> add raw text being "Hellow world" in a new cell
>  * save / exit
> 
> Later I can review the notebook from within Firefox and make some minor fixes,
> but I would like rather having such features from IPython.
> 
> Do you see something close to the previous ideas? Is it easy to do it?


Still what you ask shouldn't be too hard to do; .ipynb files are simple json files that are really easy to process and create by hand.
You can have a look at Ipython/nbformat/v3/* to get some utilities functions.

I suggest you have a look at defining you own magics, for example here :
http://nbviewer.ipython.org/url/github.com/ipython/ipython-in-depth/raw/master/notebooks/Customizing%20IPython%20-%20Magics.ipynb

And you probably want to have a look at our %history magic that will show you how to get access to 
the Nth input of current session. You probably want to dig a little deeper than pure function magics
as you might want to be statefull in your work, and go toward class magics. then you could do something like:

In[1] %open foo.ipynb 

In[2] some code
In[3] some code
In[4] some code

In[5] %code 2,4
In[6] %h2 This is a a test
...
In[70] %saveAndClose

 
Depending on what editor you use; you might want to have a look at EIN
http://tkf.github.io/emacs-ipython-notebook/

That allow to actually talk to the notebook server and manipulate notebooks files directly 
from within emacs. It might be better suited to your need.

If you have any more question, feel free to ask. 

I would also suggest you to subscribe/post to ipython-dev also:
Have a look here for the why :
http://python.6.x6.nabble.com/IPython-User-Our-quot-single-mailing-list-quot-policy-the-future-of-the-IPython-user-list-td5026879.html

-- 
Matthias

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-user/attachments/20131012/cc2f4eb5/attachment.html 


More information about the IPython-User mailing list