@ -24,7 +24,6 @@ var IPython = (function (IPython) {
this . clipboard = null ;
this . paste _enabled = false ;
this . dirty = false ;
this . msg _cell _map = { } ;
this . metadata = { } ;
this . control _key _active = false ;
this . notebook _id = null ;
@ -34,9 +33,6 @@ var IPython = (function (IPython) {
this . style ( ) ;
this . create _elements ( ) ;
this . bind _events ( ) ;
this . set _tooltipontab ( true ) ;
this . set _smartcompleter ( true ) ;
this . set _timebeforetooltip ( 1200 ) ;
} ;
@ -64,6 +60,20 @@ var IPython = (function (IPython) {
Notebook . prototype . bind _events = function ( ) {
var that = this ;
$ ( [ IPython . events ] ) . on ( 'set_next_input.Notebook' , function ( event , data ) {
var index = that . find _cell _index ( data . cell ) ;
var new _cell = that . insert _cell _below ( 'code' , index ) ;
new _cell . set _text ( data . text ) ;
that . dirty = true ;
} ) ;
$ ( [ IPython . events ] ) . on ( 'select.Cell' , function ( event , data ) {
var index = that . find _cell _index ( data . cell ) ;
that . select ( index ) ;
} ) ;
$ ( document ) . keydown ( function ( event ) {
// console.log(event);
if ( that . read _only ) return true ;
@ -393,19 +403,6 @@ var IPython = (function (IPython) {
} ;
Notebook . prototype . cell _for _msg = function ( msg _id ) {
var cell _id = this . msg _cell _map [ msg _id ] ;
var result = null ;
this . get _cell _elements ( ) . filter ( function ( index ) {
cell = $ ( this ) . data ( "cell" ) ;
if ( cell . cell _id === cell _id ) {
result = cell ;
} ;
} ) ;
return result ;
} ;
// Cell selection.
Notebook . prototype . select = function ( index ) {
@ -528,16 +525,16 @@ var IPython = (function (IPython) {
var cell = null ;
if ( this . ncells ( ) === 0 || this . is _valid _cell _index ( index ) ) {
if ( type === 'code' ) {
cell = new IPython . CodeCell ( this );
cell = new IPython . CodeCell ( this .kernel );
cell . set _input _prompt ( ) ;
} else if ( type === 'markdown' ) {
cell = new IPython . MarkdownCell ( this ) ;
cell = new IPython . MarkdownCell ( ) ;
} else if ( type === 'html' ) {
cell = new IPython . HTMLCell ( this ) ;
cell = new IPython . HTMLCell ( ) ;
} else if ( type === 'raw' ) {
cell = new IPython . RawCell ( this ) ;
cell = new IPython . RawCell ( ) ;
} else if ( type === 'heading' ) {
cell = new IPython . HeadingCell ( this ) ;
cell = new IPython . HeadingCell ( ) ;
} ;
if ( cell !== null ) {
if ( this . ncells ( ) === 0 ) {
@ -562,16 +559,16 @@ var IPython = (function (IPython) {
var cell = null ;
if ( this . ncells ( ) === 0 || this . is _valid _cell _index ( index ) ) {
if ( type === 'code' ) {
cell = new IPython . CodeCell ( this );
cell = new IPython . CodeCell ( this .kernel );
cell . set _input _prompt ( ) ;
} else if ( type === 'markdown' ) {
cell = new IPython . MarkdownCell ( this ) ;
cell = new IPython . MarkdownCell ( ) ;
} else if ( type === 'html' ) {
cell = new IPython . HTMLCell ( this ) ;
cell = new IPython . HTMLCell ( ) ;
} else if ( type === 'raw' ) {
cell = new IPython . RawCell ( this ) ;
cell = new IPython . RawCell ( ) ;
} else if ( type === 'heading' ) {
cell = new IPython . HeadingCell ( this ) ;
cell = new IPython . HeadingCell ( ) ;
} ;
if ( cell !== null ) {
if ( this . ncells ( ) === 0 ) {
@ -864,21 +861,6 @@ var IPython = (function (IPython) {
} ;
Notebook . prototype . set _timebeforetooltip = function ( time ) {
this . time _before _tooltip = time ;
} ;
Notebook . prototype . set _tooltipontab = function ( state ) {
this . tooltip _on _tab = state ;
} ;
Notebook . prototype . set _smartcompleter = function ( state ) {
this . smart _completer = state ;
} ;
Notebook . prototype . clear _all _output = function ( ) {
var ncells = this . ncells ( ) ;
var cells = this . get _cells ( ) ;
@ -903,8 +885,9 @@ var IPython = (function (IPython) {
// Kernel related things
Notebook . prototype . start _kernel = function ( ) {
this . kernel = new IPython . Kernel ( ) ;
this . kernel . start ( this . notebook _id , $ . proxy ( this . kernel _started , this ) ) ;
var base _url = $ ( 'body' ) . data ( 'baseKernelUrl' ) + "kernels" ;
this . kernel = new IPython . Kernel ( base _url ) ;
this . kernel . start ( this . notebook _id ) ;
} ;
@ -920,117 +903,7 @@ var IPython = (function (IPython) {
closeText : '' ,
buttons : {
"Restart" : function ( ) {
that . kernel . restart ( $ . proxy ( that . kernel _started , that ) ) ;
$ ( this ) . dialog ( 'close' ) ;
} ,
"Continue running" : function ( ) {
$ ( this ) . dialog ( 'close' ) ;
}
}
} ) ;
} ;
Notebook . prototype . kernel _started = function ( ) {
console . log ( "Kernel started: " , this . kernel . kernel _id ) ;
this . kernel . shell _channel . onmessage = $ . proxy ( this . handle _shell _reply , this ) ;
this . kernel . iopub _channel . onmessage = $ . proxy ( this . handle _iopub _reply , this ) ;
} ;
Notebook . prototype . handle _shell _reply = function ( e ) {
reply = $ . parseJSON ( e . data ) ;
var header = reply . header ;
var content = reply . content ;
var msg _type = header . msg _type ;
// console.log(reply);
var cell = this . cell _for _msg ( reply . parent _header . msg _id ) ;
if ( msg _type === "execute_reply" ) {
cell . set _input _prompt ( content . execution _count ) ;
cell . element . removeClass ( "running" ) ;
this . dirty = true ;
} else if ( msg _type === "complete_reply" ) {
cell . finish _completing ( content . matched _text , content . matches ) ;
} else if ( msg _type === "object_info_reply" ) {
//console.log('back from object_info_request : ')
rep = reply . content ;
if ( rep . found )
{
cell . finish _tooltip ( rep ) ;
}
} else {
//console.log("unknown reply:"+msg_type);
}
// when having a rely from object_info_reply,
// no payload so no nned to handle it
if ( typeof ( content . payload ) != 'undefined' ) {
var payload = content . payload || [ ] ;
this . handle _payload ( cell , payload ) ;
}
} ;
Notebook . prototype . handle _payload = function ( cell , payload ) {
var l = payload . length ;
for ( var i = 0 ; i < l ; i ++ ) {
if ( payload [ i ] . source === 'IPython.zmq.page.page' ) {
if ( payload [ i ] . text . trim ( ) !== '' ) {
IPython . pager . clear ( ) ;
IPython . pager . expand ( ) ;
IPython . pager . append _text ( payload [ i ] . text ) ;
}
} else if ( payload [ i ] . source === 'IPython.zmq.zmqshell.ZMQInteractiveShell.set_next_input' ) {
var index = this . find _cell _index ( cell ) ;
var new _cell = this . insert _cell _below ( 'code' , index ) ;
new _cell . set _text ( payload [ i ] . text ) ;
this . dirty = true ;
}
} ;
} ;
Notebook . prototype . handle _iopub _reply = function ( e ) {
reply = $ . parseJSON ( e . data ) ;
var content = reply . content ;
// console.log(reply);
var msg _type = reply . header . msg _type ;
var cell = this . cell _for _msg ( reply . parent _header . msg _id ) ;
if ( msg _type !== 'status' && ! cell ) {
// message not from this notebook, but should be attached to a cell
// console.log("Received IOPub message not caused by one of my cells");
// console.log(reply);
return ;
}
var output _types = [ 'stream' , 'display_data' , 'pyout' , 'pyerr' ] ;
if ( output _types . indexOf ( msg _type ) >= 0 ) {
this . handle _output ( cell , msg _type , content ) ;
} else if ( msg _type === 'status' ) {
if ( content . execution _state === 'busy' ) {
$ ( [ IPython . events ] ) . trigger ( 'status_busy.Kernel' ) ;
} else if ( content . execution _state === 'idle' ) {
$ ( [ IPython . events ] ) . trigger ( 'status_idle.Kernel' ) ;
} else if ( content . execution _state === 'dead' ) {
this . handle _status _dead ( ) ;
} ;
} else if ( msg _type === 'clear_output' ) {
cell . clear _output ( content . stdout , content . stderr , content . other ) ;
} ;
} ;
Notebook . prototype . handle _status _dead = function ( ) {
var that = this ;
this . kernel . stop _channels ( ) ;
var dialog = $ ( '<div/>' ) ;
dialog . html ( 'The kernel has died, would you like to restart it? If you do not restart the kernel, you will be able to save the notebook, but running code will not work until the notebook is reopened.' ) ;
$ ( document ) . append ( dialog ) ;
dialog . dialog ( {
resizable : false ,
modal : true ,
title : "Dead kernel" ,
buttons : {
"Restart" : function ( ) {
that . start _kernel ( ) ;
that . kernel . restart ( ) ;
$ ( this ) . dialog ( 'close' ) ;
} ,
"Continue running" : function ( ) {
@ -1041,57 +914,6 @@ var IPython = (function (IPython) {
} ;
Notebook . prototype . handle _output = function ( cell , msg _type , content ) {
var json = { } ;
json . output _type = msg _type ;
if ( msg _type === "stream" ) {
json . text = content . data ;
json . stream = content . name ;
} else if ( msg _type === "display_data" ) {
json = this . convert _mime _types ( json , content . data ) ;
} else if ( msg _type === "pyout" ) {
json . prompt _number = content . execution _count ;
json = this . convert _mime _types ( json , content . data ) ;
} else if ( msg _type === "pyerr" ) {
json . ename = content . ename ;
json . evalue = content . evalue ;
json . traceback = content . traceback ;
} ;
// append with dynamic=true
cell . append _output ( json , true ) ;
this . dirty = true ;
} ;
Notebook . prototype . convert _mime _types = function ( json , data ) {
if ( data [ 'text/plain' ] !== undefined ) {
json . text = data [ 'text/plain' ] ;
} ;
if ( data [ 'text/html' ] !== undefined ) {
json . html = data [ 'text/html' ] ;
} ;
if ( data [ 'image/svg+xml' ] !== undefined ) {
json . svg = data [ 'image/svg+xml' ] ;
} ;
if ( data [ 'image/png' ] !== undefined ) {
json . png = data [ 'image/png' ] ;
} ;
if ( data [ 'image/jpeg' ] !== undefined ) {
json . jpeg = data [ 'image/jpeg' ] ;
} ;
if ( data [ 'text/latex' ] !== undefined ) {
json . latex = data [ 'text/latex' ] ;
} ;
if ( data [ 'application/json' ] !== undefined ) {
json . json = data [ 'application/json' ] ;
} ;
if ( data [ 'application/javascript' ] !== undefined ) {
json . javascript = data [ 'application/javascript' ] ;
}
return json ;
} ;
Notebook . prototype . execute _selected _cell = function ( options ) {
// add_new: should a new cell be added if we are at the end of the nb
// terminal: execute in terminal mode, which stays in the current cell
@ -1101,12 +923,7 @@ var IPython = (function (IPython) {
var cell = that . get _selected _cell ( ) ;
var cell _index = that . find _cell _index ( cell ) ;
if ( cell instanceof IPython . CodeCell ) {
cell . clear _output ( true , true , true ) ;
cell . set _input _prompt ( '*' ) ;
cell . element . addClass ( "running" ) ;
var code = cell . get _text ( ) ;
var msg _id = that . kernel . execute ( cell . get _text ( ) ) ;
that . msg _cell _map [ msg _id ] = cell . cell _id ;
cell . execute ( ) ;
} else if ( cell instanceof IPython . HTMLCell ) {
cell . render ( ) ;
}
@ -1134,37 +951,6 @@ var IPython = (function (IPython) {
this . scroll _to _bottom ( ) ;
} ;
Notebook . prototype . request _tool _tip = function ( cell , func ) {
// Feel free to shorten this logic if you are better
// than me in regEx
// basicaly you shoul be able to get xxx.xxx.xxx from
// something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,
// remove everything between matchin bracket (need to iterate)
matchBracket = /\([^\(\)]+\)/g ;
oldfunc = func ;
func = func . replace ( matchBracket , "" ) ;
while ( oldfunc != func )
{
oldfunc = func ;
func = func . replace ( matchBracket , "" ) ;
}
// remove everythin after last open bracket
endBracket = /\([^\(]*$/g ;
func = func . replace ( endBracket , "" ) ;
var re = /[a-z_][0-9a-z._]+$/gi ; // casse insensitive
var msg _id = this . kernel . object _info _request ( re . exec ( func ) ) ;
if ( typeof ( msg _id ) != 'undefined' ) {
this . msg _cell _map [ msg _id ] = cell . cell _id ;
}
} ;
Notebook . prototype . complete _cell = function ( cell , line , cursor _pos ) {
var msg _id = this . kernel . complete ( line , cursor _pos ) ;
this . msg _cell _map [ msg _id ] = cell . cell _id ;
} ;
// Persistance and loading
Notebook . prototype . get _notebook _id = function ( ) {
@ -1290,14 +1076,15 @@ var IPython = (function (IPython) {
Notebook . prototype . load _notebook _success = function ( data , status , xhr ) {
// Create the kernel before creating cells as they need to be passed it.
if ( ! this . read _only ) {
this . start _kernel ( ) ;
}
this . fromJSON ( data ) ;
if ( this . ncells ( ) === 0 ) {
this . insert _cell _below ( 'code' ) ;
} ;
this . dirty = false ;
if ( ! this . read _only ) {
this . start _kernel ( ) ;
}
this . select ( 0 ) ;
this . scroll _to _top ( ) ;
if ( data . orig _nbformat !== undefined && data . nbformat !== data . orig _nbformat ) {