Track realtime schedules of bus 68 in Paris.

Plot.ipynb 840KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Bus 68 RATP (IDFM) OpenData"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "This notebook is used to analyze and plot graphs on top of the STIF/IDFM OpenData collection over a few days."
  15. ]
  16. },
  17. {
  18. "cell_type": "code",
  19. "execution_count": 1,
  20. "metadata": {},
  21. "outputs": [],
  22. "source": [
  23. "%matplotlib notebook"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "execution_count": 2,
  29. "metadata": {},
  30. "outputs": [],
  31. "source": [
  32. "import collections\n",
  33. "import datetime\n",
  34. "import math\n",
  35. "import os\n",
  36. "import sys\n",
  37. "\n",
  38. "import matplotlib.dates as mdates\n",
  39. "import matplotlib.pyplot as plt\n",
  40. "import peewee\n",
  41. "\n",
  42. "from playhouse.db_url import connect"
  43. ]
  44. },
  45. {
  46. "cell_type": "code",
  47. "execution_count": 3,
  48. "metadata": {},
  49. "outputs": [],
  50. "source": [
  51. "# Initialize db\n",
  52. "db = connect('sqlite:///bus_68_datapoints.db')\n",
  53. "\n",
  54. "\n",
  55. "class DataPoint(peewee.Model):\n",
  56. " class Meta:\n",
  57. " database = db\n",
  58. "\n",
  59. " datetime = peewee.DateTimeField(\n",
  60. " default=datetime.datetime.now()\n",
  61. " )\n",
  62. " lineDirection = peewee.CharField(max_length=255)\n",
  63. " code = peewee.CharField(max_length=255)\n",
  64. " time = peewee.CharField(max_length=255)"
  65. ]
  66. },
  67. {
  68. "cell_type": "code",
  69. "execution_count": 4,
  70. "metadata": {},
  71. "outputs": [
  72. {
  73. "data": {
  74. "text/plain": [
  75. "True"
  76. ]
  77. },
  78. "execution_count": 4,
  79. "metadata": {},
  80. "output_type": "execute_result"
  81. }
  82. ],
  83. "source": [
  84. "db.connect()"
  85. ]
  86. },
  87. {
  88. "cell_type": "code",
  89. "execution_count": 5,
  90. "metadata": {},
  91. "outputs": [
  92. {
  93. "data": {
  94. "text/plain": [
  95. "(datetime.datetime(2018, 9, 25, 0, 0), datetime.datetime(2018, 12, 12, 0, 0))"
  96. ]
  97. },
  98. "execution_count": 5,
  99. "metadata": {},
  100. "output_type": "execute_result"
  101. }
  102. ],
  103. "source": [
  104. "# Get first and last day in the dataset\n",
  105. "first_dp = DataPoint().select().order_by(DataPoint.datetime.asc()).first()\n",
  106. "first_day = first_dp.datetime.replace(\n",
  107. " hour=0, minute=0, second=0, microsecond=0\n",
  108. ")\n",
  109. "\n",
  110. "last_dp = DataPoint().select().order_by(DataPoint.datetime.desc()).first()\n",
  111. "last_day = last_dp.datetime.replace(\n",
  112. " hour=0, minute=0, second=0, microsecond=0\n",
  113. ")\n",
  114. "\n",
  115. "# Remove first and last day as they are likely not complete in the dataset\n",
  116. "first_day += datetime.timedelta(days=1)\n",
  117. "last_day -= datetime.timedelta(days=1)\n",
  118. "\n",
  119. "first_day, last_day"
  120. ]
  121. },
  122. {
  123. "cell_type": "code",
  124. "execution_count": 6,
  125. "metadata": {},
  126. "outputs": [
  127. {
  128. "data": {
  129. "text/plain": [
  130. "(datetime.datetime(2018, 11, 1, 0, 0), datetime.datetime(2018, 12, 1, 0, 0))"
  131. ]
  132. },
  133. "execution_count": 6,
  134. "metadata": {},
  135. "output_type": "execute_result"
  136. }
  137. ],
  138. "source": [
  139. "# Truncate data\n",
  140. "first_day = datetime.datetime(\n",
  141. " year=2018, month=11, day=1,\n",
  142. " hour=0, minute=0, second=0, microsecond=0\n",
  143. ")\n",
  144. "last_day = datetime.datetime(\n",
  145. " year=2018, month=12, day=1,\n",
  146. " hour=0, minute=0, second=0, microsecond=0\n",
  147. ")\n",
  148. "first_day, last_day"
  149. ]
  150. },
  151. {
  152. "cell_type": "code",
  153. "execution_count": 7,
  154. "metadata": {},
  155. "outputs": [
  156. {
  157. "name": "stdout",
  158. "output_type": "stream",
  159. "text": [
  160. "2018-11-01 00:00:00\n",
  161. "2018-11-02 00:00:00\n",
  162. "2018-11-03 00:00:00\n",
  163. "2018-11-04 00:00:00\n",
  164. "2018-11-05 00:00:00\n",
  165. "2018-11-06 00:00:00\n",
  166. "2018-11-07 00:00:00\n",
  167. "2018-11-08 00:00:00\n",
  168. "2018-11-09 00:00:00\n",
  169. "2018-11-10 00:00:00\n",
  170. "2018-11-11 00:00:00\n",
  171. "2018-11-12 00:00:00\n",
  172. "2018-11-13 00:00:00\n",
  173. "2018-11-14 00:00:00\n",
  174. "2018-11-15 00:00:00\n",
  175. "2018-11-16 00:00:00\n",
  176. "2018-11-17 00:00:00\n",
  177. "2018-11-18 00:00:00\n",
  178. "2018-11-19 00:00:00\n",
  179. "2018-11-20 00:00:00\n",
  180. "2018-11-21 00:00:00\n",
  181. "2018-11-22 00:00:00\n",
  182. "2018-11-23 00:00:00\n",
  183. "2018-11-24 00:00:00\n",
  184. "2018-11-25 00:00:00\n",
  185. "2018-11-26 00:00:00\n",
  186. "2018-11-27 00:00:00\n",
  187. "2018-11-28 00:00:00\n",
  188. "2018-11-29 00:00:00\n",
  189. "2018-11-30 00:00:00\n",
  190. "2018-12-01 00:00:00\n",
  191. "Done!\n"
  192. ]
  193. }
  194. ],
  195. "source": [
  196. "# Line direction to filter on\n",
  197. "LINE_DIRECTION = \"Chatillon Montrouge\"\n",
  198. "# Dictionnary mapping days to a dict of list of datetimes\n",
  199. "# for at stops, approaching, possibly missed and waiting times\n",
  200. "processed_data = {}\n",
  201. "\n",
  202. "day = first_day\n",
  203. "while day <= last_day:\n",
  204. " print(day)\n",
  205. " \n",
  206. " # Get all the datapoints for the current day\n",
  207. " next_day = day + datetime.timedelta(days=1)\n",
  208. " day_dps = DataPoint().select().where(\n",
  209. " (DataPoint.datetime >= day) &\n",
  210. " (DataPoint.datetime < next_day) &\n",
  211. " (DataPoint.lineDirection == LINE_DIRECTION)\n",
  212. " ).order_by(DataPoint.datetime.asc())\n",
  213. "\n",
  214. " processed_data[day] = {}\n",
  215. " processed_data[day]['at_stop_datetimes'] = []\n",
  216. " processed_data[day]['at_approach_datetimes'] = []\n",
  217. " processed_data[day]['possibly_missed_datetimes'] = []\n",
  218. " processed_data[day]['waiting_times'] = [] # Tuples of datetimes and waiting times\n",
  219. " current_waiting_time = math.inf\n",
  220. " \n",
  221. " # Group items by datetimes since there are two times\n",
  222. " # (for next bus and next next bus) returned at every moment by the API\n",
  223. " day_dps_by_datetimes = collections.defaultdict(list)\n",
  224. " for item in day_dps:\n",
  225. " # TODO: We should be able to detect multiple buses coming at the same time here\n",
  226. " day_dps_by_datetimes[item.datetime].append(item)\n",
  227. " \n",
  228. " for dt in day_dps_by_datetimes.keys():\n",
  229. " selected_item = day_dps_by_datetimes[dt][0] # Default to first item choosen\n",
  230. " for item in day_dps_by_datetimes[dt]:\n",
  231. " if \"arrêt\" in selected_item.time:\n",
  232. " # If selected item is \"at stop\", just keep this one\n",
  233. " continue\n",
  234. "\n",
  235. " if \"arrêt\" in item.time or \"approche\" in item.time:\n",
  236. " # Else, if the looped item is at stop or approaching, pick it\n",
  237. " selected_item = item\n",
  238. " elif \"approche\" not in selected_item.time and int(item.time) < int(selected_item.time):\n",
  239. " # Else, only keep it if current waiting time is the lowest one\n",
  240. " selected_item = item\n",
  241. " \n",
  242. " if \"arrêt\" in selected_item.time:\n",
  243. " # Bus at stop, note the time in the \"at stop\" list\n",
  244. " processed_data[day]['at_stop_datetimes'].append(selected_item.datetime)\n",
  245. " current_waiting_time = math.inf\n",
  246. " processed_data[day]['waiting_times'].append((selected_item.datetime, 0))\n",
  247. " elif \"approche\" in selected_item.time:\n",
  248. " # Bus approaching, note the time in the \"approaching\" list\n",
  249. " processed_data[day]['at_approach_datetimes'].append(selected_item.datetime)\n",
  250. " current_waiting_time = 0\n",
  251. " processed_data[day]['waiting_times'].append((selected_item.datetime, 0))\n",
  252. " else:\n",
  253. " # Else, check the current expected duration\n",
  254. " new_current_waiting_time = int(selected_item.time)\n",
  255. " if current_waiting_time > 0 and current_waiting_time < new_current_waiting_time:\n",
  256. " # We might have missed a bus\n",
  257. " processed_data[day]['possibly_missed_datetimes'].append(selected_item.datetime)\n",
  258. " # Update current duration and store current waiting times\n",
  259. " current_waiting_time = new_current_waiting_time\n",
  260. " processed_data[day]['waiting_times'].append((selected_item.datetime, current_waiting_time))\n",
  261. " \n",
  262. " day = next_day\n",
  263. " \n",
  264. "print('Done!')"
  265. ]
  266. },
  267. {
  268. "cell_type": "code",
  269. "execution_count": 8,
  270. "metadata": {},
  271. "outputs": [
  272. {
  273. "data": {
  274. "application/javascript": [
  275. "/* Put everything inside the global mpl namespace */\n",
  276. "window.mpl = {};\n",
  277. "\n",
  278. "\n",
  279. "mpl.get_websocket_type = function() {\n",
  280. " if (typeof(WebSocket) !== 'undefined') {\n",
  281. " return WebSocket;\n",
  282. " } else if (typeof(MozWebSocket) !== 'undefined') {\n",
  283. " return MozWebSocket;\n",
  284. " } else {\n",
  285. " alert('Your browser does not have WebSocket support.' +\n",
  286. " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
  287. " 'Firefox 4 and 5 are also supported but you ' +\n",
  288. " 'have to enable WebSockets in about:config.');\n",
  289. " };\n",
  290. "}\n",
  291. "\n",
  292. "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
  293. " this.id = figure_id;\n",
  294. "\n",
  295. " this.ws = websocket;\n",
  296. "\n",
  297. " this.supports_binary = (this.ws.binaryType != undefined);\n",
  298. "\n",
  299. " if (!this.supports_binary) {\n",
  300. " var warnings = document.getElementById(\"mpl-warnings\");\n",
  301. " if (warnings) {\n",
  302. " warnings.style.display = 'block';\n",
  303. " warnings.textContent = (\n",
  304. " \"This browser does not support binary websocket messages. \" +\n",
  305. " \"Performance may be slow.\");\n",
  306. " }\n",
  307. " }\n",
  308. "\n",
  309. " this.imageObj = new Image();\n",
  310. "\n",
  311. " this.context = undefined;\n",
  312. " this.message = undefined;\n",
  313. " this.canvas = undefined;\n",
  314. " this.rubberband_canvas = undefined;\n",
  315. " this.rubberband_context = undefined;\n",
  316. " this.format_dropdown = undefined;\n",
  317. "\n",
  318. " this.image_mode = 'full';\n",
  319. "\n",
  320. " this.root = $('<div/>');\n",
  321. " this._root_extra_style(this.root)\n",
  322. " this.root.attr('style', 'display: inline-block');\n",
  323. "\n",
  324. " $(parent_element).append(this.root);\n",
  325. "\n",
  326. " this._init_header(this);\n",
  327. " this._init_canvas(this);\n",
  328. " this._init_toolbar(this);\n",
  329. "\n",
  330. " var fig = this;\n",
  331. "\n",
  332. " this.waiting = false;\n",
  333. "\n",
  334. " this.ws.onopen = function () {\n",
  335. " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
  336. " fig.send_message(\"send_image_mode\", {});\n",
  337. " if (mpl.ratio != 1) {\n",
  338. " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
  339. " }\n",
  340. " fig.send_message(\"refresh\", {});\n",
  341. " }\n",
  342. "\n",
  343. " this.imageObj.onload = function() {\n",
  344. " if (fig.image_mode == 'full') {\n",
  345. " // Full images could contain transparency (where diff images\n",
  346. " // almost always do), so we need to clear the canvas so that\n",
  347. " // there is no ghosting.\n",
  348. " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
  349. " }\n",
  350. " fig.context.drawImage(fig.imageObj, 0, 0);\n",
  351. " };\n",
  352. "\n",
  353. " this.imageObj.onunload = function() {\n",
  354. " fig.ws.close();\n",
  355. " }\n",
  356. "\n",
  357. " this.ws.onmessage = this._make_on_message_function(this);\n",
  358. "\n",
  359. " this.ondownload = ondownload;\n",
  360. "}\n",
  361. "\n",
  362. "mpl.figure.prototype._init_header = function() {\n",
  363. " var titlebar = $(\n",
  364. " '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
  365. " 'ui-helper-clearfix\"/>');\n",
  366. " var titletext = $(\n",
  367. " '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
  368. " 'text-align: center; padding: 3px;\"/>');\n",
  369. " titlebar.append(titletext)\n",
  370. " this.root.append(titlebar);\n",
  371. " this.header = titletext[0];\n",
  372. "}\n",
  373. "\n",
  374. "\n",
  375. "\n",
  376. "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
  377. "\n",
  378. "}\n",
  379. "\n",
  380. "\n",
  381. "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
  382. "\n",
  383. "}\n",
  384. "\n",
  385. "mpl.figure.prototype._init_canvas = function() {\n",
  386. " var fig = this;\n",
  387. "\n",
  388. " var canvas_div = $('<div/>');\n",
  389. "\n",
  390. " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
  391. "\n",
  392. " function canvas_keyboard_event(event) {\n",
  393. " return fig.key_event(event, event['data']);\n",
  394. " }\n",
  395. "\n",
  396. " canvas_div.keydown('key_press', canvas_keyboard_event);\n",
  397. " canvas_div.keyup('key_release', canvas_keyboard_event);\n",
  398. " this.canvas_div = canvas_div\n",
  399. " this._canvas_extra_style(canvas_div)\n",
  400. " this.root.append(canvas_div);\n",
  401. "\n",
  402. " var canvas = $('<canvas/>');\n",
  403. " canvas.addClass('mpl-canvas');\n",
  404. " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
  405. "\n",
  406. " this.canvas = canvas[0];\n",
  407. " this.context = canvas[0].getContext(\"2d\");\n",
  408. "\n",
  409. " var backingStore = this.context.backingStorePixelRatio ||\n",
  410. "\tthis.context.webkitBackingStorePixelRatio ||\n",
  411. "\tthis.context.mozBackingStorePixelRatio ||\n",
  412. "\tthis.context.msBackingStorePixelRatio ||\n",
  413. "\tthis.context.oBackingStorePixelRatio ||\n",
  414. "\tthis.context.backingStorePixelRatio || 1;\n",
  415. "\n",
  416. " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
  417. "\n",
  418. " var rubberband = $('<canvas/>');\n",
  419. " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
  420. "\n",
  421. " var pass_mouse_events = true;\n",
  422. "\n",
  423. " canvas_div.resizable({\n",
  424. " start: function(event, ui) {\n",
  425. " pass_mouse_events = false;\n",
  426. " },\n",
  427. " resize: function(event, ui) {\n",
  428. " fig.request_resize(ui.size.width, ui.size.height);\n",
  429. " },\n",
  430. " stop: function(event, ui) {\n",
  431. " pass_mouse_events = true;\n",
  432. " fig.request_resize(ui.size.width, ui.size.height);\n",
  433. " },\n",
  434. " });\n",
  435. "\n",
  436. " function mouse_event_fn(event) {\n",
  437. " if (pass_mouse_events)\n",
  438. " return fig.mouse_event(event, event['data']);\n",
  439. " }\n",
  440. "\n",
  441. " rubberband.mousedown('button_press', mouse_event_fn);\n",
  442. " rubberband.mouseup('button_release', mouse_event_fn);\n",
  443. " // Throttle sequential mouse events to 1 every 20ms.\n",
  444. " rubberband.mousemove('motion_notify', mouse_event_fn);\n",
  445. "\n",
  446. " rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
  447. " rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
  448. "\n",
  449. " canvas_div.on(\"wheel\", function (event) {\n",
  450. " event = event.originalEvent;\n",
  451. " event['data'] = 'scroll'\n",
  452. " if (event.deltaY < 0) {\n",
  453. " event.step = 1;\n",
  454. " } else {\n",
  455. " event.step = -1;\n",
  456. " }\n",
  457. " mouse_event_fn(event);\n",
  458. " });\n",
  459. "\n",
  460. " canvas_div.append(canvas);\n",
  461. " canvas_div.append(rubberband);\n",
  462. "\n",
  463. " this.rubberband = rubberband;\n",
  464. " this.rubberband_canvas = rubberband[0];\n",
  465. " this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
  466. " this.rubberband_context.strokeStyle = \"#000000\";\n",
  467. "\n",
  468. " this._resize_canvas = function(width, height) {\n",
  469. " // Keep the size of the canvas, canvas container, and rubber band\n",
  470. " // canvas in synch.\n",
  471. " canvas_div.css('width', width)\n",
  472. " canvas_div.css('height', height)\n",
  473. "\n",
  474. " canvas.attr('width', width * mpl.ratio);\n",
  475. " canvas.attr('height', height * mpl.ratio);\n",
  476. " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
  477. "\n",
  478. " rubberband.attr('width', width);\n",
  479. " rubberband.attr('height', height);\n",
  480. " }\n",
  481. "\n",
  482. " // Set the figure to an initial 600x600px, this will subsequently be updated\n",
  483. " // upon first draw.\n",
  484. " this._resize_canvas(600, 600);\n",
  485. "\n",
  486. " // Disable right mouse context menu.\n",
  487. " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
  488. " return false;\n",
  489. " });\n",
  490. "\n",
  491. " function set_focus () {\n",
  492. " canvas.focus();\n",
  493. " canvas_div.focus();\n",
  494. " }\n",
  495. "\n",
  496. " window.setTimeout(set_focus, 100);\n",
  497. "}\n",
  498. "\n",
  499. "mpl.figure.prototype._init_toolbar = function() {\n",
  500. " var fig = this;\n",
  501. "\n",
  502. " var nav_element = $('<div/>')\n",
  503. " nav_element.attr('style', 'width: 100%');\n",
  504. " this.root.append(nav_element);\n",
  505. "\n",
  506. " // Define a callback function for later on.\n",
  507. " function toolbar_event(event) {\n",
  508. " return fig.toolbar_button_onclick(event['data']);\n",
  509. " }\n",
  510. " function toolbar_mouse_event(event) {\n",
  511. " return fig.toolbar_button_onmouseover(event['data']);\n",
  512. " }\n",
  513. "\n",
  514. " for(var toolbar_ind in mpl.toolbar_items) {\n",
  515. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  516. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  517. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  518. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  519. "\n",
  520. " if (!name) {\n",
  521. " // put a spacer in here.\n",
  522. " continue;\n",
  523. " }\n",
  524. " var button = $('<button/>');\n",
  525. " button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
  526. " 'ui-button-icon-only');\n",
  527. " button.attr('role', 'button');\n",
  528. " button.attr('aria-disabled', 'false');\n",
  529. " button.click(method_name, toolbar_event);\n",
  530. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  531. "\n",
  532. " var icon_img = $('<span/>');\n",
  533. " icon_img.addClass('ui-button-icon-primary ui-icon');\n",
  534. " icon_img.addClass(image);\n",
  535. " icon_img.addClass('ui-corner-all');\n",
  536. "\n",
  537. " var tooltip_span = $('<span/>');\n",
  538. " tooltip_span.addClass('ui-button-text');\n",
  539. " tooltip_span.html(tooltip);\n",
  540. "\n",
  541. " button.append(icon_img);\n",
  542. " button.append(tooltip_span);\n",
  543. "\n",
  544. " nav_element.append(button);\n",
  545. " }\n",
  546. "\n",
  547. " var fmt_picker_span = $('<span/>');\n",
  548. "\n",
  549. " var fmt_picker = $('<select/>');\n",
  550. " fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
  551. " fmt_picker_span.append(fmt_picker);\n",
  552. " nav_element.append(fmt_picker_span);\n",
  553. " this.format_dropdown = fmt_picker[0];\n",
  554. "\n",
  555. " for (var ind in mpl.extensions) {\n",
  556. " var fmt = mpl.extensions[ind];\n",
  557. " var option = $(\n",
  558. " '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
  559. " fmt_picker.append(option)\n",
  560. " }\n",
  561. "\n",
  562. " // Add hover states to the ui-buttons\n",
  563. " $( \".ui-button\" ).hover(\n",
  564. " function() { $(this).addClass(\"ui-state-hover\");},\n",
  565. " function() { $(this).removeClass(\"ui-state-hover\");}\n",
  566. " );\n",
  567. "\n",
  568. " var status_bar = $('<span class=\"mpl-message\"/>');\n",
  569. " nav_element.append(status_bar);\n",
  570. " this.message = status_bar[0];\n",
  571. "}\n",
  572. "\n",
  573. "mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
  574. " // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
  575. " // which will in turn request a refresh of the image.\n",
  576. " this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
  577. "}\n",
  578. "\n",
  579. "mpl.figure.prototype.send_message = function(type, properties) {\n",
  580. " properties['type'] = type;\n",
  581. " properties['figure_id'] = this.id;\n",
  582. " this.ws.send(JSON.stringify(properties));\n",
  583. "}\n",
  584. "\n",
  585. "mpl.figure.prototype.send_draw_message = function() {\n",
  586. " if (!this.waiting) {\n",
  587. " this.waiting = true;\n",
  588. " this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
  589. " }\n",
  590. "}\n",
  591. "\n",
  592. "\n",
  593. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  594. " var format_dropdown = fig.format_dropdown;\n",
  595. " var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
  596. " fig.ondownload(fig, format);\n",
  597. "}\n",
  598. "\n",
  599. "\n",
  600. "mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
  601. " var size = msg['size'];\n",
  602. " if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
  603. " fig._resize_canvas(size[0], size[1]);\n",
  604. " fig.send_message(\"refresh\", {});\n",
  605. " };\n",
  606. "}\n",
  607. "\n",
  608. "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
  609. " var x0 = msg['x0'] / mpl.ratio;\n",
  610. " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
  611. " var x1 = msg['x1'] / mpl.ratio;\n",
  612. " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
  613. " x0 = Math.floor(x0) + 0.5;\n",
  614. " y0 = Math.floor(y0) + 0.5;\n",
  615. " x1 = Math.floor(x1) + 0.5;\n",
  616. " y1 = Math.floor(y1) + 0.5;\n",
  617. " var min_x = Math.min(x0, x1);\n",
  618. " var min_y = Math.min(y0, y1);\n",
  619. " var width = Math.abs(x1 - x0);\n",
  620. " var height = Math.abs(y1 - y0);\n",
  621. "\n",
  622. " fig.rubberband_context.clearRect(\n",
  623. " 0, 0, fig.canvas.width, fig.canvas.height);\n",
  624. "\n",
  625. " fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
  626. "}\n",
  627. "\n",
  628. "mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
  629. " // Updates the figure title.\n",
  630. " fig.header.textContent = msg['label'];\n",
  631. "}\n",
  632. "\n",
  633. "mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
  634. " var cursor = msg['cursor'];\n",
  635. " switch(cursor)\n",
  636. " {\n",
  637. " case 0:\n",
  638. " cursor = 'pointer';\n",
  639. " break;\n",
  640. " case 1:\n",
  641. " cursor = 'default';\n",
  642. " break;\n",
  643. " case 2:\n",
  644. " cursor = 'crosshair';\n",
  645. " break;\n",
  646. " case 3:\n",
  647. " cursor = 'move';\n",
  648. " break;\n",
  649. " }\n",
  650. " fig.rubberband_canvas.style.cursor = cursor;\n",
  651. "}\n",
  652. "\n",
  653. "mpl.figure.prototype.handle_message = function(fig, msg) {\n",
  654. " fig.message.textContent = msg['message'];\n",
  655. "}\n",
  656. "\n",
  657. "mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
  658. " // Request the server to send over a new figure.\n",
  659. " fig.send_draw_message();\n",
  660. "}\n",
  661. "\n",
  662. "mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
  663. " fig.image_mode = msg['mode'];\n",
  664. "}\n",
  665. "\n",
  666. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  667. " // Called whenever the canvas gets updated.\n",
  668. " this.send_message(\"ack\", {});\n",
  669. "}\n",
  670. "\n",
  671. "// A function to construct a web socket function for onmessage handling.\n",
  672. "// Called in the figure constructor.\n",
  673. "mpl.figure.prototype._make_on_message_function = function(fig) {\n",
  674. " return function socket_on_message(evt) {\n",
  675. " if (evt.data instanceof Blob) {\n",
  676. " /* FIXME: We get \"Resource interpreted as Image but\n",
  677. " * transferred with MIME type text/plain:\" errors on\n",
  678. " * Chrome. But how to set the MIME type? It doesn't seem\n",
  679. " * to be part of the websocket stream */\n",
  680. " evt.data.type = \"image/png\";\n",
  681. "\n",
  682. " /* Free the memory for the previous frames */\n",
  683. " if (fig.imageObj.src) {\n",
  684. " (window.URL || window.webkitURL).revokeObjectURL(\n",
  685. " fig.imageObj.src);\n",
  686. " }\n",
  687. "\n",
  688. " fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
  689. " evt.data);\n",
  690. " fig.updated_canvas_event();\n",
  691. " fig.waiting = false;\n",
  692. " return;\n",
  693. " }\n",
  694. " else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
  695. " fig.imageObj.src = evt.data;\n",
  696. " fig.updated_canvas_event();\n",
  697. " fig.waiting = false;\n",
  698. " return;\n",
  699. " }\n",
  700. "\n",
  701. " var msg = JSON.parse(evt.data);\n",
  702. " var msg_type = msg['type'];\n",
  703. "\n",
  704. " // Call the \"handle_{type}\" callback, which takes\n",
  705. " // the figure and JSON message as its only arguments.\n",
  706. " try {\n",
  707. " var callback = fig[\"handle_\" + msg_type];\n",
  708. " } catch (e) {\n",
  709. " console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
  710. " return;\n",
  711. " }\n",
  712. "\n",
  713. " if (callback) {\n",
  714. " try {\n",
  715. " // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
  716. " callback(fig, msg);\n",
  717. " } catch (e) {\n",
  718. " console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
  719. " }\n",
  720. " }\n",
  721. " };\n",
  722. "}\n",
  723. "\n",
  724. "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
  725. "mpl.findpos = function(e) {\n",
  726. " //this section is from http://www.quirksmode.org/js/events_properties.html\n",
  727. " var targ;\n",
  728. " if (!e)\n",
  729. " e = window.event;\n",
  730. " if (e.target)\n",
  731. " targ = e.target;\n",
  732. " else if (e.srcElement)\n",
  733. " targ = e.srcElement;\n",
  734. " if (targ.nodeType == 3) // defeat Safari bug\n",
  735. " targ = targ.parentNode;\n",
  736. "\n",
  737. " // jQuery normalizes the pageX and pageY\n",
  738. " // pageX,Y are the mouse positions relative to the document\n",
  739. " // offset() returns the position of the element relative to the document\n",
  740. " var x = e.pageX - $(targ).offset().left;\n",
  741. " var y = e.pageY - $(targ).offset().top;\n",
  742. "\n",
  743. " return {\"x\": x, \"y\": y};\n",
  744. "};\n",
  745. "\n",
  746. "/*\n",
  747. " * return a copy of an object with only non-object keys\n",
  748. " * we need this to avoid circular references\n",
  749. " * http://stackoverflow.com/a/24161582/3208463\n",
  750. " */\n",
  751. "function simpleKeys (original) {\n",
  752. " return Object.keys(original).reduce(function (obj, key) {\n",
  753. " if (typeof original[key] !== 'object')\n",
  754. " obj[key] = original[key]\n",
  755. " return obj;\n",
  756. " }, {});\n",
  757. "}\n",
  758. "\n",
  759. "mpl.figure.prototype.mouse_event = function(event, name) {\n",
  760. " var canvas_pos = mpl.findpos(event)\n",
  761. "\n",
  762. " if (name === 'button_press')\n",
  763. " {\n",
  764. " this.canvas.focus();\n",
  765. " this.canvas_div.focus();\n",
  766. " }\n",
  767. "\n",
  768. " var x = canvas_pos.x * mpl.ratio;\n",
  769. " var y = canvas_pos.y * mpl.ratio;\n",
  770. "\n",
  771. " this.send_message(name, {x: x, y: y, button: event.button,\n",
  772. " step: event.step,\n",
  773. " guiEvent: simpleKeys(event)});\n",
  774. "\n",
  775. " /* This prevents the web browser from automatically changing to\n",
  776. " * the text insertion cursor when the button is pressed. We want\n",
  777. " * to control all of the cursor setting manually through the\n",
  778. " * 'cursor' event from matplotlib */\n",
  779. " event.preventDefault();\n",
  780. " return false;\n",
  781. "}\n",
  782. "\n",
  783. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  784. " // Handle any extra behaviour associated with a key event\n",
  785. "}\n",
  786. "\n",
  787. "mpl.figure.prototype.key_event = function(event, name) {\n",
  788. "\n",
  789. " // Prevent repeat events\n",
  790. " if (name == 'key_press')\n",
  791. " {\n",
  792. " if (event.which === this._key)\n",
  793. " return;\n",
  794. " else\n",
  795. " this._key = event.which;\n",
  796. " }\n",
  797. " if (name == 'key_release')\n",
  798. " this._key = null;\n",
  799. "\n",
  800. " var value = '';\n",
  801. " if (event.ctrlKey && event.which != 17)\n",
  802. " value += \"ctrl+\";\n",
  803. " if (event.altKey && event.which != 18)\n",
  804. " value += \"alt+\";\n",
  805. " if (event.shiftKey && event.which != 16)\n",
  806. " value += \"shift+\";\n",
  807. "\n",
  808. " value += 'k';\n",
  809. " value += event.which.toString();\n",
  810. "\n",
  811. " this._key_event_extra(event, name);\n",
  812. "\n",
  813. " this.send_message(name, {key: value,\n",
  814. " guiEvent: simpleKeys(event)});\n",
  815. " return false;\n",
  816. "}\n",
  817. "\n",
  818. "mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
  819. " if (name == 'download') {\n",
  820. " this.handle_save(this, null);\n",
  821. " } else {\n",
  822. " this.send_message(\"toolbar_button\", {name: name});\n",
  823. " }\n",
  824. "};\n",
  825. "\n",
  826. "mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
  827. " this.message.textContent = tooltip;\n",
  828. "};\n",
  829. "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
  830. "\n",
  831. "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n",
  832. "\n",
  833. "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
  834. " // Create a \"websocket\"-like object which calls the given IPython comm\n",
  835. " // object with the appropriate methods. Currently this is a non binary\n",
  836. " // socket, so there is still some room for performance tuning.\n",
  837. " var ws = {};\n",
  838. "\n",
  839. " ws.close = function() {\n",
  840. " comm.close()\n",
  841. " };\n",
  842. " ws.send = function(m) {\n",
  843. " //console.log('sending', m);\n",
  844. " comm.send(m);\n",
  845. " };\n",
  846. " // Register the callback with on_msg.\n",
  847. " comm.on_msg(function(msg) {\n",
  848. " //console.log('receiving', msg['content']['data'], msg);\n",
  849. " // Pass the mpl event to the overridden (by mpl) onmessage function.\n",
  850. " ws.onmessage(msg['content']['data'])\n",
  851. " });\n",
  852. " return ws;\n",
  853. "}\n",
  854. "\n",
  855. "mpl.mpl_figure_comm = function(comm, msg) {\n",
  856. " // This is the function which gets called when the mpl process\n",
  857. " // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
  858. "\n",
  859. " var id = msg.content.data.id;\n",
  860. " // Get hold of the div created by the display call when the Comm\n",
  861. " // socket was opened in Python.\n",
  862. " var element = $(\"#\" + id);\n",
  863. " var ws_proxy = comm_websocket_adapter(comm)\n",
  864. "\n",
  865. " function ondownload(figure, format) {\n",
  866. " window.open(figure.imageObj.src);\n",
  867. " }\n",
  868. "\n",
  869. " var fig = new mpl.figure(id, ws_proxy,\n",
  870. " ondownload,\n",
  871. " element.get(0));\n",
  872. "\n",
  873. " // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
  874. " // web socket which is closed, not our websocket->open comm proxy.\n",
  875. " ws_proxy.onopen();\n",
  876. "\n",
  877. " fig.parent_element = element.get(0);\n",
  878. " fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
  879. " if (!fig.cell_info) {\n",
  880. " console.error(\"Failed to find cell for figure\", id, fig);\n",
  881. " return;\n",
  882. " }\n",
  883. "\n",
  884. " var output_index = fig.cell_info[2]\n",
  885. " var cell = fig.cell_info[0];\n",
  886. "\n",
  887. "};\n",
  888. "\n",
  889. "mpl.figure.prototype.handle_close = function(fig, msg) {\n",
  890. " var width = fig.canvas.width/mpl.ratio\n",
  891. " fig.root.unbind('remove')\n",
  892. "\n",
  893. " // Update the output cell to use the data from the current canvas.\n",
  894. " fig.push_to_output();\n",
  895. " var dataURL = fig.canvas.toDataURL();\n",
  896. " // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
  897. " // the notebook keyboard shortcuts fail.\n",
  898. " IPython.keyboard_manager.enable()\n",
  899. " $(fig.parent_element).html('<img src=\"' + dataURL + '\" width=\"' + width + '\">');\n",
  900. " fig.close_ws(fig, msg);\n",
  901. "}\n",
  902. "\n",
  903. "mpl.figure.prototype.close_ws = function(fig, msg){\n",
  904. " fig.send_message('closing', msg);\n",
  905. " // fig.ws.close()\n",
  906. "}\n",
  907. "\n",
  908. "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
  909. " // Turn the data on the canvas into data in the output cell.\n",
  910. " var width = this.canvas.width/mpl.ratio\n",
  911. " var dataURL = this.canvas.toDataURL();\n",
  912. " this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
  913. "}\n",
  914. "\n",
  915. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  916. " // Tell IPython that the notebook contents must change.\n",
  917. " IPython.notebook.set_dirty(true);\n",
  918. " this.send_message(\"ack\", {});\n",
  919. " var fig = this;\n",
  920. " // Wait a second, then push the new image to the DOM so\n",
  921. " // that it is saved nicely (might be nice to debounce this).\n",
  922. " setTimeout(function () { fig.push_to_output() }, 1000);\n",
  923. "}\n",
  924. "\n",
  925. "mpl.figure.prototype._init_toolbar = function() {\n",
  926. " var fig = this;\n",
  927. "\n",
  928. " var nav_element = $('<div/>')\n",
  929. " nav_element.attr('style', 'width: 100%');\n",
  930. " this.root.append(nav_element);\n",
  931. "\n",
  932. " // Define a callback function for later on.\n",
  933. " function toolbar_event(event) {\n",
  934. " return fig.toolbar_button_onclick(event['data']);\n",
  935. " }\n",
  936. " function toolbar_mouse_event(event) {\n",
  937. " return fig.toolbar_button_onmouseover(event['data']);\n",
  938. " }\n",
  939. "\n",
  940. " for(var toolbar_ind in mpl.toolbar_items){\n",
  941. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  942. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  943. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  944. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  945. "\n",
  946. " if (!name) { continue; };\n",
  947. "\n",
  948. " var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
  949. " button.click(method_name, toolbar_event);\n",
  950. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  951. " nav_element.append(button);\n",
  952. " }\n",
  953. "\n",
  954. " // Add the status bar.\n",
  955. " var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
  956. " nav_element.append(status_bar);\n",
  957. " this.message = status_bar[0];\n",
  958. "\n",
  959. " // Add the close button to the window.\n",
  960. " var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
  961. " var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
  962. " button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
  963. " button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
  964. " buttongrp.append(button);\n",
  965. " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
  966. " titlebar.prepend(buttongrp);\n",
  967. "}\n",
  968. "\n",
  969. "mpl.figure.prototype._root_extra_style = function(el){\n",
  970. " var fig = this\n",
  971. " el.on(\"remove\", function(){\n",
  972. "\tfig.close_ws(fig, {});\n",
  973. " });\n",
  974. "}\n",
  975. "\n",
  976. "mpl.figure.prototype._canvas_extra_style = function(el){\n",
  977. " // this is important to make the div 'focusable\n",
  978. " el.attr('tabindex', 0)\n",
  979. " // reach out to IPython and tell the keyboard manager to turn it's self\n",
  980. " // off when our div gets focus\n",
  981. "\n",
  982. " // location in version 3\n",
  983. " if (IPython.notebook.keyboard_manager) {\n",
  984. " IPython.notebook.keyboard_manager.register_events(el);\n",
  985. " }\n",
  986. " else {\n",
  987. " // location in version 2\n",
  988. " IPython.keyboard_manager.register_events(el);\n",
  989. " }\n",
  990. "\n",
  991. "}\n",
  992. "\n",
  993. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  994. " var manager = IPython.notebook.keyboard_manager;\n",
  995. " if (!manager)\n",
  996. " manager = IPython.keyboard_manager;\n",
  997. "\n",
  998. " // Check for shift+enter\n",
  999. " if (event.shiftKey && event.which == 13) {\n",
  1000. " this.canvas_div.blur();\n",
  1001. " event.shiftKey = false;\n",
  1002. " // Send a \"J\" for go to next cell\n",
  1003. " event.which = 74;\n",
  1004. " event.keyCode = 74;\n",
  1005. " manager.command_mode();\n",
  1006. " manager.handle_keydown(event);\n",
  1007. " }\n",
  1008. "}\n",
  1009. "\n",
  1010. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  1011. " fig.ondownload(fig, null);\n",
  1012. "}\n",
  1013. "\n",
  1014. "\n",
  1015. "mpl.find_output_cell = function(html_output) {\n",
  1016. " // Return the cell and output element which can be found *uniquely* in the notebook.\n",
  1017. " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
  1018. " // IPython event is triggered only after the cells have been serialised, which for\n",
  1019. " // our purposes (turning an active figure into a static one), is too late.\n",
  1020. " var cells = IPython.notebook.get_cells();\n",
  1021. " var ncells = cells.length;\n",
  1022. " for (var i=0; i<ncells; i++) {\n",
  1023. " var cell = cells[i];\n",
  1024. " if (cell.cell_type === 'code'){\n",
  1025. " for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
  1026. " var data = cell.output_area.outputs[j];\n",
  1027. " if (data.data) {\n",
  1028. " // IPython >= 3 moved mimebundle to data attribute of output\n",
  1029. " data = data.data;\n",
  1030. " }\n",
  1031. " if (data['text/html'] == html_output) {\n",
  1032. " return [cell, data, j];\n",
  1033. " }\n",
  1034. " }\n",
  1035. " }\n",
  1036. " }\n",
  1037. "}\n",
  1038. "\n",
  1039. "// Register the function which deals with the matplotlib target/channel.\n",
  1040. "// The kernel may be null if the page has been refreshed.\n",
  1041. "if (IPython.notebook.kernel != null) {\n",
  1042. " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
  1043. "}\n"
  1044. ],
  1045. "text/plain": [
  1046. "<IPython.core.display.Javascript object>"
  1047. ]
  1048. },
  1049. "metadata": {},
  1050. "output_type": "display_data"
  1051. },
  1052. {
  1053. "data": {
  1054. "text/html": [
  1055. "<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAEvklEQVR4nO3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4Mwu4AAehrHaUAAAAASUVORK5CYII=\" width=\"640\">"
  1056. ],
  1057. "text/plain": [
  1058. "<IPython.core.display.HTML object>"
  1059. ]
  1060. },
  1061. "metadata": {},
  1062. "output_type": "display_data"
  1063. },
  1064. {
  1065. "data": {
  1066. "application/javascript": [
  1067. "/* Put everything inside the global mpl namespace */\n",
  1068. "window.mpl = {};\n",
  1069. "\n",
  1070. "\n",
  1071. "mpl.get_websocket_type = function() {\n",
  1072. " if (typeof(WebSocket) !== 'undefined') {\n",
  1073. " return WebSocket;\n",
  1074. " } else if (typeof(MozWebSocket) !== 'undefined') {\n",
  1075. " return MozWebSocket;\n",
  1076. " } else {\n",
  1077. " alert('Your browser does not have WebSocket support.' +\n",
  1078. " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
  1079. " 'Firefox 4 and 5 are also supported but you ' +\n",
  1080. " 'have to enable WebSockets in about:config.');\n",
  1081. " };\n",
  1082. "}\n",
  1083. "\n",
  1084. "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
  1085. " this.id = figure_id;\n",
  1086. "\n",
  1087. " this.ws = websocket;\n",
  1088. "\n",
  1089. " this.supports_binary = (this.ws.binaryType != undefined);\n",
  1090. "\n",
  1091. " if (!this.supports_binary) {\n",
  1092. " var warnings = document.getElementById(\"mpl-warnings\");\n",
  1093. " if (warnings) {\n",
  1094. " warnings.style.display = 'block';\n",
  1095. " warnings.textContent = (\n",
  1096. " \"This browser does not support binary websocket messages. \" +\n",
  1097. " \"Performance may be slow.\");\n",
  1098. " }\n",
  1099. " }\n",
  1100. "\n",
  1101. " this.imageObj = new Image();\n",
  1102. "\n",
  1103. " this.context = undefined;\n",
  1104. " this.message = undefined;\n",
  1105. " this.canvas = undefined;\n",
  1106. " this.rubberband_canvas = undefined;\n",
  1107. " this.rubberband_context = undefined;\n",
  1108. " this.format_dropdown = undefined;\n",
  1109. "\n",
  1110. " this.image_mode = 'full';\n",
  1111. "\n",
  1112. " this.root = $('<div/>');\n",
  1113. " this._root_extra_style(this.root)\n",
  1114. " this.root.attr('style', 'display: inline-block');\n",
  1115. "\n",
  1116. " $(parent_element).append(this.root);\n",
  1117. "\n",
  1118. " this._init_header(this);\n",
  1119. " this._init_canvas(this);\n",
  1120. " this._init_toolbar(this);\n",
  1121. "\n",
  1122. " var fig = this;\n",
  1123. "\n",
  1124. " this.waiting = false;\n",
  1125. "\n",
  1126. " this.ws.onopen = function () {\n",
  1127. " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
  1128. " fig.send_message(\"send_image_mode\", {});\n",
  1129. " if (mpl.ratio != 1) {\n",
  1130. " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
  1131. " }\n",
  1132. " fig.send_message(\"refresh\", {});\n",
  1133. " }\n",
  1134. "\n",
  1135. " this.imageObj.onload = function() {\n",
  1136. " if (fig.image_mode == 'full') {\n",
  1137. " // Full images could contain transparency (where diff images\n",
  1138. " // almost always do), so we need to clear the canvas so that\n",
  1139. " // there is no ghosting.\n",
  1140. " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
  1141. " }\n",
  1142. " fig.context.drawImage(fig.imageObj, 0, 0);\n",
  1143. " };\n",
  1144. "\n",
  1145. " this.imageObj.onunload = function() {\n",
  1146. " fig.ws.close();\n",
  1147. " }\n",
  1148. "\n",
  1149. " this.ws.onmessage = this._make_on_message_function(this);\n",
  1150. "\n",
  1151. " this.ondownload = ondownload;\n",
  1152. "}\n",
  1153. "\n",
  1154. "mpl.figure.prototype._init_header = function() {\n",
  1155. " var titlebar = $(\n",
  1156. " '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
  1157. " 'ui-helper-clearfix\"/>');\n",
  1158. " var titletext = $(\n",
  1159. " '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
  1160. " 'text-align: center; padding: 3px;\"/>');\n",
  1161. " titlebar.append(titletext)\n",
  1162. " this.root.append(titlebar);\n",
  1163. " this.header = titletext[0];\n",
  1164. "}\n",
  1165. "\n",
  1166. "\n",
  1167. "\n",
  1168. "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
  1169. "\n",
  1170. "}\n",
  1171. "\n",
  1172. "\n",
  1173. "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
  1174. "\n",
  1175. "}\n",
  1176. "\n",
  1177. "mpl.figure.prototype._init_canvas = function() {\n",
  1178. " var fig = this;\n",
  1179. "\n",
  1180. " var canvas_div = $('<div/>');\n",
  1181. "\n",
  1182. " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
  1183. "\n",
  1184. " function canvas_keyboard_event(event) {\n",
  1185. " return fig.key_event(event, event['data']);\n",
  1186. " }\n",
  1187. "\n",
  1188. " canvas_div.keydown('key_press', canvas_keyboard_event);\n",
  1189. " canvas_div.keyup('key_release', canvas_keyboard_event);\n",
  1190. " this.canvas_div = canvas_div\n",
  1191. " this._canvas_extra_style(canvas_div)\n",
  1192. " this.root.append(canvas_div);\n",
  1193. "\n",
  1194. " var canvas = $('<canvas/>');\n",
  1195. " canvas.addClass('mpl-canvas');\n",
  1196. " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
  1197. "\n",
  1198. " this.canvas = canvas[0];\n",
  1199. " this.context = canvas[0].getContext(\"2d\");\n",
  1200. "\n",
  1201. " var backingStore = this.context.backingStorePixelRatio ||\n",
  1202. "\tthis.context.webkitBackingStorePixelRatio ||\n",
  1203. "\tthis.context.mozBackingStorePixelRatio ||\n",
  1204. "\tthis.context.msBackingStorePixelRatio ||\n",
  1205. "\tthis.context.oBackingStorePixelRatio ||\n",
  1206. "\tthis.context.backingStorePixelRatio || 1;\n",
  1207. "\n",
  1208. " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
  1209. "\n",
  1210. " var rubberband = $('<canvas/>');\n",
  1211. " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
  1212. "\n",
  1213. " var pass_mouse_events = true;\n",
  1214. "\n",
  1215. " canvas_div.resizable({\n",
  1216. " start: function(event, ui) {\n",
  1217. " pass_mouse_events = false;\n",
  1218. " },\n",
  1219. " resize: function(event, ui) {\n",
  1220. " fig.request_resize(ui.size.width, ui.size.height);\n",
  1221. " },\n",
  1222. " stop: function(event, ui) {\n",
  1223. " pass_mouse_events = true;\n",
  1224. " fig.request_resize(ui.size.width, ui.size.height);\n",
  1225. " },\n",
  1226. " });\n",
  1227. "\n",
  1228. " function mouse_event_fn(event) {\n",
  1229. " if (pass_mouse_events)\n",
  1230. " return fig.mouse_event(event, event['data']);\n",
  1231. " }\n",
  1232. "\n",
  1233. " rubberband.mousedown('button_press', mouse_event_fn);\n",
  1234. " rubberband.mouseup('button_release', mouse_event_fn);\n",
  1235. " // Throttle sequential mouse events to 1 every 20ms.\n",
  1236. " rubberband.mousemove('motion_notify', mouse_event_fn);\n",
  1237. "\n",
  1238. " rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
  1239. " rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
  1240. "\n",
  1241. " canvas_div.on(\"wheel\", function (event) {\n",
  1242. " event = event.originalEvent;\n",
  1243. " event['data'] = 'scroll'\n",
  1244. " if (event.deltaY < 0) {\n",
  1245. " event.step = 1;\n",
  1246. " } else {\n",
  1247. " event.step = -1;\n",
  1248. " }\n",
  1249. " mouse_event_fn(event);\n",
  1250. " });\n",
  1251. "\n",
  1252. " canvas_div.append(canvas);\n",
  1253. " canvas_div.append(rubberband);\n",
  1254. "\n",
  1255. " this.rubberband = rubberband;\n",
  1256. " this.rubberband_canvas = rubberband[0];\n",
  1257. " this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
  1258. " this.rubberband_context.strokeStyle = \"#000000\";\n",
  1259. "\n",
  1260. " this._resize_canvas = function(width, height) {\n",
  1261. " // Keep the size of the canvas, canvas container, and rubber band\n",
  1262. " // canvas in synch.\n",
  1263. " canvas_div.css('width', width)\n",
  1264. " canvas_div.css('height', height)\n",
  1265. "\n",
  1266. " canvas.attr('width', width * mpl.ratio);\n",
  1267. " canvas.attr('height', height * mpl.ratio);\n",
  1268. " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
  1269. "\n",
  1270. " rubberband.attr('width', width);\n",
  1271. " rubberband.attr('height', height);\n",
  1272. " }\n",
  1273. "\n",
  1274. " // Set the figure to an initial 600x600px, this will subsequently be updated\n",
  1275. " // upon first draw.\n",
  1276. " this._resize_canvas(600, 600);\n",
  1277. "\n",
  1278. " // Disable right mouse context menu.\n",
  1279. " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
  1280. " return false;\n",
  1281. " });\n",
  1282. "\n",
  1283. " function set_focus () {\n",
  1284. " canvas.focus();\n",
  1285. " canvas_div.focus();\n",
  1286. " }\n",
  1287. "\n",
  1288. " window.setTimeout(set_focus, 100);\n",
  1289. "}\n",
  1290. "\n",
  1291. "mpl.figure.prototype._init_toolbar = function() {\n",
  1292. " var fig = this;\n",
  1293. "\n",
  1294. " var nav_element = $('<div/>')\n",
  1295. " nav_element.attr('style', 'width: 100%');\n",
  1296. " this.root.append(nav_element);\n",
  1297. "\n",
  1298. " // Define a callback function for later on.\n",
  1299. " function toolbar_event(event) {\n",
  1300. " return fig.toolbar_button_onclick(event['data']);\n",
  1301. " }\n",
  1302. " function toolbar_mouse_event(event) {\n",
  1303. " return fig.toolbar_button_onmouseover(event['data']);\n",
  1304. " }\n",
  1305. "\n",
  1306. " for(var toolbar_ind in mpl.toolbar_items) {\n",
  1307. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  1308. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  1309. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  1310. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  1311. "\n",
  1312. " if (!name) {\n",
  1313. " // put a spacer in here.\n",
  1314. " continue;\n",
  1315. " }\n",
  1316. " var button = $('<button/>');\n",
  1317. " button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
  1318. " 'ui-button-icon-only');\n",
  1319. " button.attr('role', 'button');\n",
  1320. " button.attr('aria-disabled', 'false');\n",
  1321. " button.click(method_name, toolbar_event);\n",
  1322. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  1323. "\n",
  1324. " var icon_img = $('<span/>');\n",
  1325. " icon_img.addClass('ui-button-icon-primary ui-icon');\n",
  1326. " icon_img.addClass(image);\n",
  1327. " icon_img.addClass('ui-corner-all');\n",
  1328. "\n",
  1329. " var tooltip_span = $('<span/>');\n",
  1330. " tooltip_span.addClass('ui-button-text');\n",
  1331. " tooltip_span.html(tooltip);\n",
  1332. "\n",
  1333. " button.append(icon_img);\n",
  1334. " button.append(tooltip_span);\n",
  1335. "\n",
  1336. " nav_element.append(button);\n",
  1337. " }\n",
  1338. "\n",
  1339. " var fmt_picker_span = $('<span/>');\n",
  1340. "\n",
  1341. " var fmt_picker = $('<select/>');\n",
  1342. " fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
  1343. " fmt_picker_span.append(fmt_picker);\n",
  1344. " nav_element.append(fmt_picker_span);\n",
  1345. " this.format_dropdown = fmt_picker[0];\n",
  1346. "\n",
  1347. " for (var ind in mpl.extensions) {\n",
  1348. " var fmt = mpl.extensions[ind];\n",
  1349. " var option = $(\n",
  1350. " '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
  1351. " fmt_picker.append(option)\n",
  1352. " }\n",
  1353. "\n",
  1354. " // Add hover states to the ui-buttons\n",
  1355. " $( \".ui-button\" ).hover(\n",
  1356. " function() { $(this).addClass(\"ui-state-hover\");},\n",
  1357. " function() { $(this).removeClass(\"ui-state-hover\");}\n",
  1358. " );\n",
  1359. "\n",
  1360. " var status_bar = $('<span class=\"mpl-message\"/>');\n",
  1361. " nav_element.append(status_bar);\n",
  1362. " this.message = status_bar[0];\n",
  1363. "}\n",
  1364. "\n",
  1365. "mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
  1366. " // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
  1367. " // which will in turn request a refresh of the image.\n",
  1368. " this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
  1369. "}\n",
  1370. "\n",
  1371. "mpl.figure.prototype.send_message = function(type, properties) {\n",
  1372. " properties['type'] = type;\n",
  1373. " properties['figure_id'] = this.id;\n",
  1374. " this.ws.send(JSON.stringify(properties));\n",
  1375. "}\n",
  1376. "\n",
  1377. "mpl.figure.prototype.send_draw_message = function() {\n",
  1378. " if (!this.waiting) {\n",
  1379. " this.waiting = true;\n",
  1380. " this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
  1381. " }\n",
  1382. "}\n",
  1383. "\n",
  1384. "\n",
  1385. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  1386. " var format_dropdown = fig.format_dropdown;\n",
  1387. " var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
  1388. " fig.ondownload(fig, format);\n",
  1389. "}\n",
  1390. "\n",
  1391. "\n",
  1392. "mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
  1393. " var size = msg['size'];\n",
  1394. " if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
  1395. " fig._resize_canvas(size[0], size[1]);\n",
  1396. " fig.send_message(\"refresh\", {});\n",
  1397. " };\n",
  1398. "}\n",
  1399. "\n",
  1400. "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
  1401. " var x0 = msg['x0'] / mpl.ratio;\n",
  1402. " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
  1403. " var x1 = msg['x1'] / mpl.ratio;\n",
  1404. " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
  1405. " x0 = Math.floor(x0) + 0.5;\n",
  1406. " y0 = Math.floor(y0) + 0.5;\n",
  1407. " x1 = Math.floor(x1) + 0.5;\n",
  1408. " y1 = Math.floor(y1) + 0.5;\n",
  1409. " var min_x = Math.min(x0, x1);\n",
  1410. " var min_y = Math.min(y0, y1);\n",
  1411. " var width = Math.abs(x1 - x0);\n",
  1412. " var height = Math.abs(y1 - y0);\n",
  1413. "\n",
  1414. " fig.rubberband_context.clearRect(\n",
  1415. " 0, 0, fig.canvas.width, fig.canvas.height);\n",
  1416. "\n",
  1417. " fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
  1418. "}\n",
  1419. "\n",
  1420. "mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
  1421. " // Updates the figure title.\n",
  1422. " fig.header.textContent = msg['label'];\n",
  1423. "}\n",
  1424. "\n",
  1425. "mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
  1426. " var cursor = msg['cursor'];\n",
  1427. " switch(cursor)\n",
  1428. " {\n",
  1429. " case 0:\n",
  1430. " cursor = 'pointer';\n",
  1431. " break;\n",
  1432. " case 1:\n",
  1433. " cursor = 'default';\n",
  1434. " break;\n",
  1435. " case 2:\n",
  1436. " cursor = 'crosshair';\n",
  1437. " break;\n",
  1438. " case 3:\n",
  1439. " cursor = 'move';\n",
  1440. " break;\n",
  1441. " }\n",
  1442. " fig.rubberband_canvas.style.cursor = cursor;\n",
  1443. "}\n",
  1444. "\n",
  1445. "mpl.figure.prototype.handle_message = function(fig, msg) {\n",
  1446. " fig.message.textContent = msg['message'];\n",
  1447. "}\n",
  1448. "\n",
  1449. "mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
  1450. " // Request the server to send over a new figure.\n",
  1451. " fig.send_draw_message();\n",
  1452. "}\n",
  1453. "\n",
  1454. "mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
  1455. " fig.image_mode = msg['mode'];\n",
  1456. "}\n",
  1457. "\n",
  1458. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  1459. " // Called whenever the canvas gets updated.\n",
  1460. " this.send_message(\"ack\", {});\n",
  1461. "}\n",
  1462. "\n",
  1463. "// A function to construct a web socket function for onmessage handling.\n",
  1464. "// Called in the figure constructor.\n",
  1465. "mpl.figure.prototype._make_on_message_function = function(fig) {\n",
  1466. " return function socket_on_message(evt) {\n",
  1467. " if (evt.data instanceof Blob) {\n",
  1468. " /* FIXME: We get \"Resource interpreted as Image but\n",
  1469. " * transferred with MIME type text/plain:\" errors on\n",
  1470. " * Chrome. But how to set the MIME type? It doesn't seem\n",
  1471. " * to be part of the websocket stream */\n",
  1472. " evt.data.type = \"image/png\";\n",
  1473. "\n",
  1474. " /* Free the memory for the previous frames */\n",
  1475. " if (fig.imageObj.src) {\n",
  1476. " (window.URL || window.webkitURL).revokeObjectURL(\n",
  1477. " fig.imageObj.src);\n",
  1478. " }\n",
  1479. "\n",
  1480. " fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
  1481. " evt.data);\n",
  1482. " fig.updated_canvas_event();\n",
  1483. " fig.waiting = false;\n",
  1484. " return;\n",
  1485. " }\n",
  1486. " else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
  1487. " fig.imageObj.src = evt.data;\n",
  1488. " fig.updated_canvas_event();\n",
  1489. " fig.waiting = false;\n",
  1490. " return;\n",
  1491. " }\n",
  1492. "\n",
  1493. " var msg = JSON.parse(evt.data);\n",
  1494. " var msg_type = msg['type'];\n",
  1495. "\n",
  1496. " // Call the \"handle_{type}\" callback, which takes\n",
  1497. " // the figure and JSON message as its only arguments.\n",
  1498. " try {\n",
  1499. " var callback = fig[\"handle_\" + msg_type];\n",
  1500. " } catch (e) {\n",
  1501. " console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
  1502. " return;\n",
  1503. " }\n",
  1504. "\n",
  1505. " if (callback) {\n",
  1506. " try {\n",
  1507. " // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
  1508. " callback(fig, msg);\n",
  1509. " } catch (e) {\n",
  1510. " console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
  1511. " }\n",
  1512. " }\n",
  1513. " };\n",
  1514. "}\n",
  1515. "\n",
  1516. "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
  1517. "mpl.findpos = function(e) {\n",
  1518. " //this section is from http://www.quirksmode.org/js/events_properties.html\n",
  1519. " var targ;\n",
  1520. " if (!e)\n",
  1521. " e = window.event;\n",
  1522. " if (e.target)\n",
  1523. " targ = e.target;\n",
  1524. " else if (e.srcElement)\n",
  1525. " targ = e.srcElement;\n",
  1526. " if (targ.nodeType == 3) // defeat Safari bug\n",
  1527. " targ = targ.parentNode;\n",
  1528. "\n",
  1529. " // jQuery normalizes the pageX and pageY\n",
  1530. " // pageX,Y are the mouse positions relative to the document\n",
  1531. " // offset() returns the position of the element relative to the document\n",
  1532. " var x = e.pageX - $(targ).offset().left;\n",
  1533. " var y = e.pageY - $(targ).offset().top;\n",
  1534. "\n",
  1535. " return {\"x\": x, \"y\": y};\n",
  1536. "};\n",
  1537. "\n",
  1538. "/*\n",
  1539. " * return a copy of an object with only non-object keys\n",
  1540. " * we need this to avoid circular references\n",
  1541. " * http://stackoverflow.com/a/24161582/3208463\n",
  1542. " */\n",
  1543. "function simpleKeys (original) {\n",
  1544. " return Object.keys(original).reduce(function (obj, key) {\n",
  1545. " if (typeof original[key] !== 'object')\n",
  1546. " obj[key] = original[key]\n",
  1547. " return obj;\n",
  1548. " }, {});\n",
  1549. "}\n",
  1550. "\n",
  1551. "mpl.figure.prototype.mouse_event = function(event, name) {\n",
  1552. " var canvas_pos = mpl.findpos(event)\n",
  1553. "\n",
  1554. " if (name === 'button_press')\n",
  1555. " {\n",
  1556. " this.canvas.focus();\n",
  1557. " this.canvas_div.focus();\n",
  1558. " }\n",
  1559. "\n",
  1560. " var x = canvas_pos.x * mpl.ratio;\n",
  1561. " var y = canvas_pos.y * mpl.ratio;\n",
  1562. "\n",
  1563. " this.send_message(name, {x: x, y: y, button: event.button,\n",
  1564. " step: event.step,\n",
  1565. " guiEvent: simpleKeys(event)});\n",
  1566. "\n",
  1567. " /* This prevents the web browser from automatically changing to\n",
  1568. " * the text insertion cursor when the button is pressed. We want\n",
  1569. " * to control all of the cursor setting manually through the\n",
  1570. " * 'cursor' event from matplotlib */\n",
  1571. " event.preventDefault();\n",
  1572. " return false;\n",
  1573. "}\n",
  1574. "\n",
  1575. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  1576. " // Handle any extra behaviour associated with a key event\n",
  1577. "}\n",
  1578. "\n",
  1579. "mpl.figure.prototype.key_event = function(event, name) {\n",
  1580. "\n",
  1581. " // Prevent repeat events\n",
  1582. " if (name == 'key_press')\n",
  1583. " {\n",
  1584. " if (event.which === this._key)\n",
  1585. " return;\n",
  1586. " else\n",
  1587. " this._key = event.which;\n",
  1588. " }\n",
  1589. " if (name == 'key_release')\n",
  1590. " this._key = null;\n",
  1591. "\n",
  1592. " var value = '';\n",
  1593. " if (event.ctrlKey && event.which != 17)\n",
  1594. " value += \"ctrl+\";\n",
  1595. " if (event.altKey && event.which != 18)\n",
  1596. " value += \"alt+\";\n",
  1597. " if (event.shiftKey && event.which != 16)\n",
  1598. " value += \"shift+\";\n",
  1599. "\n",
  1600. " value += 'k';\n",
  1601. " value += event.which.toString();\n",
  1602. "\n",
  1603. " this._key_event_extra(event, name);\n",
  1604. "\n",
  1605. " this.send_message(name, {key: value,\n",
  1606. " guiEvent: simpleKeys(event)});\n",
  1607. " return false;\n",
  1608. "}\n",
  1609. "\n",
  1610. "mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
  1611. " if (name == 'download') {\n",
  1612. " this.handle_save(this, null);\n",
  1613. " } else {\n",
  1614. " this.send_message(\"toolbar_button\", {name: name});\n",
  1615. " }\n",
  1616. "};\n",
  1617. "\n",
  1618. "mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
  1619. " this.message.textContent = tooltip;\n",
  1620. "};\n",
  1621. "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
  1622. "\n",
  1623. "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n",
  1624. "\n",
  1625. "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
  1626. " // Create a \"websocket\"-like object which calls the given IPython comm\n",
  1627. " // object with the appropriate methods. Currently this is a non binary\n",
  1628. " // socket, so there is still some room for performance tuning.\n",
  1629. " var ws = {};\n",
  1630. "\n",
  1631. " ws.close = function() {\n",
  1632. " comm.close()\n",
  1633. " };\n",
  1634. " ws.send = function(m) {\n",
  1635. " //console.log('sending', m);\n",
  1636. " comm.send(m);\n",
  1637. " };\n",
  1638. " // Register the callback with on_msg.\n",
  1639. " comm.on_msg(function(msg) {\n",
  1640. " //console.log('receiving', msg['content']['data'], msg);\n",
  1641. " // Pass the mpl event to the overridden (by mpl) onmessage function.\n",
  1642. " ws.onmessage(msg['content']['data'])\n",
  1643. " });\n",
  1644. " return ws;\n",
  1645. "}\n",
  1646. "\n",
  1647. "mpl.mpl_figure_comm = function(comm, msg) {\n",
  1648. " // This is the function which gets called when the mpl process\n",
  1649. " // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
  1650. "\n",
  1651. " var id = msg.content.data.id;\n",
  1652. " // Get hold of the div created by the display call when the Comm\n",
  1653. " // socket was opened in Python.\n",
  1654. " var element = $(\"#\" + id);\n",
  1655. " var ws_proxy = comm_websocket_adapter(comm)\n",
  1656. "\n",
  1657. " function ondownload(figure, format) {\n",
  1658. " window.open(figure.imageObj.src);\n",
  1659. " }\n",
  1660. "\n",
  1661. " var fig = new mpl.figure(id, ws_proxy,\n",
  1662. " ondownload,\n",
  1663. " element.get(0));\n",
  1664. "\n",
  1665. " // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
  1666. " // web socket which is closed, not our websocket->open comm proxy.\n",
  1667. " ws_proxy.onopen();\n",
  1668. "\n",
  1669. " fig.parent_element = element.get(0);\n",
  1670. " fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
  1671. " if (!fig.cell_info) {\n",
  1672. " console.error(\"Failed to find cell for figure\", id, fig);\n",
  1673. " return;\n",
  1674. " }\n",
  1675. "\n",
  1676. " var output_index = fig.cell_info[2]\n",
  1677. " var cell = fig.cell_info[0];\n",
  1678. "\n",
  1679. "};\n",
  1680. "\n",
  1681. "mpl.figure.prototype.handle_close = function(fig, msg) {\n",
  1682. " var width = fig.canvas.width/mpl.ratio\n",
  1683. " fig.root.unbind('remove')\n",
  1684. "\n",
  1685. " // Update the output cell to use the data from the current canvas.\n",
  1686. " fig.push_to_output();\n",
  1687. " var dataURL = fig.canvas.toDataURL();\n",
  1688. " // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
  1689. " // the notebook keyboard shortcuts fail.\n",
  1690. " IPython.keyboard_manager.enable()\n",
  1691. " $(fig.parent_element).html('<img src=\"' + dataURL + '\" width=\"' + width + '\">');\n",
  1692. " fig.close_ws(fig, msg);\n",
  1693. "}\n",
  1694. "\n",
  1695. "mpl.figure.prototype.close_ws = function(fig, msg){\n",
  1696. " fig.send_message('closing', msg);\n",
  1697. " // fig.ws.close()\n",
  1698. "}\n",
  1699. "\n",
  1700. "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
  1701. " // Turn the data on the canvas into data in the output cell.\n",
  1702. " var width = this.canvas.width/mpl.ratio\n",
  1703. " var dataURL = this.canvas.toDataURL();\n",
  1704. " this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
  1705. "}\n",
  1706. "\n",
  1707. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  1708. " // Tell IPython that the notebook contents must change.\n",
  1709. " IPython.notebook.set_dirty(true);\n",
  1710. " this.send_message(\"ack\", {});\n",
  1711. " var fig = this;\n",
  1712. " // Wait a second, then push the new image to the DOM so\n",
  1713. " // that it is saved nicely (might be nice to debounce this).\n",
  1714. " setTimeout(function () { fig.push_to_output() }, 1000);\n",
  1715. "}\n",
  1716. "\n",
  1717. "mpl.figure.prototype._init_toolbar = function() {\n",
  1718. " var fig = this;\n",
  1719. "\n",
  1720. " var nav_element = $('<div/>')\n",
  1721. " nav_element.attr('style', 'width: 100%');\n",
  1722. " this.root.append(nav_element);\n",
  1723. "\n",
  1724. " // Define a callback function for later on.\n",
  1725. " function toolbar_event(event) {\n",
  1726. " return fig.toolbar_button_onclick(event['data']);\n",
  1727. " }\n",
  1728. " function toolbar_mouse_event(event) {\n",
  1729. " return fig.toolbar_button_onmouseover(event['data']);\n",
  1730. " }\n",
  1731. "\n",
  1732. " for(var toolbar_ind in mpl.toolbar_items){\n",
  1733. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  1734. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  1735. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  1736. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  1737. "\n",
  1738. " if (!name) { continue; };\n",
  1739. "\n",
  1740. " var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
  1741. " button.click(method_name, toolbar_event);\n",
  1742. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  1743. " nav_element.append(button);\n",
  1744. " }\n",
  1745. "\n",
  1746. " // Add the status bar.\n",
  1747. " var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
  1748. " nav_element.append(status_bar);\n",
  1749. " this.message = status_bar[0];\n",
  1750. "\n",
  1751. " // Add the close button to the window.\n",
  1752. " var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
  1753. " var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
  1754. " button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
  1755. " button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
  1756. " buttongrp.append(button);\n",
  1757. " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
  1758. " titlebar.prepend(buttongrp);\n",
  1759. "}\n",
  1760. "\n",
  1761. "mpl.figure.prototype._root_extra_style = function(el){\n",
  1762. " var fig = this\n",
  1763. " el.on(\"remove\", function(){\n",
  1764. "\tfig.close_ws(fig, {});\n",
  1765. " });\n",
  1766. "}\n",
  1767. "\n",
  1768. "mpl.figure.prototype._canvas_extra_style = function(el){\n",
  1769. " // this is important to make the div 'focusable\n",
  1770. " el.attr('tabindex', 0)\n",
  1771. " // reach out to IPython and tell the keyboard manager to turn it's self\n",
  1772. " // off when our div gets focus\n",
  1773. "\n",
  1774. " // location in version 3\n",
  1775. " if (IPython.notebook.keyboard_manager) {\n",
  1776. " IPython.notebook.keyboard_manager.register_events(el);\n",
  1777. " }\n",
  1778. " else {\n",
  1779. " // location in version 2\n",
  1780. " IPython.keyboard_manager.register_events(el);\n",
  1781. " }\n",
  1782. "\n",
  1783. "}\n",
  1784. "\n",
  1785. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  1786. " var manager = IPython.notebook.keyboard_manager;\n",
  1787. " if (!manager)\n",
  1788. " manager = IPython.keyboard_manager;\n",
  1789. "\n",
  1790. " // Check for shift+enter\n",
  1791. " if (event.shiftKey && event.which == 13) {\n",
  1792. " this.canvas_div.blur();\n",
  1793. " event.shiftKey = false;\n",
  1794. " // Send a \"J\" for go to next cell\n",
  1795. " event.which = 74;\n",
  1796. " event.keyCode = 74;\n",
  1797. " manager.command_mode();\n",
  1798. " manager.handle_keydown(event);\n",
  1799. " }\n",
  1800. "}\n",
  1801. "\n",
  1802. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  1803. " fig.ondownload(fig, null);\n",
  1804. "}\n",
  1805. "\n",
  1806. "\n",
  1807. "mpl.find_output_cell = function(html_output) {\n",
  1808. " // Return the cell and output element which can be found *uniquely* in the notebook.\n",
  1809. " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
  1810. " // IPython event is triggered only after the cells have been serialised, which for\n",
  1811. " // our purposes (turning an active figure into a static one), is too late.\n",
  1812. " var cells = IPython.notebook.get_cells();\n",
  1813. " var ncells = cells.length;\n",
  1814. " for (var i=0; i<ncells; i++) {\n",
  1815. " var cell = cells[i];\n",
  1816. " if (cell.cell_type === 'code'){\n",
  1817. " for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
  1818. " var data = cell.output_area.outputs[j];\n",
  1819. " if (data.data) {\n",
  1820. " // IPython >= 3 moved mimebundle to data attribute of output\n",
  1821. " data = data.data;\n",
  1822. " }\n",
  1823. " if (data['text/html'] == html_output) {\n",
  1824. " return [cell, data, j];\n",
  1825. " }\n",
  1826. " }\n",
  1827. " }\n",
  1828. " }\n",
  1829. "}\n",
  1830. "\n",
  1831. "// Register the function which deals with the matplotlib target/channel.\n",
  1832. "// The kernel may be null if the page has been refreshed.\n",
  1833. "if (IPython.notebook.kernel != null) {\n",
  1834. " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
  1835. "}\n"
  1836. ],
  1837. "text/plain": [
  1838. "<IPython.core.display.Javascript object>"
  1839. ]
  1840. },
  1841. "metadata": {},
  1842. "output_type": "display_data"
  1843. },
  1844. {
  1845. "data": {
  1846. "text/html": [
  1847. "<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAEvklEQVR4nO3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4Mwu4AAehrHaUAAAAASUVORK5CYII=\" width=\"640\">"
  1848. ],
  1849. "text/plain": [
  1850. "<IPython.core.display.HTML object>"
  1851. ]
  1852. },
  1853. "metadata": {},
  1854. "output_type": "display_data"
  1855. },
  1856. {
  1857. "data": {
  1858. "application/javascript": [
  1859. "/* Put everything inside the global mpl namespace */\n",
  1860. "window.mpl = {};\n",
  1861. "\n",
  1862. "\n",
  1863. "mpl.get_websocket_type = function() {\n",
  1864. " if (typeof(WebSocket) !== 'undefined') {\n",
  1865. " return WebSocket;\n",
  1866. " } else if (typeof(MozWebSocket) !== 'undefined') {\n",
  1867. " return MozWebSocket;\n",
  1868. " } else {\n",
  1869. " alert('Your browser does not have WebSocket support.' +\n",
  1870. " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
  1871. " 'Firefox 4 and 5 are also supported but you ' +\n",
  1872. " 'have to enable WebSockets in about:config.');\n",
  1873. " };\n",
  1874. "}\n",
  1875. "\n",
  1876. "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
  1877. " this.id = figure_id;\n",
  1878. "\n",
  1879. " this.ws = websocket;\n",
  1880. "\n",
  1881. " this.supports_binary = (this.ws.binaryType != undefined);\n",
  1882. "\n",
  1883. " if (!this.supports_binary) {\n",
  1884. " var warnings = document.getElementById(\"mpl-warnings\");\n",
  1885. " if (warnings) {\n",
  1886. " warnings.style.display = 'block';\n",
  1887. " warnings.textContent = (\n",
  1888. " \"This browser does not support binary websocket messages. \" +\n",
  1889. " \"Performance may be slow.\");\n",
  1890. " }\n",
  1891. " }\n",
  1892. "\n",
  1893. " this.imageObj = new Image();\n",
  1894. "\n",
  1895. " this.context = undefined;\n",
  1896. " this.message = undefined;\n",
  1897. " this.canvas = undefined;\n",
  1898. " this.rubberband_canvas = undefined;\n",
  1899. " this.rubberband_context = undefined;\n",
  1900. " this.format_dropdown = undefined;\n",
  1901. "\n",
  1902. " this.image_mode = 'full';\n",
  1903. "\n",
  1904. " this.root = $('<div/>');\n",
  1905. " this._root_extra_style(this.root)\n",
  1906. " this.root.attr('style', 'display: inline-block');\n",
  1907. "\n",
  1908. " $(parent_element).append(this.root);\n",
  1909. "\n",
  1910. " this._init_header(this);\n",
  1911. " this._init_canvas(this);\n",
  1912. " this._init_toolbar(this);\n",
  1913. "\n",
  1914. " var fig = this;\n",
  1915. "\n",
  1916. " this.waiting = false;\n",
  1917. "\n",
  1918. " this.ws.onopen = function () {\n",
  1919. " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
  1920. " fig.send_message(\"send_image_mode\", {});\n",
  1921. " if (mpl.ratio != 1) {\n",
  1922. " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
  1923. " }\n",
  1924. " fig.send_message(\"refresh\", {});\n",
  1925. " }\n",
  1926. "\n",
  1927. " this.imageObj.onload = function() {\n",
  1928. " if (fig.image_mode == 'full') {\n",
  1929. " // Full images could contain transparency (where diff images\n",
  1930. " // almost always do), so we need to clear the canvas so that\n",
  1931. " // there is no ghosting.\n",
  1932. " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
  1933. " }\n",
  1934. " fig.context.drawImage(fig.imageObj, 0, 0);\n",
  1935. " };\n",
  1936. "\n",
  1937. " this.imageObj.onunload = function() {\n",
  1938. " fig.ws.close();\n",
  1939. " }\n",
  1940. "\n",
  1941. " this.ws.onmessage = this._make_on_message_function(this);\n",
  1942. "\n",
  1943. " this.ondownload = ondownload;\n",
  1944. "}\n",
  1945. "\n",
  1946. "mpl.figure.prototype._init_header = function() {\n",
  1947. " var titlebar = $(\n",
  1948. " '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
  1949. " 'ui-helper-clearfix\"/>');\n",
  1950. " var titletext = $(\n",
  1951. " '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
  1952. " 'text-align: center; padding: 3px;\"/>');\n",
  1953. " titlebar.append(titletext)\n",
  1954. " this.root.append(titlebar);\n",
  1955. " this.header = titletext[0];\n",
  1956. "}\n",
  1957. "\n",
  1958. "\n",
  1959. "\n",
  1960. "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
  1961. "\n",
  1962. "}\n",
  1963. "\n",
  1964. "\n",
  1965. "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
  1966. "\n",
  1967. "}\n",
  1968. "\n",
  1969. "mpl.figure.prototype._init_canvas = function() {\n",
  1970. " var fig = this;\n",
  1971. "\n",
  1972. " var canvas_div = $('<div/>');\n",
  1973. "\n",
  1974. " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
  1975. "\n",
  1976. " function canvas_keyboard_event(event) {\n",
  1977. " return fig.key_event(event, event['data']);\n",
  1978. " }\n",
  1979. "\n",
  1980. " canvas_div.keydown('key_press', canvas_keyboard_event);\n",
  1981. " canvas_div.keyup('key_release', canvas_keyboard_event);\n",
  1982. " this.canvas_div = canvas_div\n",
  1983. " this._canvas_extra_style(canvas_div)\n",
  1984. " this.root.append(canvas_div);\n",
  1985. "\n",
  1986. " var canvas = $('<canvas/>');\n",
  1987. " canvas.addClass('mpl-canvas');\n",
  1988. " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
  1989. "\n",
  1990. " this.canvas = canvas[0];\n",
  1991. " this.context = canvas[0].getContext(\"2d\");\n",
  1992. "\n",
  1993. " var backingStore = this.context.backingStorePixelRatio ||\n",
  1994. "\tthis.context.webkitBackingStorePixelRatio ||\n",
  1995. "\tthis.context.mozBackingStorePixelRatio ||\n",
  1996. "\tthis.context.msBackingStorePixelRatio ||\n",
  1997. "\tthis.context.oBackingStorePixelRatio ||\n",
  1998. "\tthis.context.backingStorePixelRatio || 1;\n",
  1999. "\n",
  2000. " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
  2001. "\n",
  2002. " var rubberband = $('<canvas/>');\n",
  2003. " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
  2004. "\n",
  2005. " var pass_mouse_events = true;\n",
  2006. "\n",
  2007. " canvas_div.resizable({\n",
  2008. " start: function(event, ui) {\n",
  2009. " pass_mouse_events = false;\n",
  2010. " },\n",
  2011. " resize: function(event, ui) {\n",
  2012. " fig.request_resize(ui.size.width, ui.size.height);\n",
  2013. " },\n",
  2014. " stop: function(event, ui) {\n",
  2015. " pass_mouse_events = true;\n",
  2016. " fig.request_resize(ui.size.width, ui.size.height);\n",
  2017. " },\n",
  2018. " });\n",
  2019. "\n",
  2020. " function mouse_event_fn(event) {\n",
  2021. " if (pass_mouse_events)\n",
  2022. " return fig.mouse_event(event, event['data']);\n",
  2023. " }\n",
  2024. "\n",
  2025. " rubberband.mousedown('button_press', mouse_event_fn);\n",
  2026. " rubberband.mouseup('button_release', mouse_event_fn);\n",
  2027. " // Throttle sequential mouse events to 1 every 20ms.\n",
  2028. " rubberband.mousemove('motion_notify', mouse_event_fn);\n",
  2029. "\n",
  2030. " rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
  2031. " rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
  2032. "\n",
  2033. " canvas_div.on(\"wheel\", function (event) {\n",
  2034. " event = event.originalEvent;\n",
  2035. " event['data'] = 'scroll'\n",
  2036. " if (event.deltaY < 0) {\n",
  2037. " event.step = 1;\n",
  2038. " } else {\n",
  2039. " event.step = -1;\n",
  2040. " }\n",
  2041. " mouse_event_fn(event);\n",
  2042. " });\n",
  2043. "\n",
  2044. " canvas_div.append(canvas);\n",
  2045. " canvas_div.append(rubberband);\n",
  2046. "\n",
  2047. " this.rubberband = rubberband;\n",
  2048. " this.rubberband_canvas = rubberband[0];\n",
  2049. " this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
  2050. " this.rubberband_context.strokeStyle = \"#000000\";\n",
  2051. "\n",
  2052. " this._resize_canvas = function(width, height) {\n",
  2053. " // Keep the size of the canvas, canvas container, and rubber band\n",
  2054. " // canvas in synch.\n",
  2055. " canvas_div.css('width', width)\n",
  2056. " canvas_div.css('height', height)\n",
  2057. "\n",
  2058. " canvas.attr('width', width * mpl.ratio);\n",
  2059. " canvas.attr('height', height * mpl.ratio);\n",
  2060. " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
  2061. "\n",
  2062. " rubberband.attr('width', width);\n",
  2063. " rubberband.attr('height', height);\n",
  2064. " }\n",
  2065. "\n",
  2066. " // Set the figure to an initial 600x600px, this will subsequently be updated\n",
  2067. " // upon first draw.\n",
  2068. " this._resize_canvas(600, 600);\n",
  2069. "\n",
  2070. " // Disable right mouse context menu.\n",
  2071. " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
  2072. " return false;\n",
  2073. " });\n",
  2074. "\n",
  2075. " function set_focus () {\n",
  2076. " canvas.focus();\n",
  2077. " canvas_div.focus();\n",
  2078. " }\n",
  2079. "\n",
  2080. " window.setTimeout(set_focus, 100);\n",
  2081. "}\n",
  2082. "\n",
  2083. "mpl.figure.prototype._init_toolbar = function() {\n",
  2084. " var fig = this;\n",
  2085. "\n",
  2086. " var nav_element = $('<div/>')\n",
  2087. " nav_element.attr('style', 'width: 100%');\n",
  2088. " this.root.append(nav_element);\n",
  2089. "\n",
  2090. " // Define a callback function for later on.\n",
  2091. " function toolbar_event(event) {\n",
  2092. " return fig.toolbar_button_onclick(event['data']);\n",
  2093. " }\n",
  2094. " function toolbar_mouse_event(event) {\n",
  2095. " return fig.toolbar_button_onmouseover(event['data']);\n",
  2096. " }\n",
  2097. "\n",
  2098. " for(var toolbar_ind in mpl.toolbar_items) {\n",
  2099. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  2100. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  2101. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  2102. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  2103. "\n",
  2104. " if (!name) {\n",
  2105. " // put a spacer in here.\n",
  2106. " continue;\n",
  2107. " }\n",
  2108. " var button = $('<button/>');\n",
  2109. " button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
  2110. " 'ui-button-icon-only');\n",
  2111. " button.attr('role', 'button');\n",
  2112. " button.attr('aria-disabled', 'false');\n",
  2113. " button.click(method_name, toolbar_event);\n",
  2114. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  2115. "\n",
  2116. " var icon_img = $('<span/>');\n",
  2117. " icon_img.addClass('ui-button-icon-primary ui-icon');\n",
  2118. " icon_img.addClass(image);\n",
  2119. " icon_img.addClass('ui-corner-all');\n",
  2120. "\n",
  2121. " var tooltip_span = $('<span/>');\n",
  2122. " tooltip_span.addClass('ui-button-text');\n",
  2123. " tooltip_span.html(tooltip);\n",
  2124. "\n",
  2125. " button.append(icon_img);\n",
  2126. " button.append(tooltip_span);\n",
  2127. "\n",
  2128. " nav_element.append(button);\n",
  2129. " }\n",
  2130. "\n",
  2131. " var fmt_picker_span = $('<span/>');\n",
  2132. "\n",
  2133. " var fmt_picker = $('<select/>');\n",
  2134. " fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
  2135. " fmt_picker_span.append(fmt_picker);\n",
  2136. " nav_element.append(fmt_picker_span);\n",
  2137. " this.format_dropdown = fmt_picker[0];\n",
  2138. "\n",
  2139. " for (var ind in mpl.extensions) {\n",
  2140. " var fmt = mpl.extensions[ind];\n",
  2141. " var option = $(\n",
  2142. " '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
  2143. " fmt_picker.append(option)\n",
  2144. " }\n",
  2145. "\n",
  2146. " // Add hover states to the ui-buttons\n",
  2147. " $( \".ui-button\" ).hover(\n",
  2148. " function() { $(this).addClass(\"ui-state-hover\");},\n",
  2149. " function() { $(this).removeClass(\"ui-state-hover\");}\n",
  2150. " );\n",
  2151. "\n",
  2152. " var status_bar = $('<span class=\"mpl-message\"/>');\n",
  2153. " nav_element.append(status_bar);\n",
  2154. " this.message = status_bar[0];\n",
  2155. "}\n",
  2156. "\n",
  2157. "mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
  2158. " // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
  2159. " // which will in turn request a refresh of the image.\n",
  2160. " this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
  2161. "}\n",
  2162. "\n",
  2163. "mpl.figure.prototype.send_message = function(type, properties) {\n",
  2164. " properties['type'] = type;\n",
  2165. " properties['figure_id'] = this.id;\n",
  2166. " this.ws.send(JSON.stringify(properties));\n",
  2167. "}\n",
  2168. "\n",
  2169. "mpl.figure.prototype.send_draw_message = function() {\n",
  2170. " if (!this.waiting) {\n",
  2171. " this.waiting = true;\n",
  2172. " this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
  2173. " }\n",
  2174. "}\n",
  2175. "\n",
  2176. "\n",
  2177. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  2178. " var format_dropdown = fig.format_dropdown;\n",
  2179. " var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
  2180. " fig.ondownload(fig, format);\n",
  2181. "}\n",
  2182. "\n",
  2183. "\n",
  2184. "mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
  2185. " var size = msg['size'];\n",
  2186. " if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
  2187. " fig._resize_canvas(size[0], size[1]);\n",
  2188. " fig.send_message(\"refresh\", {});\n",
  2189. " };\n",
  2190. "}\n",
  2191. "\n",
  2192. "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
  2193. " var x0 = msg['x0'] / mpl.ratio;\n",
  2194. " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
  2195. " var x1 = msg['x1'] / mpl.ratio;\n",
  2196. " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
  2197. " x0 = Math.floor(x0) + 0.5;\n",
  2198. " y0 = Math.floor(y0) + 0.5;\n",
  2199. " x1 = Math.floor(x1) + 0.5;\n",
  2200. " y1 = Math.floor(y1) + 0.5;\n",
  2201. " var min_x = Math.min(x0, x1);\n",
  2202. " var min_y = Math.min(y0, y1);\n",
  2203. " var width = Math.abs(x1 - x0);\n",
  2204. " var height = Math.abs(y1 - y0);\n",
  2205. "\n",
  2206. " fig.rubberband_context.clearRect(\n",
  2207. " 0, 0, fig.canvas.width, fig.canvas.height);\n",
  2208. "\n",
  2209. " fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
  2210. "}\n",
  2211. "\n",
  2212. "mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
  2213. " // Updates the figure title.\n",
  2214. " fig.header.textContent = msg['label'];\n",
  2215. "}\n",
  2216. "\n",
  2217. "mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
  2218. " var cursor = msg['cursor'];\n",
  2219. " switch(cursor)\n",
  2220. " {\n",
  2221. " case 0:\n",
  2222. " cursor = 'pointer';\n",
  2223. " break;\n",
  2224. " case 1:\n",
  2225. " cursor = 'default';\n",
  2226. " break;\n",
  2227. " case 2:\n",
  2228. " cursor = 'crosshair';\n",
  2229. " break;\n",
  2230. " case 3:\n",
  2231. " cursor = 'move';\n",
  2232. " break;\n",
  2233. " }\n",
  2234. " fig.rubberband_canvas.style.cursor = cursor;\n",
  2235. "}\n",
  2236. "\n",
  2237. "mpl.figure.prototype.handle_message = function(fig, msg) {\n",
  2238. " fig.message.textContent = msg['message'];\n",
  2239. "}\n",
  2240. "\n",
  2241. "mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
  2242. " // Request the server to send over a new figure.\n",
  2243. " fig.send_draw_message();\n",
  2244. "}\n",
  2245. "\n",
  2246. "mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
  2247. " fig.image_mode = msg['mode'];\n",
  2248. "}\n",
  2249. "\n",
  2250. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  2251. " // Called whenever the canvas gets updated.\n",
  2252. " this.send_message(\"ack\", {});\n",
  2253. "}\n",
  2254. "\n",
  2255. "// A function to construct a web socket function for onmessage handling.\n",
  2256. "// Called in the figure constructor.\n",
  2257. "mpl.figure.prototype._make_on_message_function = function(fig) {\n",
  2258. " return function socket_on_message(evt) {\n",
  2259. " if (evt.data instanceof Blob) {\n",
  2260. " /* FIXME: We get \"Resource interpreted as Image but\n",
  2261. " * transferred with MIME type text/plain:\" errors on\n",
  2262. " * Chrome. But how to set the MIME type? It doesn't seem\n",
  2263. " * to be part of the websocket stream */\n",
  2264. " evt.data.type = \"image/png\";\n",
  2265. "\n",
  2266. " /* Free the memory for the previous frames */\n",
  2267. " if (fig.imageObj.src) {\n",
  2268. " (window.URL || window.webkitURL).revokeObjectURL(\n",
  2269. " fig.imageObj.src);\n",
  2270. " }\n",
  2271. "\n",
  2272. " fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
  2273. " evt.data);\n",
  2274. " fig.updated_canvas_event();\n",
  2275. " fig.waiting = false;\n",
  2276. " return;\n",
  2277. " }\n",
  2278. " else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
  2279. " fig.imageObj.src = evt.data;\n",
  2280. " fig.updated_canvas_event();\n",
  2281. " fig.waiting = false;\n",
  2282. " return;\n",
  2283. " }\n",
  2284. "\n",
  2285. " var msg = JSON.parse(evt.data);\n",
  2286. " var msg_type = msg['type'];\n",
  2287. "\n",
  2288. " // Call the \"handle_{type}\" callback, which takes\n",
  2289. " // the figure and JSON message as its only arguments.\n",
  2290. " try {\n",
  2291. " var callback = fig[\"handle_\" + msg_type];\n",
  2292. " } catch (e) {\n",
  2293. " console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
  2294. " return;\n",
  2295. " }\n",
  2296. "\n",
  2297. " if (callback) {\n",
  2298. " try {\n",
  2299. " // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
  2300. " callback(fig, msg);\n",
  2301. " } catch (e) {\n",
  2302. " console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
  2303. " }\n",
  2304. " }\n",
  2305. " };\n",
  2306. "}\n",
  2307. "\n",
  2308. "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
  2309. "mpl.findpos = function(e) {\n",
  2310. " //this section is from http://www.quirksmode.org/js/events_properties.html\n",
  2311. " var targ;\n",
  2312. " if (!e)\n",
  2313. " e = window.event;\n",
  2314. " if (e.target)\n",
  2315. " targ = e.target;\n",
  2316. " else if (e.srcElement)\n",
  2317. " targ = e.srcElement;\n",
  2318. " if (targ.nodeType == 3) // defeat Safari bug\n",
  2319. " targ = targ.parentNode;\n",
  2320. "\n",
  2321. " // jQuery normalizes the pageX and pageY\n",
  2322. " // pageX,Y are the mouse positions relative to the document\n",
  2323. " // offset() returns the position of the element relative to the document\n",
  2324. " var x = e.pageX - $(targ).offset().left;\n",
  2325. " var y = e.pageY - $(targ).offset().top;\n",
  2326. "\n",
  2327. " return {\"x\": x, \"y\": y};\n",
  2328. "};\n",
  2329. "\n",
  2330. "/*\n",
  2331. " * return a copy of an object with only non-object keys\n",
  2332. " * we need this to avoid circular references\n",
  2333. " * http://stackoverflow.com/a/24161582/3208463\n",
  2334. " */\n",
  2335. "function simpleKeys (original) {\n",
  2336. " return Object.keys(original).reduce(function (obj, key) {\n",
  2337. " if (typeof original[key] !== 'object')\n",
  2338. " obj[key] = original[key]\n",
  2339. " return obj;\n",
  2340. " }, {});\n",
  2341. "}\n",
  2342. "\n",
  2343. "mpl.figure.prototype.mouse_event = function(event, name) {\n",
  2344. " var canvas_pos = mpl.findpos(event)\n",
  2345. "\n",
  2346. " if (name === 'button_press')\n",
  2347. " {\n",
  2348. " this.canvas.focus();\n",
  2349. " this.canvas_div.focus();\n",
  2350. " }\n",
  2351. "\n",
  2352. " var x = canvas_pos.x * mpl.ratio;\n",
  2353. " var y = canvas_pos.y * mpl.ratio;\n",
  2354. "\n",
  2355. " this.send_message(name, {x: x, y: y, button: event.button,\n",
  2356. " step: event.step,\n",
  2357. " guiEvent: simpleKeys(event)});\n",
  2358. "\n",
  2359. " /* This prevents the web browser from automatically changing to\n",
  2360. " * the text insertion cursor when the button is pressed. We want\n",
  2361. " * to control all of the cursor setting manually through the\n",
  2362. " * 'cursor' event from matplotlib */\n",
  2363. " event.preventDefault();\n",
  2364. " return false;\n",
  2365. "}\n",
  2366. "\n",
  2367. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  2368. " // Handle any extra behaviour associated with a key event\n",
  2369. "}\n",
  2370. "\n",
  2371. "mpl.figure.prototype.key_event = function(event, name) {\n",
  2372. "\n",
  2373. " // Prevent repeat events\n",
  2374. " if (name == 'key_press')\n",
  2375. " {\n",
  2376. " if (event.which === this._key)\n",
  2377. " return;\n",
  2378. " else\n",
  2379. " this._key = event.which;\n",
  2380. " }\n",
  2381. " if (name == 'key_release')\n",
  2382. " this._key = null;\n",
  2383. "\n",
  2384. " var value = '';\n",
  2385. " if (event.ctrlKey && event.which != 17)\n",
  2386. " value += \"ctrl+\";\n",
  2387. " if (event.altKey && event.which != 18)\n",
  2388. " value += \"alt+\";\n",
  2389. " if (event.shiftKey && event.which != 16)\n",
  2390. " value += \"shift+\";\n",
  2391. "\n",
  2392. " value += 'k';\n",
  2393. " value += event.which.toString();\n",
  2394. "\n",
  2395. " this._key_event_extra(event, name);\n",
  2396. "\n",
  2397. " this.send_message(name, {key: value,\n",
  2398. " guiEvent: simpleKeys(event)});\n",
  2399. " return false;\n",
  2400. "}\n",
  2401. "\n",
  2402. "mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
  2403. " if (name == 'download') {\n",
  2404. " this.handle_save(this, null);\n",
  2405. " } else {\n",
  2406. " this.send_message(\"toolbar_button\", {name: name});\n",
  2407. " }\n",
  2408. "};\n",
  2409. "\n",
  2410. "mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
  2411. " this.message.textContent = tooltip;\n",
  2412. "};\n",
  2413. "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
  2414. "\n",
  2415. "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n",
  2416. "\n",
  2417. "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
  2418. " // Create a \"websocket\"-like object which calls the given IPython comm\n",
  2419. " // object with the appropriate methods. Currently this is a non binary\n",
  2420. " // socket, so there is still some room for performance tuning.\n",
  2421. " var ws = {};\n",
  2422. "\n",
  2423. " ws.close = function() {\n",
  2424. " comm.close()\n",
  2425. " };\n",
  2426. " ws.send = function(m) {\n",
  2427. " //console.log('sending', m);\n",
  2428. " comm.send(m);\n",
  2429. " };\n",
  2430. " // Register the callback with on_msg.\n",
  2431. " comm.on_msg(function(msg) {\n",
  2432. " //console.log('receiving', msg['content']['data'], msg);\n",
  2433. " // Pass the mpl event to the overridden (by mpl) onmessage function.\n",
  2434. " ws.onmessage(msg['content']['data'])\n",
  2435. " });\n",
  2436. " return ws;\n",
  2437. "}\n",
  2438. "\n",
  2439. "mpl.mpl_figure_comm = function(comm, msg) {\n",
  2440. " // This is the function which gets called when the mpl process\n",
  2441. " // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
  2442. "\n",
  2443. " var id = msg.content.data.id;\n",
  2444. " // Get hold of the div created by the display call when the Comm\n",
  2445. " // socket was opened in Python.\n",
  2446. " var element = $(\"#\" + id);\n",
  2447. " var ws_proxy = comm_websocket_adapter(comm)\n",
  2448. "\n",
  2449. " function ondownload(figure, format) {\n",
  2450. " window.open(figure.imageObj.src);\n",
  2451. " }\n",
  2452. "\n",
  2453. " var fig = new mpl.figure(id, ws_proxy,\n",
  2454. " ondownload,\n",
  2455. " element.get(0));\n",
  2456. "\n",
  2457. " // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
  2458. " // web socket which is closed, not our websocket->open comm proxy.\n",
  2459. " ws_proxy.onopen();\n",
  2460. "\n",
  2461. " fig.parent_element = element.get(0);\n",
  2462. " fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
  2463. " if (!fig.cell_info) {\n",
  2464. " console.error(\"Failed to find cell for figure\", id, fig);\n",
  2465. " return;\n",
  2466. " }\n",
  2467. "\n",
  2468. " var output_index = fig.cell_info[2]\n",
  2469. " var cell = fig.cell_info[0];\n",
  2470. "\n",
  2471. "};\n",
  2472. "\n",
  2473. "mpl.figure.prototype.handle_close = function(fig, msg) {\n",
  2474. " var width = fig.canvas.width/mpl.ratio\n",
  2475. " fig.root.unbind('remove')\n",
  2476. "\n",
  2477. " // Update the output cell to use the data from the current canvas.\n",
  2478. " fig.push_to_output();\n",
  2479. " var dataURL = fig.canvas.toDataURL();\n",
  2480. " // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
  2481. " // the notebook keyboard shortcuts fail.\n",
  2482. " IPython.keyboard_manager.enable()\n",
  2483. " $(fig.parent_element).html('<img src=\"' + dataURL + '\" width=\"' + width + '\">');\n",
  2484. " fig.close_ws(fig, msg);\n",
  2485. "}\n",
  2486. "\n",
  2487. "mpl.figure.prototype.close_ws = function(fig, msg){\n",
  2488. " fig.send_message('closing', msg);\n",
  2489. " // fig.ws.close()\n",
  2490. "}\n",
  2491. "\n",
  2492. "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
  2493. " // Turn the data on the canvas into data in the output cell.\n",
  2494. " var width = this.canvas.width/mpl.ratio\n",
  2495. " var dataURL = this.canvas.toDataURL();\n",
  2496. " this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
  2497. "}\n",
  2498. "\n",
  2499. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  2500. " // Tell IPython that the notebook contents must change.\n",
  2501. " IPython.notebook.set_dirty(true);\n",
  2502. " this.send_message(\"ack\", {});\n",
  2503. " var fig = this;\n",
  2504. " // Wait a second, then push the new image to the DOM so\n",
  2505. " // that it is saved nicely (might be nice to debounce this).\n",
  2506. " setTimeout(function () { fig.push_to_output() }, 1000);\n",
  2507. "}\n",
  2508. "\n",
  2509. "mpl.figure.prototype._init_toolbar = function() {\n",
  2510. " var fig = this;\n",
  2511. "\n",
  2512. " var nav_element = $('<div/>')\n",
  2513. " nav_element.attr('style', 'width: 100%');\n",
  2514. " this.root.append(nav_element);\n",
  2515. "\n",
  2516. " // Define a callback function for later on.\n",
  2517. " function toolbar_event(event) {\n",
  2518. " return fig.toolbar_button_onclick(event['data']);\n",
  2519. " }\n",
  2520. " function toolbar_mouse_event(event) {\n",
  2521. " return fig.toolbar_button_onmouseover(event['data']);\n",
  2522. " }\n",
  2523. "\n",
  2524. " for(var toolbar_ind in mpl.toolbar_items){\n",
  2525. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  2526. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  2527. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  2528. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  2529. "\n",
  2530. " if (!name) { continue; };\n",
  2531. "\n",
  2532. " var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
  2533. " button.click(method_name, toolbar_event);\n",
  2534. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  2535. " nav_element.append(button);\n",
  2536. " }\n",
  2537. "\n",
  2538. " // Add the status bar.\n",
  2539. " var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
  2540. " nav_element.append(status_bar);\n",
  2541. " this.message = status_bar[0];\n",
  2542. "\n",
  2543. " // Add the close button to the window.\n",
  2544. " var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
  2545. " var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
  2546. " button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
  2547. " button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
  2548. " buttongrp.append(button);\n",
  2549. " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
  2550. " titlebar.prepend(buttongrp);\n",
  2551. "}\n",
  2552. "\n",
  2553. "mpl.figure.prototype._root_extra_style = function(el){\n",
  2554. " var fig = this\n",
  2555. " el.on(\"remove\", function(){\n",
  2556. "\tfig.close_ws(fig, {});\n",
  2557. " });\n",
  2558. "}\n",
  2559. "\n",
  2560. "mpl.figure.prototype._canvas_extra_style = function(el){\n",
  2561. " // this is important to make the div 'focusable\n",
  2562. " el.attr('tabindex', 0)\n",
  2563. " // reach out to IPython and tell the keyboard manager to turn it's self\n",
  2564. " // off when our div gets focus\n",
  2565. "\n",
  2566. " // location in version 3\n",
  2567. " if (IPython.notebook.keyboard_manager) {\n",
  2568. " IPython.notebook.keyboard_manager.register_events(el);\n",
  2569. " }\n",
  2570. " else {\n",
  2571. " // location in version 2\n",
  2572. " IPython.keyboard_manager.register_events(el);\n",
  2573. " }\n",
  2574. "\n",
  2575. "}\n",
  2576. "\n",
  2577. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  2578. " var manager = IPython.notebook.keyboard_manager;\n",
  2579. " if (!manager)\n",
  2580. " manager = IPython.keyboard_manager;\n",
  2581. "\n",
  2582. " // Check for shift+enter\n",
  2583. " if (event.shiftKey && event.which == 13) {\n",
  2584. " this.canvas_div.blur();\n",
  2585. " event.shiftKey = false;\n",
  2586. " // Send a \"J\" for go to next cell\n",
  2587. " event.which = 74;\n",
  2588. " event.keyCode = 74;\n",
  2589. " manager.command_mode();\n",
  2590. " manager.handle_keydown(event);\n",
  2591. " }\n",
  2592. "}\n",
  2593. "\n",
  2594. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  2595. " fig.ondownload(fig, null);\n",
  2596. "}\n",
  2597. "\n",
  2598. "\n",
  2599. "mpl.find_output_cell = function(html_output) {\n",
  2600. " // Return the cell and output element which can be found *uniquely* in the notebook.\n",
  2601. " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
  2602. " // IPython event is triggered only after the cells have been serialised, which for\n",
  2603. " // our purposes (turning an active figure into a static one), is too late.\n",
  2604. " var cells = IPython.notebook.get_cells();\n",
  2605. " var ncells = cells.length;\n",
  2606. " for (var i=0; i<ncells; i++) {\n",
  2607. " var cell = cells[i];\n",
  2608. " if (cell.cell_type === 'code'){\n",
  2609. " for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
  2610. " var data = cell.output_area.outputs[j];\n",
  2611. " if (data.data) {\n",
  2612. " // IPython >= 3 moved mimebundle to data attribute of output\n",
  2613. " data = data.data;\n",
  2614. " }\n",
  2615. " if (data['text/html'] == html_output) {\n",
  2616. " return [cell, data, j];\n",
  2617. " }\n",
  2618. " }\n",
  2619. " }\n",
  2620. " }\n",
  2621. "}\n",
  2622. "\n",
  2623. "// Register the function which deals with the matplotlib target/channel.\n",
  2624. "// The kernel may be null if the page has been refreshed.\n",
  2625. "if (IPython.notebook.kernel != null) {\n",
  2626. " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
  2627. "}\n"
  2628. ],
  2629. "text/plain": [
  2630. "<IPython.core.display.Javascript object>"
  2631. ]
  2632. },
  2633. "metadata": {},
  2634. "output_type": "display_data"
  2635. },
  2636. {
  2637. "data": {
  2638. "text/html": [
  2639. "<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAEvklEQVR4nO3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4Mwu4AAehrHaUAAAAASUVORK5CYII=\" width=\"640\">"
  2640. ],
  2641. "text/plain": [
  2642. "<IPython.core.display.HTML object>"
  2643. ]
  2644. },
  2645. "metadata": {},
  2646. "output_type": "display_data"
  2647. },
  2648. {
  2649. "data": {
  2650. "application/javascript": [
  2651. "/* Put everything inside the global mpl namespace */\n",
  2652. "window.mpl = {};\n",
  2653. "\n",
  2654. "\n",
  2655. "mpl.get_websocket_type = function() {\n",
  2656. " if (typeof(WebSocket) !== 'undefined') {\n",
  2657. " return WebSocket;\n",
  2658. " } else if (typeof(MozWebSocket) !== 'undefined') {\n",
  2659. " return MozWebSocket;\n",
  2660. " } else {\n",
  2661. " alert('Your browser does not have WebSocket support.' +\n",
  2662. " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
  2663. " 'Firefox 4 and 5 are also supported but you ' +\n",
  2664. " 'have to enable WebSockets in about:config.');\n",
  2665. " };\n",
  2666. "}\n",
  2667. "\n",
  2668. "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
  2669. " this.id = figure_id;\n",
  2670. "\n",
  2671. " this.ws = websocket;\n",
  2672. "\n",
  2673. " this.supports_binary = (this.ws.binaryType != undefined);\n",
  2674. "\n",
  2675. " if (!this.supports_binary) {\n",
  2676. " var warnings = document.getElementById(\"mpl-warnings\");\n",
  2677. " if (warnings) {\n",
  2678. " warnings.style.display = 'block';\n",
  2679. " warnings.textContent = (\n",
  2680. " \"This browser does not support binary websocket messages. \" +\n",
  2681. " \"Performance may be slow.\");\n",
  2682. " }\n",
  2683. " }\n",
  2684. "\n",
  2685. " this.imageObj = new Image();\n",
  2686. "\n",
  2687. " this.context = undefined;\n",
  2688. " this.message = undefined;\n",
  2689. " this.canvas = undefined;\n",
  2690. " this.rubberband_canvas = undefined;\n",
  2691. " this.rubberband_context = undefined;\n",
  2692. " this.format_dropdown = undefined;\n",
  2693. "\n",
  2694. " this.image_mode = 'full';\n",
  2695. "\n",
  2696. " this.root = $('<div/>');\n",
  2697. " this._root_extra_style(this.root)\n",
  2698. " this.root.attr('style', 'display: inline-block');\n",
  2699. "\n",
  2700. " $(parent_element).append(this.root);\n",
  2701. "\n",
  2702. " this._init_header(this);\n",
  2703. " this._init_canvas(this);\n",
  2704. " this._init_toolbar(this);\n",
  2705. "\n",
  2706. " var fig = this;\n",
  2707. "\n",
  2708. " this.waiting = false;\n",
  2709. "\n",
  2710. " this.ws.onopen = function () {\n",
  2711. " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
  2712. " fig.send_message(\"send_image_mode\", {});\n",
  2713. " if (mpl.ratio != 1) {\n",
  2714. " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
  2715. " }\n",
  2716. " fig.send_message(\"refresh\", {});\n",
  2717. " }\n",
  2718. "\n",
  2719. " this.imageObj.onload = function() {\n",
  2720. " if (fig.image_mode == 'full') {\n",
  2721. " // Full images could contain transparency (where diff images\n",
  2722. " // almost always do), so we need to clear the canvas so that\n",
  2723. " // there is no ghosting.\n",
  2724. " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
  2725. " }\n",
  2726. " fig.context.drawImage(fig.imageObj, 0, 0);\n",
  2727. " };\n",
  2728. "\n",
  2729. " this.imageObj.onunload = function() {\n",
  2730. " fig.ws.close();\n",
  2731. " }\n",
  2732. "\n",
  2733. " this.ws.onmessage = this._make_on_message_function(this);\n",
  2734. "\n",
  2735. " this.ondownload = ondownload;\n",
  2736. "}\n",
  2737. "\n",
  2738. "mpl.figure.prototype._init_header = function() {\n",
  2739. " var titlebar = $(\n",
  2740. " '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
  2741. " 'ui-helper-clearfix\"/>');\n",
  2742. " var titletext = $(\n",
  2743. " '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
  2744. " 'text-align: center; padding: 3px;\"/>');\n",
  2745. " titlebar.append(titletext)\n",
  2746. " this.root.append(titlebar);\n",
  2747. " this.header = titletext[0];\n",
  2748. "}\n",
  2749. "\n",
  2750. "\n",
  2751. "\n",
  2752. "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
  2753. "\n",
  2754. "}\n",
  2755. "\n",
  2756. "\n",
  2757. "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
  2758. "\n",
  2759. "}\n",
  2760. "\n",
  2761. "mpl.figure.prototype._init_canvas = function() {\n",
  2762. " var fig = this;\n",
  2763. "\n",
  2764. " var canvas_div = $('<div/>');\n",
  2765. "\n",
  2766. " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
  2767. "\n",
  2768. " function canvas_keyboard_event(event) {\n",
  2769. " return fig.key_event(event, event['data']);\n",
  2770. " }\n",
  2771. "\n",
  2772. " canvas_div.keydown('key_press', canvas_keyboard_event);\n",
  2773. " canvas_div.keyup('key_release', canvas_keyboard_event);\n",
  2774. " this.canvas_div = canvas_div\n",
  2775. " this._canvas_extra_style(canvas_div)\n",
  2776. " this.root.append(canvas_div);\n",
  2777. "\n",
  2778. " var canvas = $('<canvas/>');\n",
  2779. " canvas.addClass('mpl-canvas');\n",
  2780. " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
  2781. "\n",
  2782. " this.canvas = canvas[0];\n",
  2783. " this.context = canvas[0].getContext(\"2d\");\n",
  2784. "\n",
  2785. " var backingStore = this.context.backingStorePixelRatio ||\n",
  2786. "\tthis.context.webkitBackingStorePixelRatio ||\n",
  2787. "\tthis.context.mozBackingStorePixelRatio ||\n",
  2788. "\tthis.context.msBackingStorePixelRatio ||\n",
  2789. "\tthis.context.oBackingStorePixelRatio ||\n",
  2790. "\tthis.context.backingStorePixelRatio || 1;\n",
  2791. "\n",
  2792. " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
  2793. "\n",
  2794. " var rubberband = $('<canvas/>');\n",
  2795. " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
  2796. "\n",
  2797. " var pass_mouse_events = true;\n",
  2798. "\n",
  2799. " canvas_div.resizable({\n",
  2800. " start: function(event, ui) {\n",
  2801. " pass_mouse_events = false;\n",
  2802. " },\n",
  2803. " resize: function(event, ui) {\n",
  2804. " fig.request_resize(ui.size.width, ui.size.height);\n",
  2805. " },\n",
  2806. " stop: function(event, ui) {\n",
  2807. " pass_mouse_events = true;\n",
  2808. " fig.request_resize(ui.size.width, ui.size.height);\n",
  2809. " },\n",
  2810. " });\n",
  2811. "\n",
  2812. " function mouse_event_fn(event) {\n",
  2813. " if (pass_mouse_events)\n",
  2814. " return fig.mouse_event(event, event['data']);\n",
  2815. " }\n",
  2816. "\n",
  2817. " rubberband.mousedown('button_press', mouse_event_fn);\n",
  2818. " rubberband.mouseup('button_release', mouse_event_fn);\n",
  2819. " // Throttle sequential mouse events to 1 every 20ms.\n",
  2820. " rubberband.mousemove('motion_notify', mouse_event_fn);\n",
  2821. "\n",
  2822. " rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
  2823. " rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
  2824. "\n",
  2825. " canvas_div.on(\"wheel\", function (event) {\n",
  2826. " event = event.originalEvent;\n",
  2827. " event['data'] = 'scroll'\n",
  2828. " if (event.deltaY < 0) {\n",
  2829. " event.step = 1;\n",
  2830. " } else {\n",
  2831. " event.step = -1;\n",
  2832. " }\n",
  2833. " mouse_event_fn(event);\n",
  2834. " });\n",
  2835. "\n",
  2836. " canvas_div.append(canvas);\n",
  2837. " canvas_div.append(rubberband);\n",
  2838. "\n",
  2839. " this.rubberband = rubberband;\n",
  2840. " this.rubberband_canvas = rubberband[0];\n",
  2841. " this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
  2842. " this.rubberband_context.strokeStyle = \"#000000\";\n",
  2843. "\n",
  2844. " this._resize_canvas = function(width, height) {\n",
  2845. " // Keep the size of the canvas, canvas container, and rubber band\n",
  2846. " // canvas in synch.\n",
  2847. " canvas_div.css('width', width)\n",
  2848. " canvas_div.css('height', height)\n",
  2849. "\n",
  2850. " canvas.attr('width', width * mpl.ratio);\n",
  2851. " canvas.attr('height', height * mpl.ratio);\n",
  2852. " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
  2853. "\n",
  2854. " rubberband.attr('width', width);\n",
  2855. " rubberband.attr('height', height);\n",
  2856. " }\n",
  2857. "\n",
  2858. " // Set the figure to an initial 600x600px, this will subsequently be updated\n",
  2859. " // upon first draw.\n",
  2860. " this._resize_canvas(600, 600);\n",
  2861. "\n",
  2862. " // Disable right mouse context menu.\n",
  2863. " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
  2864. " return false;\n",
  2865. " });\n",
  2866. "\n",
  2867. " function set_focus () {\n",
  2868. " canvas.focus();\n",
  2869. " canvas_div.focus();\n",
  2870. " }\n",
  2871. "\n",
  2872. " window.setTimeout(set_focus, 100);\n",
  2873. "}\n",
  2874. "\n",
  2875. "mpl.figure.prototype._init_toolbar = function() {\n",
  2876. " var fig = this;\n",
  2877. "\n",
  2878. " var nav_element = $('<div/>')\n",
  2879. " nav_element.attr('style', 'width: 100%');\n",
  2880. " this.root.append(nav_element);\n",
  2881. "\n",
  2882. " // Define a callback function for later on.\n",
  2883. " function toolbar_event(event) {\n",
  2884. " return fig.toolbar_button_onclick(event['data']);\n",
  2885. " }\n",
  2886. " function toolbar_mouse_event(event) {\n",
  2887. " return fig.toolbar_button_onmouseover(event['data']);\n",
  2888. " }\n",
  2889. "\n",
  2890. " for(var toolbar_ind in mpl.toolbar_items) {\n",
  2891. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  2892. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  2893. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  2894. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  2895. "\n",
  2896. " if (!name) {\n",
  2897. " // put a spacer in here.\n",
  2898. " continue;\n",
  2899. " }\n",
  2900. " var button = $('<button/>');\n",
  2901. " button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
  2902. " 'ui-button-icon-only');\n",
  2903. " button.attr('role', 'button');\n",
  2904. " button.attr('aria-disabled', 'false');\n",
  2905. " button.click(method_name, toolbar_event);\n",
  2906. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  2907. "\n",
  2908. " var icon_img = $('<span/>');\n",
  2909. " icon_img.addClass('ui-button-icon-primary ui-icon');\n",
  2910. " icon_img.addClass(image);\n",
  2911. " icon_img.addClass('ui-corner-all');\n",
  2912. "\n",
  2913. " var tooltip_span = $('<span/>');\n",
  2914. " tooltip_span.addClass('ui-button-text');\n",
  2915. " tooltip_span.html(tooltip);\n",
  2916. "\n",
  2917. " button.append(icon_img);\n",
  2918. " button.append(tooltip_span);\n",
  2919. "\n",
  2920. " nav_element.append(button);\n",
  2921. " }\n",
  2922. "\n",
  2923. " var fmt_picker_span = $('<span/>');\n",
  2924. "\n",
  2925. " var fmt_picker = $('<select/>');\n",
  2926. " fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
  2927. " fmt_picker_span.append(fmt_picker);\n",
  2928. " nav_element.append(fmt_picker_span);\n",
  2929. " this.format_dropdown = fmt_picker[0];\n",
  2930. "\n",
  2931. " for (var ind in mpl.extensions) {\n",
  2932. " var fmt = mpl.extensions[ind];\n",
  2933. " var option = $(\n",
  2934. " '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
  2935. " fmt_picker.append(option)\n",
  2936. " }\n",
  2937. "\n",
  2938. " // Add hover states to the ui-buttons\n",
  2939. " $( \".ui-button\" ).hover(\n",
  2940. " function() { $(this).addClass(\"ui-state-hover\");},\n",
  2941. " function() { $(this).removeClass(\"ui-state-hover\");}\n",
  2942. " );\n",
  2943. "\n",
  2944. " var status_bar = $('<span class=\"mpl-message\"/>');\n",
  2945. " nav_element.append(status_bar);\n",
  2946. " this.message = status_bar[0];\n",
  2947. "}\n",
  2948. "\n",
  2949. "mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
  2950. " // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
  2951. " // which will in turn request a refresh of the image.\n",
  2952. " this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
  2953. "}\n",
  2954. "\n",
  2955. "mpl.figure.prototype.send_message = function(type, properties) {\n",
  2956. " properties['type'] = type;\n",
  2957. " properties['figure_id'] = this.id;\n",
  2958. " this.ws.send(JSON.stringify(properties));\n",
  2959. "}\n",
  2960. "\n",
  2961. "mpl.figure.prototype.send_draw_message = function() {\n",
  2962. " if (!this.waiting) {\n",
  2963. " this.waiting = true;\n",
  2964. " this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
  2965. " }\n",
  2966. "}\n",
  2967. "\n",
  2968. "\n",
  2969. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  2970. " var format_dropdown = fig.format_dropdown;\n",
  2971. " var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
  2972. " fig.ondownload(fig, format);\n",
  2973. "}\n",
  2974. "\n",
  2975. "\n",
  2976. "mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
  2977. " var size = msg['size'];\n",
  2978. " if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
  2979. " fig._resize_canvas(size[0], size[1]);\n",
  2980. " fig.send_message(\"refresh\", {});\n",
  2981. " };\n",
  2982. "}\n",
  2983. "\n",
  2984. "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
  2985. " var x0 = msg['x0'] / mpl.ratio;\n",
  2986. " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
  2987. " var x1 = msg['x1'] / mpl.ratio;\n",
  2988. " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
  2989. " x0 = Math.floor(x0) + 0.5;\n",
  2990. " y0 = Math.floor(y0) + 0.5;\n",
  2991. " x1 = Math.floor(x1) + 0.5;\n",
  2992. " y1 = Math.floor(y1) + 0.5;\n",
  2993. " var min_x = Math.min(x0, x1);\n",
  2994. " var min_y = Math.min(y0, y1);\n",
  2995. " var width = Math.abs(x1 - x0);\n",
  2996. " var height = Math.abs(y1 - y0);\n",
  2997. "\n",
  2998. " fig.rubberband_context.clearRect(\n",
  2999. " 0, 0, fig.canvas.width, fig.canvas.height);\n",
  3000. "\n",
  3001. " fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
  3002. "}\n",
  3003. "\n",
  3004. "mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
  3005. " // Updates the figure title.\n",
  3006. " fig.header.textContent = msg['label'];\n",
  3007. "}\n",
  3008. "\n",
  3009. "mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
  3010. " var cursor = msg['cursor'];\n",
  3011. " switch(cursor)\n",
  3012. " {\n",
  3013. " case 0:\n",
  3014. " cursor = 'pointer';\n",
  3015. " break;\n",
  3016. " case 1:\n",
  3017. " cursor = 'default';\n",
  3018. " break;\n",
  3019. " case 2:\n",
  3020. " cursor = 'crosshair';\n",
  3021. " break;\n",
  3022. " case 3:\n",
  3023. " cursor = 'move';\n",
  3024. " break;\n",
  3025. " }\n",
  3026. " fig.rubberband_canvas.style.cursor = cursor;\n",
  3027. "}\n",
  3028. "\n",
  3029. "mpl.figure.prototype.handle_message = function(fig, msg) {\n",
  3030. " fig.message.textContent = msg['message'];\n",
  3031. "}\n",
  3032. "\n",
  3033. "mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
  3034. " // Request the server to send over a new figure.\n",
  3035. " fig.send_draw_message();\n",
  3036. "}\n",
  3037. "\n",
  3038. "mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
  3039. " fig.image_mode = msg['mode'];\n",
  3040. "}\n",
  3041. "\n",
  3042. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  3043. " // Called whenever the canvas gets updated.\n",
  3044. " this.send_message(\"ack\", {});\n",
  3045. "}\n",
  3046. "\n",
  3047. "// A function to construct a web socket function for onmessage handling.\n",
  3048. "// Called in the figure constructor.\n",
  3049. "mpl.figure.prototype._make_on_message_function = function(fig) {\n",
  3050. " return function socket_on_message(evt) {\n",
  3051. " if (evt.data instanceof Blob) {\n",
  3052. " /* FIXME: We get \"Resource interpreted as Image but\n",
  3053. " * transferred with MIME type text/plain:\" errors on\n",
  3054. " * Chrome. But how to set the MIME type? It doesn't seem\n",
  3055. " * to be part of the websocket stream */\n",
  3056. " evt.data.type = \"image/png\";\n",
  3057. "\n",
  3058. " /* Free the memory for the previous frames */\n",
  3059. " if (fig.imageObj.src) {\n",
  3060. " (window.URL || window.webkitURL).revokeObjectURL(\n",
  3061. " fig.imageObj.src);\n",
  3062. " }\n",
  3063. "\n",
  3064. " fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
  3065. " evt.data);\n",
  3066. " fig.updated_canvas_event();\n",
  3067. " fig.waiting = false;\n",
  3068. " return;\n",
  3069. " }\n",
  3070. " else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
  3071. " fig.imageObj.src = evt.data;\n",
  3072. " fig.updated_canvas_event();\n",
  3073. " fig.waiting = false;\n",
  3074. " return;\n",
  3075. " }\n",
  3076. "\n",
  3077. " var msg = JSON.parse(evt.data);\n",
  3078. " var msg_type = msg['type'];\n",
  3079. "\n",
  3080. " // Call the \"handle_{type}\" callback, which takes\n",
  3081. " // the figure and JSON message as its only arguments.\n",
  3082. " try {\n",
  3083. " var callback = fig[\"handle_\" + msg_type];\n",
  3084. " } catch (e) {\n",
  3085. " console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
  3086. " return;\n",
  3087. " }\n",
  3088. "\n",
  3089. " if (callback) {\n",
  3090. " try {\n",
  3091. " // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
  3092. " callback(fig, msg);\n",
  3093. " } catch (e) {\n",
  3094. " console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
  3095. " }\n",
  3096. " }\n",
  3097. " };\n",
  3098. "}\n",
  3099. "\n",
  3100. "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
  3101. "mpl.findpos = function(e) {\n",
  3102. " //this section is from http://www.quirksmode.org/js/events_properties.html\n",
  3103. " var targ;\n",
  3104. " if (!e)\n",
  3105. " e = window.event;\n",
  3106. " if (e.target)\n",
  3107. " targ = e.target;\n",
  3108. " else if (e.srcElement)\n",
  3109. " targ = e.srcElement;\n",
  3110. " if (targ.nodeType == 3) // defeat Safari bug\n",
  3111. " targ = targ.parentNode;\n",
  3112. "\n",
  3113. " // jQuery normalizes the pageX and pageY\n",
  3114. " // pageX,Y are the mouse positions relative to the document\n",
  3115. " // offset() returns the position of the element relative to the document\n",
  3116. " var x = e.pageX - $(targ).offset().left;\n",
  3117. " var y = e.pageY - $(targ).offset().top;\n",
  3118. "\n",
  3119. " return {\"x\": x, \"y\": y};\n",
  3120. "};\n",
  3121. "\n",
  3122. "/*\n",
  3123. " * return a copy of an object with only non-object keys\n",
  3124. " * we need this to avoid circular references\n",
  3125. " * http://stackoverflow.com/a/24161582/3208463\n",
  3126. " */\n",
  3127. "function simpleKeys (original) {\n",
  3128. " return Object.keys(original).reduce(function (obj, key) {\n",
  3129. " if (typeof original[key] !== 'object')\n",
  3130. " obj[key] = original[key]\n",
  3131. " return obj;\n",
  3132. " }, {});\n",
  3133. "}\n",
  3134. "\n",
  3135. "mpl.figure.prototype.mouse_event = function(event, name) {\n",
  3136. " var canvas_pos = mpl.findpos(event)\n",
  3137. "\n",
  3138. " if (name === 'button_press')\n",
  3139. " {\n",
  3140. " this.canvas.focus();\n",
  3141. " this.canvas_div.focus();\n",
  3142. " }\n",
  3143. "\n",
  3144. " var x = canvas_pos.x * mpl.ratio;\n",
  3145. " var y = canvas_pos.y * mpl.ratio;\n",
  3146. "\n",
  3147. " this.send_message(name, {x: x, y: y, button: event.button,\n",
  3148. " step: event.step,\n",
  3149. " guiEvent: simpleKeys(event)});\n",
  3150. "\n",
  3151. " /* This prevents the web browser from automatically changing to\n",
  3152. " * the text insertion cursor when the button is pressed. We want\n",
  3153. " * to control all of the cursor setting manually through the\n",
  3154. " * 'cursor' event from matplotlib */\n",
  3155. " event.preventDefault();\n",
  3156. " return false;\n",
  3157. "}\n",
  3158. "\n",
  3159. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  3160. " // Handle any extra behaviour associated with a key event\n",
  3161. "}\n",
  3162. "\n",
  3163. "mpl.figure.prototype.key_event = function(event, name) {\n",
  3164. "\n",
  3165. " // Prevent repeat events\n",
  3166. " if (name == 'key_press')\n",
  3167. " {\n",
  3168. " if (event.which === this._key)\n",
  3169. " return;\n",
  3170. " else\n",
  3171. " this._key = event.which;\n",
  3172. " }\n",
  3173. " if (name == 'key_release')\n",
  3174. " this._key = null;\n",
  3175. "\n",
  3176. " var value = '';\n",
  3177. " if (event.ctrlKey && event.which != 17)\n",
  3178. " value += \"ctrl+\";\n",
  3179. " if (event.altKey && event.which != 18)\n",
  3180. " value += \"alt+\";\n",
  3181. " if (event.shiftKey && event.which != 16)\n",
  3182. " value += \"shift+\";\n",
  3183. "\n",
  3184. " value += 'k';\n",
  3185. " value += event.which.toString();\n",
  3186. "\n",
  3187. " this._key_event_extra(event, name);\n",
  3188. "\n",
  3189. " this.send_message(name, {key: value,\n",
  3190. " guiEvent: simpleKeys(event)});\n",
  3191. " return false;\n",
  3192. "}\n",
  3193. "\n",
  3194. "mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
  3195. " if (name == 'download') {\n",
  3196. " this.handle_save(this, null);\n",
  3197. " } else {\n",
  3198. " this.send_message(\"toolbar_button\", {name: name});\n",
  3199. " }\n",
  3200. "};\n",
  3201. "\n",
  3202. "mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
  3203. " this.message.textContent = tooltip;\n",
  3204. "};\n",
  3205. "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
  3206. "\n",
  3207. "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n",
  3208. "\n",
  3209. "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
  3210. " // Create a \"websocket\"-like object which calls the given IPython comm\n",
  3211. " // object with the appropriate methods. Currently this is a non binary\n",
  3212. " // socket, so there is still some room for performance tuning.\n",
  3213. " var ws = {};\n",
  3214. "\n",
  3215. " ws.close = function() {\n",
  3216. " comm.close()\n",
  3217. " };\n",
  3218. " ws.send = function(m) {\n",
  3219. " //console.log('sending', m);\n",
  3220. " comm.send(m);\n",
  3221. " };\n",
  3222. " // Register the callback with on_msg.\n",
  3223. " comm.on_msg(function(msg) {\n",
  3224. " //console.log('receiving', msg['content']['data'], msg);\n",
  3225. " // Pass the mpl event to the overridden (by mpl) onmessage function.\n",
  3226. " ws.onmessage(msg['content']['data'])\n",
  3227. " });\n",
  3228. " return ws;\n",
  3229. "}\n",
  3230. "\n",
  3231. "mpl.mpl_figure_comm = function(comm, msg) {\n",
  3232. " // This is the function which gets called when the mpl process\n",
  3233. " // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
  3234. "\n",
  3235. " var id = msg.content.data.id;\n",
  3236. " // Get hold of the div created by the display call when the Comm\n",
  3237. " // socket was opened in Python.\n",
  3238. " var element = $(\"#\" + id);\n",
  3239. " var ws_proxy = comm_websocket_adapter(comm)\n",
  3240. "\n",
  3241. " function ondownload(figure, format) {\n",
  3242. " window.open(figure.imageObj.src);\n",
  3243. " }\n",
  3244. "\n",
  3245. " var fig = new mpl.figure(id, ws_proxy,\n",
  3246. " ondownload,\n",
  3247. " element.get(0));\n",
  3248. "\n",
  3249. " // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
  3250. " // web socket which is closed, not our websocket->open comm proxy.\n",
  3251. " ws_proxy.onopen();\n",
  3252. "\n",
  3253. " fig.parent_element = element.get(0);\n",
  3254. " fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
  3255. " if (!fig.cell_info) {\n",
  3256. " console.error(\"Failed to find cell for figure\", id, fig);\n",
  3257. " return;\n",
  3258. " }\n",
  3259. "\n",
  3260. " var output_index = fig.cell_info[2]\n",
  3261. " var cell = fig.cell_info[0];\n",
  3262. "\n",
  3263. "};\n",
  3264. "\n",
  3265. "mpl.figure.prototype.handle_close = function(fig, msg) {\n",
  3266. " var width = fig.canvas.width/mpl.ratio\n",
  3267. " fig.root.unbind('remove')\n",
  3268. "\n",
  3269. " // Update the output cell to use the data from the current canvas.\n",
  3270. " fig.push_to_output();\n",
  3271. " var dataURL = fig.canvas.toDataURL();\n",
  3272. " // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
  3273. " // the notebook keyboard shortcuts fail.\n",
  3274. " IPython.keyboard_manager.enable()\n",
  3275. " $(fig.parent_element).html('<img src=\"' + dataURL + '\" width=\"' + width + '\">');\n",
  3276. " fig.close_ws(fig, msg);\n",
  3277. "}\n",
  3278. "\n",
  3279. "mpl.figure.prototype.close_ws = function(fig, msg){\n",
  3280. " fig.send_message('closing', msg);\n",
  3281. " // fig.ws.close()\n",
  3282. "}\n",
  3283. "\n",
  3284. "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
  3285. " // Turn the data on the canvas into data in the output cell.\n",
  3286. " var width = this.canvas.width/mpl.ratio\n",
  3287. " var dataURL = this.canvas.toDataURL();\n",
  3288. " this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
  3289. "}\n",
  3290. "\n",
  3291. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  3292. " // Tell IPython that the notebook contents must change.\n",
  3293. " IPython.notebook.set_dirty(true);\n",
  3294. " this.send_message(\"ack\", {});\n",
  3295. " var fig = this;\n",
  3296. " // Wait a second, then push the new image to the DOM so\n",
  3297. " // that it is saved nicely (might be nice to debounce this).\n",
  3298. " setTimeout(function () { fig.push_to_output() }, 1000);\n",
  3299. "}\n",
  3300. "\n",
  3301. "mpl.figure.prototype._init_toolbar = function() {\n",
  3302. " var fig = this;\n",
  3303. "\n",
  3304. " var nav_element = $('<div/>')\n",
  3305. " nav_element.attr('style', 'width: 100%');\n",
  3306. " this.root.append(nav_element);\n",
  3307. "\n",
  3308. " // Define a callback function for later on.\n",
  3309. " function toolbar_event(event) {\n",
  3310. " return fig.toolbar_button_onclick(event['data']);\n",
  3311. " }\n",
  3312. " function toolbar_mouse_event(event) {\n",
  3313. " return fig.toolbar_button_onmouseover(event['data']);\n",
  3314. " }\n",
  3315. "\n",
  3316. " for(var toolbar_ind in mpl.toolbar_items){\n",
  3317. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  3318. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  3319. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  3320. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  3321. "\n",
  3322. " if (!name) { continue; };\n",
  3323. "\n",
  3324. " var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
  3325. " button.click(method_name, toolbar_event);\n",
  3326. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  3327. " nav_element.append(button);\n",
  3328. " }\n",
  3329. "\n",
  3330. " // Add the status bar.\n",
  3331. " var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
  3332. " nav_element.append(status_bar);\n",
  3333. " this.message = status_bar[0];\n",
  3334. "\n",
  3335. " // Add the close button to the window.\n",
  3336. " var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
  3337. " var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
  3338. " button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
  3339. " button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
  3340. " buttongrp.append(button);\n",
  3341. " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
  3342. " titlebar.prepend(buttongrp);\n",
  3343. "}\n",
  3344. "\n",
  3345. "mpl.figure.prototype._root_extra_style = function(el){\n",
  3346. " var fig = this\n",
  3347. " el.on(\"remove\", function(){\n",
  3348. "\tfig.close_ws(fig, {});\n",
  3349. " });\n",
  3350. "}\n",
  3351. "\n",
  3352. "mpl.figure.prototype._canvas_extra_style = function(el){\n",
  3353. " // this is important to make the div 'focusable\n",
  3354. " el.attr('tabindex', 0)\n",
  3355. " // reach out to IPython and tell the keyboard manager to turn it's self\n",
  3356. " // off when our div gets focus\n",
  3357. "\n",
  3358. " // location in version 3\n",
  3359. " if (IPython.notebook.keyboard_manager) {\n",
  3360. " IPython.notebook.keyboard_manager.register_events(el);\n",
  3361. " }\n",
  3362. " else {\n",
  3363. " // location in version 2\n",
  3364. " IPython.keyboard_manager.register_events(el);\n",
  3365. " }\n",
  3366. "\n",
  3367. "}\n",
  3368. "\n",
  3369. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  3370. " var manager = IPython.notebook.keyboard_manager;\n",
  3371. " if (!manager)\n",
  3372. " manager = IPython.keyboard_manager;\n",
  3373. "\n",
  3374. " // Check for shift+enter\n",
  3375. " if (event.shiftKey && event.which == 13) {\n",
  3376. " this.canvas_div.blur();\n",
  3377. " event.shiftKey = false;\n",
  3378. " // Send a \"J\" for go to next cell\n",
  3379. " event.which = 74;\n",
  3380. " event.keyCode = 74;\n",
  3381. " manager.command_mode();\n",
  3382. " manager.handle_keydown(event);\n",
  3383. " }\n",
  3384. "}\n",
  3385. "\n",
  3386. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  3387. " fig.ondownload(fig, null);\n",
  3388. "}\n",
  3389. "\n",
  3390. "\n",
  3391. "mpl.find_output_cell = function(html_output) {\n",
  3392. " // Return the cell and output element which can be found *uniquely* in the notebook.\n",
  3393. " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
  3394. " // IPython event is triggered only after the cells have been serialised, which for\n",
  3395. " // our purposes (turning an active figure into a static one), is too late.\n",
  3396. " var cells = IPython.notebook.get_cells();\n",
  3397. " var ncells = cells.length;\n",
  3398. " for (var i=0; i<ncells; i++) {\n",
  3399. " var cell = cells[i];\n",
  3400. " if (cell.cell_type === 'code'){\n",
  3401. " for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
  3402. " var data = cell.output_area.outputs[j];\n",
  3403. " if (data.data) {\n",
  3404. " // IPython >= 3 moved mimebundle to data attribute of output\n",
  3405. " data = data.data;\n",
  3406. " }\n",
  3407. " if (data['text/html'] == html_output) {\n",
  3408. " return [cell, data, j];\n",
  3409. " }\n",
  3410. " }\n",
  3411. " }\n",
  3412. " }\n",
  3413. "}\n",
  3414. "\n",
  3415. "// Register the function which deals with the matplotlib target/channel.\n",
  3416. "// The kernel may be null if the page has been refreshed.\n",
  3417. "if (IPython.notebook.kernel != null) {\n",
  3418. " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
  3419. "}\n"
  3420. ],
  3421. "text/plain": [
  3422. "<IPython.core.display.Javascript object>"
  3423. ]
  3424. },
  3425. "metadata": {},
  3426. "output_type": "display_data"
  3427. },
  3428. {
  3429. "data": {
  3430. "text/html": [
  3431. "<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAEvklEQVR4nO3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4Mwu4AAehrHaUAAAAASUVORK5CYII=\" width=\"640\">"
  3432. ],
  3433. "text/plain": [
  3434. "<IPython.core.display.HTML object>"
  3435. ]
  3436. },
  3437. "metadata": {},
  3438. "output_type": "display_data"
  3439. },
  3440. {
  3441. "data": {
  3442. "application/javascript": [
  3443. "/* Put everything inside the global mpl namespace */\n",
  3444. "window.mpl = {};\n",
  3445. "\n",
  3446. "\n",
  3447. "mpl.get_websocket_type = function() {\n",
  3448. " if (typeof(WebSocket) !== 'undefined') {\n",
  3449. " return WebSocket;\n",
  3450. " } else if (typeof(MozWebSocket) !== 'undefined') {\n",
  3451. " return MozWebSocket;\n",
  3452. " } else {\n",
  3453. " alert('Your browser does not have WebSocket support.' +\n",
  3454. " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
  3455. " 'Firefox 4 and 5 are also supported but you ' +\n",
  3456. " 'have to enable WebSockets in about:config.');\n",
  3457. " };\n",
  3458. "}\n",
  3459. "\n",
  3460. "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
  3461. " this.id = figure_id;\n",
  3462. "\n",
  3463. " this.ws = websocket;\n",
  3464. "\n",
  3465. " this.supports_binary = (this.ws.binaryType != undefined);\n",
  3466. "\n",
  3467. " if (!this.supports_binary) {\n",
  3468. " var warnings = document.getElementById(\"mpl-warnings\");\n",
  3469. " if (warnings) {\n",
  3470. " warnings.style.display = 'block';\n",
  3471. " warnings.textContent = (\n",
  3472. " \"This browser does not support binary websocket messages. \" +\n",
  3473. " \"Performance may be slow.\");\n",
  3474. " }\n",
  3475. " }\n",
  3476. "\n",
  3477. " this.imageObj = new Image();\n",
  3478. "\n",
  3479. " this.context = undefined;\n",
  3480. " this.message = undefined;\n",
  3481. " this.canvas = undefined;\n",
  3482. " this.rubberband_canvas = undefined;\n",
  3483. " this.rubberband_context = undefined;\n",
  3484. " this.format_dropdown = undefined;\n",
  3485. "\n",
  3486. " this.image_mode = 'full';\n",
  3487. "\n",
  3488. " this.root = $('<div/>');\n",
  3489. " this._root_extra_style(this.root)\n",
  3490. " this.root.attr('style', 'display: inline-block');\n",
  3491. "\n",
  3492. " $(parent_element).append(this.root);\n",
  3493. "\n",
  3494. " this._init_header(this);\n",
  3495. " this._init_canvas(this);\n",
  3496. " this._init_toolbar(this);\n",
  3497. "\n",
  3498. " var fig = this;\n",
  3499. "\n",
  3500. " this.waiting = false;\n",
  3501. "\n",
  3502. " this.ws.onopen = function () {\n",
  3503. " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
  3504. " fig.send_message(\"send_image_mode\", {});\n",
  3505. " if (mpl.ratio != 1) {\n",
  3506. " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
  3507. " }\n",
  3508. " fig.send_message(\"refresh\", {});\n",
  3509. " }\n",
  3510. "\n",
  3511. " this.imageObj.onload = function() {\n",
  3512. " if (fig.image_mode == 'full') {\n",
  3513. " // Full images could contain transparency (where diff images\n",
  3514. " // almost always do), so we need to clear the canvas so that\n",
  3515. " // there is no ghosting.\n",
  3516. " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
  3517. " }\n",
  3518. " fig.context.drawImage(fig.imageObj, 0, 0);\n",
  3519. " };\n",
  3520. "\n",
  3521. " this.imageObj.onunload = function() {\n",
  3522. " fig.ws.close();\n",
  3523. " }\n",
  3524. "\n",
  3525. " this.ws.onmessage = this._make_on_message_function(this);\n",
  3526. "\n",
  3527. " this.ondownload = ondownload;\n",
  3528. "}\n",
  3529. "\n",
  3530. "mpl.figure.prototype._init_header = function() {\n",
  3531. " var titlebar = $(\n",
  3532. " '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
  3533. " 'ui-helper-clearfix\"/>');\n",
  3534. " var titletext = $(\n",
  3535. " '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
  3536. " 'text-align: center; padding: 3px;\"/>');\n",
  3537. " titlebar.append(titletext)\n",
  3538. " this.root.append(titlebar);\n",
  3539. " this.header = titletext[0];\n",
  3540. "}\n",
  3541. "\n",
  3542. "\n",
  3543. "\n",
  3544. "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
  3545. "\n",
  3546. "}\n",
  3547. "\n",
  3548. "\n",
  3549. "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
  3550. "\n",
  3551. "}\n",
  3552. "\n",
  3553. "mpl.figure.prototype._init_canvas = function() {\n",
  3554. " var fig = this;\n",
  3555. "\n",
  3556. " var canvas_div = $('<div/>');\n",
  3557. "\n",
  3558. " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
  3559. "\n",
  3560. " function canvas_keyboard_event(event) {\n",
  3561. " return fig.key_event(event, event['data']);\n",
  3562. " }\n",
  3563. "\n",
  3564. " canvas_div.keydown('key_press', canvas_keyboard_event);\n",
  3565. " canvas_div.keyup('key_release', canvas_keyboard_event);\n",
  3566. " this.canvas_div = canvas_div\n",
  3567. " this._canvas_extra_style(canvas_div)\n",
  3568. " this.root.append(canvas_div);\n",
  3569. "\n",
  3570. " var canvas = $('<canvas/>');\n",
  3571. " canvas.addClass('mpl-canvas');\n",
  3572. " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
  3573. "\n",
  3574. " this.canvas = canvas[0];\n",
  3575. " this.context = canvas[0].getContext(\"2d\");\n",
  3576. "\n",
  3577. " var backingStore = this.context.backingStorePixelRatio ||\n",
  3578. "\tthis.context.webkitBackingStorePixelRatio ||\n",
  3579. "\tthis.context.mozBackingStorePixelRatio ||\n",
  3580. "\tthis.context.msBackingStorePixelRatio ||\n",
  3581. "\tthis.context.oBackingStorePixelRatio ||\n",
  3582. "\tthis.context.backingStorePixelRatio || 1;\n",
  3583. "\n",
  3584. " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
  3585. "\n",
  3586. " var rubberband = $('<canvas/>');\n",
  3587. " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
  3588. "\n",
  3589. " var pass_mouse_events = true;\n",
  3590. "\n",
  3591. " canvas_div.resizable({\n",
  3592. " start: function(event, ui) {\n",
  3593. " pass_mouse_events = false;\n",
  3594. " },\n",
  3595. " resize: function(event, ui) {\n",
  3596. " fig.request_resize(ui.size.width, ui.size.height);\n",
  3597. " },\n",
  3598. " stop: function(event, ui) {\n",
  3599. " pass_mouse_events = true;\n",
  3600. " fig.request_resize(ui.size.width, ui.size.height);\n",
  3601. " },\n",
  3602. " });\n",
  3603. "\n",
  3604. " function mouse_event_fn(event) {\n",
  3605. " if (pass_mouse_events)\n",
  3606. " return fig.mouse_event(event, event['data']);\n",
  3607. " }\n",
  3608. "\n",
  3609. " rubberband.mousedown('button_press', mouse_event_fn);\n",
  3610. " rubberband.mouseup('button_release', mouse_event_fn);\n",
  3611. " // Throttle sequential mouse events to 1 every 20ms.\n",
  3612. " rubberband.mousemove('motion_notify', mouse_event_fn);\n",
  3613. "\n",
  3614. " rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
  3615. " rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
  3616. "\n",
  3617. " canvas_div.on(\"wheel\", function (event) {\n",
  3618. " event = event.originalEvent;\n",
  3619. " event['data'] = 'scroll'\n",
  3620. " if (event.deltaY < 0) {\n",
  3621. " event.step = 1;\n",
  3622. " } else {\n",
  3623. " event.step = -1;\n",
  3624. " }\n",
  3625. " mouse_event_fn(event);\n",
  3626. " });\n",
  3627. "\n",
  3628. " canvas_div.append(canvas);\n",
  3629. " canvas_div.append(rubberband);\n",
  3630. "\n",
  3631. " this.rubberband = rubberband;\n",
  3632. " this.rubberband_canvas = rubberband[0];\n",
  3633. " this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
  3634. " this.rubberband_context.strokeStyle = \"#000000\";\n",
  3635. "\n",
  3636. " this._resize_canvas = function(width, height) {\n",
  3637. " // Keep the size of the canvas, canvas container, and rubber band\n",
  3638. " // canvas in synch.\n",
  3639. " canvas_div.css('width', width)\n",
  3640. " canvas_div.css('height', height)\n",
  3641. "\n",
  3642. " canvas.attr('width', width * mpl.ratio);\n",
  3643. " canvas.attr('height', height * mpl.ratio);\n",
  3644. " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
  3645. "\n",
  3646. " rubberband.attr('width', width);\n",
  3647. " rubberband.attr('height', height);\n",
  3648. " }\n",
  3649. "\n",
  3650. " // Set the figure to an initial 600x600px, this will subsequently be updated\n",
  3651. " // upon first draw.\n",
  3652. " this._resize_canvas(600, 600);\n",
  3653. "\n",
  3654. " // Disable right mouse context menu.\n",
  3655. " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
  3656. " return false;\n",
  3657. " });\n",
  3658. "\n",
  3659. " function set_focus () {\n",
  3660. " canvas.focus();\n",
  3661. " canvas_div.focus();\n",
  3662. " }\n",
  3663. "\n",
  3664. " window.setTimeout(set_focus, 100);\n",
  3665. "}\n",
  3666. "\n",
  3667. "mpl.figure.prototype._init_toolbar = function() {\n",
  3668. " var fig = this;\n",
  3669. "\n",
  3670. " var nav_element = $('<div/>')\n",
  3671. " nav_element.attr('style', 'width: 100%');\n",
  3672. " this.root.append(nav_element);\n",
  3673. "\n",
  3674. " // Define a callback function for later on.\n",
  3675. " function toolbar_event(event) {\n",
  3676. " return fig.toolbar_button_onclick(event['data']);\n",
  3677. " }\n",
  3678. " function toolbar_mouse_event(event) {\n",
  3679. " return fig.toolbar_button_onmouseover(event['data']);\n",
  3680. " }\n",
  3681. "\n",
  3682. " for(var toolbar_ind in mpl.toolbar_items) {\n",
  3683. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  3684. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  3685. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  3686. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  3687. "\n",
  3688. " if (!name) {\n",
  3689. " // put a spacer in here.\n",
  3690. " continue;\n",
  3691. " }\n",
  3692. " var button = $('<button/>');\n",
  3693. " button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
  3694. " 'ui-button-icon-only');\n",
  3695. " button.attr('role', 'button');\n",
  3696. " button.attr('aria-disabled', 'false');\n",
  3697. " button.click(method_name, toolbar_event);\n",
  3698. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  3699. "\n",
  3700. " var icon_img = $('<span/>');\n",
  3701. " icon_img.addClass('ui-button-icon-primary ui-icon');\n",
  3702. " icon_img.addClass(image);\n",
  3703. " icon_img.addClass('ui-corner-all');\n",
  3704. "\n",
  3705. " var tooltip_span = $('<span/>');\n",
  3706. " tooltip_span.addClass('ui-button-text');\n",
  3707. " tooltip_span.html(tooltip);\n",
  3708. "\n",
  3709. " button.append(icon_img);\n",
  3710. " button.append(tooltip_span);\n",
  3711. "\n",
  3712. " nav_element.append(button);\n",
  3713. " }\n",
  3714. "\n",
  3715. " var fmt_picker_span = $('<span/>');\n",
  3716. "\n",
  3717. " var fmt_picker = $('<select/>');\n",
  3718. " fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
  3719. " fmt_picker_span.append(fmt_picker);\n",
  3720. " nav_element.append(fmt_picker_span);\n",
  3721. " this.format_dropdown = fmt_picker[0];\n",
  3722. "\n",
  3723. " for (var ind in mpl.extensions) {\n",
  3724. " var fmt = mpl.extensions[ind];\n",
  3725. " var option = $(\n",
  3726. " '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
  3727. " fmt_picker.append(option)\n",
  3728. " }\n",
  3729. "\n",
  3730. " // Add hover states to the ui-buttons\n",
  3731. " $( \".ui-button\" ).hover(\n",
  3732. " function() { $(this).addClass(\"ui-state-hover\");},\n",
  3733. " function() { $(this).removeClass(\"ui-state-hover\");}\n",
  3734. " );\n",
  3735. "\n",
  3736. " var status_bar = $('<span class=\"mpl-message\"/>');\n",
  3737. " nav_element.append(status_bar);\n",
  3738. " this.message = status_bar[0];\n",
  3739. "}\n",
  3740. "\n",
  3741. "mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
  3742. " // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
  3743. " // which will in turn request a refresh of the image.\n",
  3744. " this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
  3745. "}\n",
  3746. "\n",
  3747. "mpl.figure.prototype.send_message = function(type, properties) {\n",
  3748. " properties['type'] = type;\n",
  3749. " properties['figure_id'] = this.id;\n",
  3750. " this.ws.send(JSON.stringify(properties));\n",
  3751. "}\n",
  3752. "\n",
  3753. "mpl.figure.prototype.send_draw_message = function() {\n",
  3754. " if (!this.waiting) {\n",
  3755. " this.waiting = true;\n",
  3756. " this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
  3757. " }\n",
  3758. "}\n",
  3759. "\n",
  3760. "\n",
  3761. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  3762. " var format_dropdown = fig.format_dropdown;\n",
  3763. " var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
  3764. " fig.ondownload(fig, format);\n",
  3765. "}\n",
  3766. "\n",
  3767. "\n",
  3768. "mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
  3769. " var size = msg['size'];\n",
  3770. " if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
  3771. " fig._resize_canvas(size[0], size[1]);\n",
  3772. " fig.send_message(\"refresh\", {});\n",
  3773. " };\n",
  3774. "}\n",
  3775. "\n",
  3776. "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
  3777. " var x0 = msg['x0'] / mpl.ratio;\n",
  3778. " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
  3779. " var x1 = msg['x1'] / mpl.ratio;\n",
  3780. " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
  3781. " x0 = Math.floor(x0) + 0.5;\n",
  3782. " y0 = Math.floor(y0) + 0.5;\n",
  3783. " x1 = Math.floor(x1) + 0.5;\n",
  3784. " y1 = Math.floor(y1) + 0.5;\n",
  3785. " var min_x = Math.min(x0, x1);\n",
  3786. " var min_y = Math.min(y0, y1);\n",
  3787. " var width = Math.abs(x1 - x0);\n",
  3788. " var height = Math.abs(y1 - y0);\n",
  3789. "\n",
  3790. " fig.rubberband_context.clearRect(\n",
  3791. " 0, 0, fig.canvas.width, fig.canvas.height);\n",
  3792. "\n",
  3793. " fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
  3794. "}\n",
  3795. "\n",
  3796. "mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
  3797. " // Updates the figure title.\n",
  3798. " fig.header.textContent = msg['label'];\n",
  3799. "}\n",
  3800. "\n",
  3801. "mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
  3802. " var cursor = msg['cursor'];\n",
  3803. " switch(cursor)\n",
  3804. " {\n",
  3805. " case 0:\n",
  3806. " cursor = 'pointer';\n",
  3807. " break;\n",
  3808. " case 1:\n",
  3809. " cursor = 'default';\n",
  3810. " break;\n",
  3811. " case 2:\n",
  3812. " cursor = 'crosshair';\n",
  3813. " break;\n",
  3814. " case 3:\n",
  3815. " cursor = 'move';\n",
  3816. " break;\n",
  3817. " }\n",
  3818. " fig.rubberband_canvas.style.cursor = cursor;\n",
  3819. "}\n",
  3820. "\n",
  3821. "mpl.figure.prototype.handle_message = function(fig, msg) {\n",
  3822. " fig.message.textContent = msg['message'];\n",
  3823. "}\n",
  3824. "\n",
  3825. "mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
  3826. " // Request the server to send over a new figure.\n",
  3827. " fig.send_draw_message();\n",
  3828. "}\n",
  3829. "\n",
  3830. "mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
  3831. " fig.image_mode = msg['mode'];\n",
  3832. "}\n",
  3833. "\n",
  3834. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  3835. " // Called whenever the canvas gets updated.\n",
  3836. " this.send_message(\"ack\", {});\n",
  3837. "}\n",
  3838. "\n",
  3839. "// A function to construct a web socket function for onmessage handling.\n",
  3840. "// Called in the figure constructor.\n",
  3841. "mpl.figure.prototype._make_on_message_function = function(fig) {\n",
  3842. " return function socket_on_message(evt) {\n",
  3843. " if (evt.data instanceof Blob) {\n",
  3844. " /* FIXME: We get \"Resource interpreted as Image but\n",
  3845. " * transferred with MIME type text/plain:\" errors on\n",
  3846. " * Chrome. But how to set the MIME type? It doesn't seem\n",
  3847. " * to be part of the websocket stream */\n",
  3848. " evt.data.type = \"image/png\";\n",
  3849. "\n",
  3850. " /* Free the memory for the previous frames */\n",
  3851. " if (fig.imageObj.src) {\n",
  3852. " (window.URL || window.webkitURL).revokeObjectURL(\n",
  3853. " fig.imageObj.src);\n",
  3854. " }\n",
  3855. "\n",
  3856. " fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
  3857. " evt.data);\n",
  3858. " fig.updated_canvas_event();\n",
  3859. " fig.waiting = false;\n",
  3860. " return;\n",
  3861. " }\n",
  3862. " else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
  3863. " fig.imageObj.src = evt.data;\n",
  3864. " fig.updated_canvas_event();\n",
  3865. " fig.waiting = false;\n",
  3866. " return;\n",
  3867. " }\n",
  3868. "\n",
  3869. " var msg = JSON.parse(evt.data);\n",
  3870. " var msg_type = msg['type'];\n",
  3871. "\n",
  3872. " // Call the \"handle_{type}\" callback, which takes\n",
  3873. " // the figure and JSON message as its only arguments.\n",
  3874. " try {\n",
  3875. " var callback = fig[\"handle_\" + msg_type];\n",
  3876. " } catch (e) {\n",
  3877. " console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
  3878. " return;\n",
  3879. " }\n",
  3880. "\n",
  3881. " if (callback) {\n",
  3882. " try {\n",
  3883. " // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
  3884. " callback(fig, msg);\n",
  3885. " } catch (e) {\n",
  3886. " console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
  3887. " }\n",
  3888. " }\n",
  3889. " };\n",
  3890. "}\n",
  3891. "\n",
  3892. "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
  3893. "mpl.findpos = function(e) {\n",
  3894. " //this section is from http://www.quirksmode.org/js/events_properties.html\n",
  3895. " var targ;\n",
  3896. " if (!e)\n",
  3897. " e = window.event;\n",
  3898. " if (e.target)\n",
  3899. " targ = e.target;\n",
  3900. " else if (e.srcElement)\n",
  3901. " targ = e.srcElement;\n",
  3902. " if (targ.nodeType == 3) // defeat Safari bug\n",
  3903. " targ = targ.parentNode;\n",
  3904. "\n",
  3905. " // jQuery normalizes the pageX and pageY\n",
  3906. " // pageX,Y are the mouse positions relative to the document\n",
  3907. " // offset() returns the position of the element relative to the document\n",
  3908. " var x = e.pageX - $(targ).offset().left;\n",
  3909. " var y = e.pageY - $(targ).offset().top;\n",
  3910. "\n",
  3911. " return {\"x\": x, \"y\": y};\n",
  3912. "};\n",
  3913. "\n",
  3914. "/*\n",
  3915. " * return a copy of an object with only non-object keys\n",
  3916. " * we need this to avoid circular references\n",
  3917. " * http://stackoverflow.com/a/24161582/3208463\n",
  3918. " */\n",
  3919. "function simpleKeys (original) {\n",
  3920. " return Object.keys(original).reduce(function (obj, key) {\n",
  3921. " if (typeof original[key] !== 'object')\n",
  3922. " obj[key] = original[key]\n",
  3923. " return obj;\n",
  3924. " }, {});\n",
  3925. "}\n",
  3926. "\n",
  3927. "mpl.figure.prototype.mouse_event = function(event, name) {\n",
  3928. " var canvas_pos = mpl.findpos(event)\n",
  3929. "\n",
  3930. " if (name === 'button_press')\n",
  3931. " {\n",
  3932. " this.canvas.focus();\n",
  3933. " this.canvas_div.focus();\n",
  3934. " }\n",
  3935. "\n",
  3936. " var x = canvas_pos.x * mpl.ratio;\n",
  3937. " var y = canvas_pos.y * mpl.ratio;\n",
  3938. "\n",
  3939. " this.send_message(name, {x: x, y: y, button: event.button,\n",
  3940. " step: event.step,\n",
  3941. " guiEvent: simpleKeys(event)});\n",
  3942. "\n",
  3943. " /* This prevents the web browser from automatically changing to\n",
  3944. " * the text insertion cursor when the button is pressed. We want\n",
  3945. " * to control all of the cursor setting manually through the\n",
  3946. " * 'cursor' event from matplotlib */\n",
  3947. " event.preventDefault();\n",
  3948. " return false;\n",
  3949. "}\n",
  3950. "\n",
  3951. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  3952. " // Handle any extra behaviour associated with a key event\n",
  3953. "}\n",
  3954. "\n",
  3955. "mpl.figure.prototype.key_event = function(event, name) {\n",
  3956. "\n",
  3957. " // Prevent repeat events\n",
  3958. " if (name == 'key_press')\n",
  3959. " {\n",
  3960. " if (event.which === this._key)\n",
  3961. " return;\n",
  3962. " else\n",
  3963. " this._key = event.which;\n",
  3964. " }\n",
  3965. " if (name == 'key_release')\n",
  3966. " this._key = null;\n",
  3967. "\n",
  3968. " var value = '';\n",
  3969. " if (event.ctrlKey && event.which != 17)\n",
  3970. " value += \"ctrl+\";\n",
  3971. " if (event.altKey && event.which != 18)\n",
  3972. " value += \"alt+\";\n",
  3973. " if (event.shiftKey && event.which != 16)\n",
  3974. " value += \"shift+\";\n",
  3975. "\n",
  3976. " value += 'k';\n",
  3977. " value += event.which.toString();\n",
  3978. "\n",
  3979. " this._key_event_extra(event, name);\n",
  3980. "\n",
  3981. " this.send_message(name, {key: value,\n",
  3982. " guiEvent: simpleKeys(event)});\n",
  3983. " return false;\n",
  3984. "}\n",
  3985. "\n",
  3986. "mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
  3987. " if (name == 'download') {\n",
  3988. " this.handle_save(this, null);\n",
  3989. " } else {\n",
  3990. " this.send_message(\"toolbar_button\", {name: name});\n",
  3991. " }\n",
  3992. "};\n",
  3993. "\n",
  3994. "mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
  3995. " this.message.textContent = tooltip;\n",
  3996. "};\n",
  3997. "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
  3998. "\n",
  3999. "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n",
  4000. "\n",
  4001. "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
  4002. " // Create a \"websocket\"-like object which calls the given IPython comm\n",
  4003. " // object with the appropriate methods. Currently this is a non binary\n",
  4004. " // socket, so there is still some room for performance tuning.\n",
  4005. " var ws = {};\n",
  4006. "\n",
  4007. " ws.close = function() {\n",
  4008. " comm.close()\n",
  4009. " };\n",
  4010. " ws.send = function(m) {\n",
  4011. " //console.log('sending', m);\n",
  4012. " comm.send(m);\n",
  4013. " };\n",
  4014. " // Register the callback with on_msg.\n",
  4015. " comm.on_msg(function(msg) {\n",
  4016. " //console.log('receiving', msg['content']['data'], msg);\n",
  4017. " // Pass the mpl event to the overridden (by mpl) onmessage function.\n",
  4018. " ws.onmessage(msg['content']['data'])\n",
  4019. " });\n",
  4020. " return ws;\n",
  4021. "}\n",
  4022. "\n",
  4023. "mpl.mpl_figure_comm = function(comm, msg) {\n",
  4024. " // This is the function which gets called when the mpl process\n",
  4025. " // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
  4026. "\n",
  4027. " var id = msg.content.data.id;\n",
  4028. " // Get hold of the div created by the display call when the Comm\n",
  4029. " // socket was opened in Python.\n",
  4030. " var element = $(\"#\" + id);\n",
  4031. " var ws_proxy = comm_websocket_adapter(comm)\n",
  4032. "\n",
  4033. " function ondownload(figure, format) {\n",
  4034. " window.open(figure.imageObj.src);\n",
  4035. " }\n",
  4036. "\n",
  4037. " var fig = new mpl.figure(id, ws_proxy,\n",
  4038. " ondownload,\n",
  4039. " element.get(0));\n",
  4040. "\n",
  4041. " // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
  4042. " // web socket which is closed, not our websocket->open comm proxy.\n",
  4043. " ws_proxy.onopen();\n",
  4044. "\n",
  4045. " fig.parent_element = element.get(0);\n",
  4046. " fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
  4047. " if (!fig.cell_info) {\n",
  4048. " console.error(\"Failed to find cell for figure\", id, fig);\n",
  4049. " return;\n",
  4050. " }\n",
  4051. "\n",
  4052. " var output_index = fig.cell_info[2]\n",
  4053. " var cell = fig.cell_info[0];\n",
  4054. "\n",
  4055. "};\n",
  4056. "\n",
  4057. "mpl.figure.prototype.handle_close = function(fig, msg) {\n",
  4058. " var width = fig.canvas.width/mpl.ratio\n",
  4059. " fig.root.unbind('remove')\n",
  4060. "\n",
  4061. " // Update the output cell to use the data from the current canvas.\n",
  4062. " fig.push_to_output();\n",
  4063. " var dataURL = fig.canvas.toDataURL();\n",
  4064. " // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
  4065. " // the notebook keyboard shortcuts fail.\n",
  4066. " IPython.keyboard_manager.enable()\n",
  4067. " $(fig.parent_element).html('<img src=\"' + dataURL + '\" width=\"' + width + '\">');\n",
  4068. " fig.close_ws(fig, msg);\n",
  4069. "}\n",
  4070. "\n",
  4071. "mpl.figure.prototype.close_ws = function(fig, msg){\n",
  4072. " fig.send_message('closing', msg);\n",
  4073. " // fig.ws.close()\n",
  4074. "}\n",
  4075. "\n",
  4076. "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
  4077. " // Turn the data on the canvas into data in the output cell.\n",
  4078. " var width = this.canvas.width/mpl.ratio\n",
  4079. " var dataURL = this.canvas.toDataURL();\n",
  4080. " this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
  4081. "}\n",
  4082. "\n",
  4083. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  4084. " // Tell IPython that the notebook contents must change.\n",
  4085. " IPython.notebook.set_dirty(true);\n",
  4086. " this.send_message(\"ack\", {});\n",
  4087. " var fig = this;\n",
  4088. " // Wait a second, then push the new image to the DOM so\n",
  4089. " // that it is saved nicely (might be nice to debounce this).\n",
  4090. " setTimeout(function () { fig.push_to_output() }, 1000);\n",
  4091. "}\n",
  4092. "\n",
  4093. "mpl.figure.prototype._init_toolbar = function() {\n",
  4094. " var fig = this;\n",
  4095. "\n",
  4096. " var nav_element = $('<div/>')\n",
  4097. " nav_element.attr('style', 'width: 100%');\n",
  4098. " this.root.append(nav_element);\n",
  4099. "\n",
  4100. " // Define a callback function for later on.\n",
  4101. " function toolbar_event(event) {\n",
  4102. " return fig.toolbar_button_onclick(event['data']);\n",
  4103. " }\n",
  4104. " function toolbar_mouse_event(event) {\n",
  4105. " return fig.toolbar_button_onmouseover(event['data']);\n",
  4106. " }\n",
  4107. "\n",
  4108. " for(var toolbar_ind in mpl.toolbar_items){\n",
  4109. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  4110. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  4111. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  4112. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  4113. "\n",
  4114. " if (!name) { continue; };\n",
  4115. "\n",
  4116. " var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
  4117. " button.click(method_name, toolbar_event);\n",
  4118. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  4119. " nav_element.append(button);\n",
  4120. " }\n",
  4121. "\n",
  4122. " // Add the status bar.\n",
  4123. " var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
  4124. " nav_element.append(status_bar);\n",
  4125. " this.message = status_bar[0];\n",
  4126. "\n",
  4127. " // Add the close button to the window.\n",
  4128. " var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
  4129. " var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
  4130. " button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
  4131. " button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
  4132. " buttongrp.append(button);\n",
  4133. " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
  4134. " titlebar.prepend(buttongrp);\n",
  4135. "}\n",
  4136. "\n",
  4137. "mpl.figure.prototype._root_extra_style = function(el){\n",
  4138. " var fig = this\n",
  4139. " el.on(\"remove\", function(){\n",
  4140. "\tfig.close_ws(fig, {});\n",
  4141. " });\n",
  4142. "}\n",
  4143. "\n",
  4144. "mpl.figure.prototype._canvas_extra_style = function(el){\n",
  4145. " // this is important to make the div 'focusable\n",
  4146. " el.attr('tabindex', 0)\n",
  4147. " // reach out to IPython and tell the keyboard manager to turn it's self\n",
  4148. " // off when our div gets focus\n",
  4149. "\n",
  4150. " // location in version 3\n",
  4151. " if (IPython.notebook.keyboard_manager) {\n",
  4152. " IPython.notebook.keyboard_manager.register_events(el);\n",
  4153. " }\n",
  4154. " else {\n",
  4155. " // location in version 2\n",
  4156. " IPython.keyboard_manager.register_events(el);\n",
  4157. " }\n",
  4158. "\n",
  4159. "}\n",
  4160. "\n",
  4161. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  4162. " var manager = IPython.notebook.keyboard_manager;\n",
  4163. " if (!manager)\n",
  4164. " manager = IPython.keyboard_manager;\n",
  4165. "\n",
  4166. " // Check for shift+enter\n",
  4167. " if (event.shiftKey && event.which == 13) {\n",
  4168. " this.canvas_div.blur();\n",
  4169. " event.shiftKey = false;\n",
  4170. " // Send a \"J\" for go to next cell\n",
  4171. " event.which = 74;\n",
  4172. " event.keyCode = 74;\n",
  4173. " manager.command_mode();\n",
  4174. " manager.handle_keydown(event);\n",
  4175. " }\n",
  4176. "}\n",
  4177. "\n",
  4178. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  4179. " fig.ondownload(fig, null);\n",
  4180. "}\n",
  4181. "\n",
  4182. "\n",
  4183. "mpl.find_output_cell = function(html_output) {\n",
  4184. " // Return the cell and output element which can be found *uniquely* in the notebook.\n",
  4185. " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
  4186. " // IPython event is triggered only after the cells have been serialised, which for\n",
  4187. " // our purposes (turning an active figure into a static one), is too late.\n",
  4188. " var cells = IPython.notebook.get_cells();\n",
  4189. " var ncells = cells.length;\n",
  4190. " for (var i=0; i<ncells; i++) {\n",
  4191. " var cell = cells[i];\n",
  4192. " if (cell.cell_type === 'code'){\n",
  4193. " for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
  4194. " var data = cell.output_area.outputs[j];\n",
  4195. " if (data.data) {\n",
  4196. " // IPython >= 3 moved mimebundle to data attribute of output\n",
  4197. " data = data.data;\n",
  4198. " }\n",
  4199. " if (data['text/html'] == html_output) {\n",
  4200. " return [cell, data, j];\n",
  4201. " }\n",
  4202. " }\n",
  4203. " }\n",
  4204. " }\n",
  4205. "}\n",
  4206. "\n",
  4207. "// Register the function which deals with the matplotlib target/channel.\n",
  4208. "// The kernel may be null if the page has been refreshed.\n",
  4209. "if (IPython.notebook.kernel != null) {\n",
  4210. " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
  4211. "}\n"
  4212. ],
  4213. "text/plain": [
  4214. "<IPython.core.display.Javascript object>"
  4215. ]
  4216. },
  4217. "metadata": {},
  4218. "output_type": "display_data"
  4219. },
  4220. {
  4221. "data": {
  4222. "text/html": [
  4223. "<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAEvklEQVR4nO3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4Mwu4AAehrHaUAAAAASUVORK5CYII=\" width=\"640\">"
  4224. ],
  4225. "text/plain": [
  4226. "<IPython.core.display.HTML object>"
  4227. ]
  4228. },
  4229. "metadata": {},
  4230. "output_type": "display_data"
  4231. },
  4232. {
  4233. "data": {
  4234. "application/javascript": [
  4235. "/* Put everything inside the global mpl namespace */\n",
  4236. "window.mpl = {};\n",
  4237. "\n",
  4238. "\n",
  4239. "mpl.get_websocket_type = function() {\n",
  4240. " if (typeof(WebSocket) !== 'undefined') {\n",
  4241. " return WebSocket;\n",
  4242. " } else if (typeof(MozWebSocket) !== 'undefined') {\n",
  4243. " return MozWebSocket;\n",
  4244. " } else {\n",
  4245. " alert('Your browser does not have WebSocket support.' +\n",
  4246. " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
  4247. " 'Firefox 4 and 5 are also supported but you ' +\n",
  4248. " 'have to enable WebSockets in about:config.');\n",
  4249. " };\n",
  4250. "}\n",
  4251. "\n",
  4252. "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
  4253. " this.id = figure_id;\n",
  4254. "\n",
  4255. " this.ws = websocket;\n",
  4256. "\n",
  4257. " this.supports_binary = (this.ws.binaryType != undefined);\n",
  4258. "\n",
  4259. " if (!this.supports_binary) {\n",
  4260. " var warnings = document.getElementById(\"mpl-warnings\");\n",
  4261. " if (warnings) {\n",
  4262. " warnings.style.display = 'block';\n",
  4263. " warnings.textContent = (\n",
  4264. " \"This browser does not support binary websocket messages. \" +\n",
  4265. " \"Performance may be slow.\");\n",
  4266. " }\n",
  4267. " }\n",
  4268. "\n",
  4269. " this.imageObj = new Image();\n",
  4270. "\n",
  4271. " this.context = undefined;\n",
  4272. " this.message = undefined;\n",
  4273. " this.canvas = undefined;\n",
  4274. " this.rubberband_canvas = undefined;\n",
  4275. " this.rubberband_context = undefined;\n",
  4276. " this.format_dropdown = undefined;\n",
  4277. "\n",
  4278. " this.image_mode = 'full';\n",
  4279. "\n",
  4280. " this.root = $('<div/>');\n",
  4281. " this._root_extra_style(this.root)\n",
  4282. " this.root.attr('style', 'display: inline-block');\n",
  4283. "\n",
  4284. " $(parent_element).append(this.root);\n",
  4285. "\n",
  4286. " this._init_header(this);\n",
  4287. " this._init_canvas(this);\n",
  4288. " this._init_toolbar(this);\n",
  4289. "\n",
  4290. " var fig = this;\n",
  4291. "\n",
  4292. " this.waiting = false;\n",
  4293. "\n",
  4294. " this.ws.onopen = function () {\n",
  4295. " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
  4296. " fig.send_message(\"send_image_mode\", {});\n",
  4297. " if (mpl.ratio != 1) {\n",
  4298. " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
  4299. " }\n",
  4300. " fig.send_message(\"refresh\", {});\n",
  4301. " }\n",
  4302. "\n",
  4303. " this.imageObj.onload = function() {\n",
  4304. " if (fig.image_mode == 'full') {\n",
  4305. " // Full images could contain transparency (where diff images\n",
  4306. " // almost always do), so we need to clear the canvas so that\n",
  4307. " // there is no ghosting.\n",
  4308. " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
  4309. " }\n",
  4310. " fig.context.drawImage(fig.imageObj, 0, 0);\n",
  4311. " };\n",
  4312. "\n",
  4313. " this.imageObj.onunload = function() {\n",
  4314. " fig.ws.close();\n",
  4315. " }\n",
  4316. "\n",
  4317. " this.ws.onmessage = this._make_on_message_function(this);\n",
  4318. "\n",
  4319. " this.ondownload = ondownload;\n",
  4320. "}\n",
  4321. "\n",
  4322. "mpl.figure.prototype._init_header = function() {\n",
  4323. " var titlebar = $(\n",
  4324. " '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
  4325. " 'ui-helper-clearfix\"/>');\n",
  4326. " var titletext = $(\n",
  4327. " '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
  4328. " 'text-align: center; padding: 3px;\"/>');\n",
  4329. " titlebar.append(titletext)\n",
  4330. " this.root.append(titlebar);\n",
  4331. " this.header = titletext[0];\n",
  4332. "}\n",
  4333. "\n",
  4334. "\n",
  4335. "\n",
  4336. "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
  4337. "\n",
  4338. "}\n",
  4339. "\n",
  4340. "\n",
  4341. "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
  4342. "\n",
  4343. "}\n",
  4344. "\n",
  4345. "mpl.figure.prototype._init_canvas = function() {\n",
  4346. " var fig = this;\n",
  4347. "\n",
  4348. " var canvas_div = $('<div/>');\n",
  4349. "\n",
  4350. " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
  4351. "\n",
  4352. " function canvas_keyboard_event(event) {\n",
  4353. " return fig.key_event(event, event['data']);\n",
  4354. " }\n",
  4355. "\n",
  4356. " canvas_div.keydown('key_press', canvas_keyboard_event);\n",
  4357. " canvas_div.keyup('key_release', canvas_keyboard_event);\n",
  4358. " this.canvas_div = canvas_div\n",
  4359. " this._canvas_extra_style(canvas_div)\n",
  4360. " this.root.append(canvas_div);\n",
  4361. "\n",
  4362. " var canvas = $('<canvas/>');\n",
  4363. " canvas.addClass('mpl-canvas');\n",
  4364. " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
  4365. "\n",
  4366. " this.canvas = canvas[0];\n",
  4367. " this.context = canvas[0].getContext(\"2d\");\n",
  4368. "\n",
  4369. " var backingStore = this.context.backingStorePixelRatio ||\n",
  4370. "\tthis.context.webkitBackingStorePixelRatio ||\n",
  4371. "\tthis.context.mozBackingStorePixelRatio ||\n",
  4372. "\tthis.context.msBackingStorePixelRatio ||\n",
  4373. "\tthis.context.oBackingStorePixelRatio ||\n",
  4374. "\tthis.context.backingStorePixelRatio || 1;\n",
  4375. "\n",
  4376. " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
  4377. "\n",
  4378. " var rubberband = $('<canvas/>');\n",
  4379. " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
  4380. "\n",
  4381. " var pass_mouse_events = true;\n",
  4382. "\n",
  4383. " canvas_div.resizable({\n",
  4384. " start: function(event, ui) {\n",
  4385. " pass_mouse_events = false;\n",
  4386. " },\n",
  4387. " resize: function(event, ui) {\n",
  4388. " fig.request_resize(ui.size.width, ui.size.height);\n",
  4389. " },\n",
  4390. " stop: function(event, ui) {\n",
  4391. " pass_mouse_events = true;\n",
  4392. " fig.request_resize(ui.size.width, ui.size.height);\n",
  4393. " },\n",
  4394. " });\n",
  4395. "\n",
  4396. " function mouse_event_fn(event) {\n",
  4397. " if (pass_mouse_events)\n",
  4398. " return fig.mouse_event(event, event['data']);\n",
  4399. " }\n",
  4400. "\n",
  4401. " rubberband.mousedown('button_press', mouse_event_fn);\n",
  4402. " rubberband.mouseup('button_release', mouse_event_fn);\n",
  4403. " // Throttle sequential mouse events to 1 every 20ms.\n",
  4404. " rubberband.mousemove('motion_notify', mouse_event_fn);\n",
  4405. "\n",
  4406. " rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
  4407. " rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
  4408. "\n",
  4409. " canvas_div.on(\"wheel\", function (event) {\n",
  4410. " event = event.originalEvent;\n",
  4411. " event['data'] = 'scroll'\n",
  4412. " if (event.deltaY < 0) {\n",
  4413. " event.step = 1;\n",
  4414. " } else {\n",
  4415. " event.step = -1;\n",
  4416. " }\n",
  4417. " mouse_event_fn(event);\n",
  4418. " });\n",
  4419. "\n",
  4420. " canvas_div.append(canvas);\n",
  4421. " canvas_div.append(rubberband);\n",
  4422. "\n",
  4423. " this.rubberband = rubberband;\n",
  4424. " this.rubberband_canvas = rubberband[0];\n",
  4425. " this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
  4426. " this.rubberband_context.strokeStyle = \"#000000\";\n",
  4427. "\n",
  4428. " this._resize_canvas = function(width, height) {\n",
  4429. " // Keep the size of the canvas, canvas container, and rubber band\n",
  4430. " // canvas in synch.\n",
  4431. " canvas_div.css('width', width)\n",
  4432. " canvas_div.css('height', height)\n",
  4433. "\n",
  4434. " canvas.attr('width', width * mpl.ratio);\n",
  4435. " canvas.attr('height', height * mpl.ratio);\n",
  4436. " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
  4437. "\n",
  4438. " rubberband.attr('width', width);\n",
  4439. " rubberband.attr('height', height);\n",
  4440. " }\n",
  4441. "\n",
  4442. " // Set the figure to an initial 600x600px, this will subsequently be updated\n",
  4443. " // upon first draw.\n",
  4444. " this._resize_canvas(600, 600);\n",
  4445. "\n",
  4446. " // Disable right mouse context menu.\n",
  4447. " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
  4448. " return false;\n",
  4449. " });\n",
  4450. "\n",
  4451. " function set_focus () {\n",
  4452. " canvas.focus();\n",
  4453. " canvas_div.focus();\n",
  4454. " }\n",
  4455. "\n",
  4456. " window.setTimeout(set_focus, 100);\n",
  4457. "}\n",
  4458. "\n",
  4459. "mpl.figure.prototype._init_toolbar = function() {\n",
  4460. " var fig = this;\n",
  4461. "\n",
  4462. " var nav_element = $('<div/>')\n",
  4463. " nav_element.attr('style', 'width: 100%');\n",
  4464. " this.root.append(nav_element);\n",
  4465. "\n",
  4466. " // Define a callback function for later on.\n",
  4467. " function toolbar_event(event) {\n",
  4468. " return fig.toolbar_button_onclick(event['data']);\n",
  4469. " }\n",
  4470. " function toolbar_mouse_event(event) {\n",
  4471. " return fig.toolbar_button_onmouseover(event['data']);\n",
  4472. " }\n",
  4473. "\n",
  4474. " for(var toolbar_ind in mpl.toolbar_items) {\n",
  4475. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  4476. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  4477. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  4478. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  4479. "\n",
  4480. " if (!name) {\n",
  4481. " // put a spacer in here.\n",
  4482. " continue;\n",
  4483. " }\n",
  4484. " var button = $('<button/>');\n",
  4485. " button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
  4486. " 'ui-button-icon-only');\n",
  4487. " button.attr('role', 'button');\n",
  4488. " button.attr('aria-disabled', 'false');\n",
  4489. " button.click(method_name, toolbar_event);\n",
  4490. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  4491. "\n",
  4492. " var icon_img = $('<span/>');\n",
  4493. " icon_img.addClass('ui-button-icon-primary ui-icon');\n",
  4494. " icon_img.addClass(image);\n",
  4495. " icon_img.addClass('ui-corner-all');\n",
  4496. "\n",
  4497. " var tooltip_span = $('<span/>');\n",
  4498. " tooltip_span.addClass('ui-button-text');\n",
  4499. " tooltip_span.html(tooltip);\n",
  4500. "\n",
  4501. " button.append(icon_img);\n",
  4502. " button.append(tooltip_span);\n",
  4503. "\n",
  4504. " nav_element.append(button);\n",
  4505. " }\n",
  4506. "\n",
  4507. " var fmt_picker_span = $('<span/>');\n",
  4508. "\n",
  4509. " var fmt_picker = $('<select/>');\n",
  4510. " fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
  4511. " fmt_picker_span.append(fmt_picker);\n",
  4512. " nav_element.append(fmt_picker_span);\n",
  4513. " this.format_dropdown = fmt_picker[0];\n",
  4514. "\n",
  4515. " for (var ind in mpl.extensions) {\n",
  4516. " var fmt = mpl.extensions[ind];\n",
  4517. " var option = $(\n",
  4518. " '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
  4519. " fmt_picker.append(option)\n",
  4520. " }\n",
  4521. "\n",
  4522. " // Add hover states to the ui-buttons\n",
  4523. " $( \".ui-button\" ).hover(\n",
  4524. " function() { $(this).addClass(\"ui-state-hover\");},\n",
  4525. " function() { $(this).removeClass(\"ui-state-hover\");}\n",
  4526. " );\n",
  4527. "\n",
  4528. " var status_bar = $('<span class=\"mpl-message\"/>');\n",
  4529. " nav_element.append(status_bar);\n",
  4530. " this.message = status_bar[0];\n",
  4531. "}\n",
  4532. "\n",
  4533. "mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
  4534. " // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
  4535. " // which will in turn request a refresh of the image.\n",
  4536. " this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
  4537. "}\n",
  4538. "\n",
  4539. "mpl.figure.prototype.send_message = function(type, properties) {\n",
  4540. " properties['type'] = type;\n",
  4541. " properties['figure_id'] = this.id;\n",
  4542. " this.ws.send(JSON.stringify(properties));\n",
  4543. "}\n",
  4544. "\n",
  4545. "mpl.figure.prototype.send_draw_message = function() {\n",
  4546. " if (!this.waiting) {\n",
  4547. " this.waiting = true;\n",
  4548. " this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
  4549. " }\n",
  4550. "}\n",
  4551. "\n",
  4552. "\n",
  4553. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  4554. " var format_dropdown = fig.format_dropdown;\n",
  4555. " var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
  4556. " fig.ondownload(fig, format);\n",
  4557. "}\n",
  4558. "\n",
  4559. "\n",
  4560. "mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
  4561. " var size = msg['size'];\n",
  4562. " if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
  4563. " fig._resize_canvas(size[0], size[1]);\n",
  4564. " fig.send_message(\"refresh\", {});\n",
  4565. " };\n",
  4566. "}\n",
  4567. "\n",
  4568. "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
  4569. " var x0 = msg['x0'] / mpl.ratio;\n",
  4570. " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
  4571. " var x1 = msg['x1'] / mpl.ratio;\n",
  4572. " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
  4573. " x0 = Math.floor(x0) + 0.5;\n",
  4574. " y0 = Math.floor(y0) + 0.5;\n",
  4575. " x1 = Math.floor(x1) + 0.5;\n",
  4576. " y1 = Math.floor(y1) + 0.5;\n",
  4577. " var min_x = Math.min(x0, x1);\n",
  4578. " var min_y = Math.min(y0, y1);\n",
  4579. " var width = Math.abs(x1 - x0);\n",
  4580. " var height = Math.abs(y1 - y0);\n",
  4581. "\n",
  4582. " fig.rubberband_context.clearRect(\n",
  4583. " 0, 0, fig.canvas.width, fig.canvas.height);\n",
  4584. "\n",
  4585. " fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
  4586. "}\n",
  4587. "\n",
  4588. "mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
  4589. " // Updates the figure title.\n",
  4590. " fig.header.textContent = msg['label'];\n",
  4591. "}\n",
  4592. "\n",
  4593. "mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
  4594. " var cursor = msg['cursor'];\n",
  4595. " switch(cursor)\n",
  4596. " {\n",
  4597. " case 0:\n",
  4598. " cursor = 'pointer';\n",
  4599. " break;\n",
  4600. " case 1:\n",
  4601. " cursor = 'default';\n",
  4602. " break;\n",
  4603. " case 2:\n",
  4604. " cursor = 'crosshair';\n",
  4605. " break;\n",
  4606. " case 3:\n",
  4607. " cursor = 'move';\n",
  4608. " break;\n",
  4609. " }\n",
  4610. " fig.rubberband_canvas.style.cursor = cursor;\n",
  4611. "}\n",
  4612. "\n",
  4613. "mpl.figure.prototype.handle_message = function(fig, msg) {\n",
  4614. " fig.message.textContent = msg['message'];\n",
  4615. "}\n",
  4616. "\n",
  4617. "mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
  4618. " // Request the server to send over a new figure.\n",
  4619. " fig.send_draw_message();\n",
  4620. "}\n",
  4621. "\n",
  4622. "mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
  4623. " fig.image_mode = msg['mode'];\n",
  4624. "}\n",
  4625. "\n",
  4626. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  4627. " // Called whenever the canvas gets updated.\n",
  4628. " this.send_message(\"ack\", {});\n",
  4629. "}\n",
  4630. "\n",
  4631. "// A function to construct a web socket function for onmessage handling.\n",
  4632. "// Called in the figure constructor.\n",
  4633. "mpl.figure.prototype._make_on_message_function = function(fig) {\n",
  4634. " return function socket_on_message(evt) {\n",
  4635. " if (evt.data instanceof Blob) {\n",
  4636. " /* FIXME: We get \"Resource interpreted as Image but\n",
  4637. " * transferred with MIME type text/plain:\" errors on\n",
  4638. " * Chrome. But how to set the MIME type? It doesn't seem\n",
  4639. " * to be part of the websocket stream */\n",
  4640. " evt.data.type = \"image/png\";\n",
  4641. "\n",
  4642. " /* Free the memory for the previous frames */\n",
  4643. " if (fig.imageObj.src) {\n",
  4644. " (window.URL || window.webkitURL).revokeObjectURL(\n",
  4645. " fig.imageObj.src);\n",
  4646. " }\n",
  4647. "\n",
  4648. " fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
  4649. " evt.data);\n",
  4650. " fig.updated_canvas_event();\n",
  4651. " fig.waiting = false;\n",
  4652. " return;\n",
  4653. " }\n",
  4654. " else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
  4655. " fig.imageObj.src = evt.data;\n",
  4656. " fig.updated_canvas_event();\n",
  4657. " fig.waiting = false;\n",
  4658. " return;\n",
  4659. " }\n",
  4660. "\n",
  4661. " var msg = JSON.parse(evt.data);\n",
  4662. " var msg_type = msg['type'];\n",
  4663. "\n",
  4664. " // Call the \"handle_{type}\" callback, which takes\n",
  4665. " // the figure and JSON message as its only arguments.\n",
  4666. " try {\n",
  4667. " var callback = fig[\"handle_\" + msg_type];\n",
  4668. " } catch (e) {\n",
  4669. " console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
  4670. " return;\n",
  4671. " }\n",
  4672. "\n",
  4673. " if (callback) {\n",
  4674. " try {\n",
  4675. " // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
  4676. " callback(fig, msg);\n",
  4677. " } catch (e) {\n",
  4678. " console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
  4679. " }\n",
  4680. " }\n",
  4681. " };\n",
  4682. "}\n",
  4683. "\n",
  4684. "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
  4685. "mpl.findpos = function(e) {\n",
  4686. " //this section is from http://www.quirksmode.org/js/events_properties.html\n",
  4687. " var targ;\n",
  4688. " if (!e)\n",
  4689. " e = window.event;\n",
  4690. " if (e.target)\n",
  4691. " targ = e.target;\n",
  4692. " else if (e.srcElement)\n",
  4693. " targ = e.srcElement;\n",
  4694. " if (targ.nodeType == 3) // defeat Safari bug\n",
  4695. " targ = targ.parentNode;\n",
  4696. "\n",
  4697. " // jQuery normalizes the pageX and pageY\n",
  4698. " // pageX,Y are the mouse positions relative to the document\n",
  4699. " // offset() returns the position of the element relative to the document\n",
  4700. " var x = e.pageX - $(targ).offset().left;\n",
  4701. " var y = e.pageY - $(targ).offset().top;\n",
  4702. "\n",
  4703. " return {\"x\": x, \"y\": y};\n",
  4704. "};\n",
  4705. "\n",
  4706. "/*\n",
  4707. " * return a copy of an object with only non-object keys\n",
  4708. " * we need this to avoid circular references\n",
  4709. " * http://stackoverflow.com/a/24161582/3208463\n",
  4710. " */\n",
  4711. "function simpleKeys (original) {\n",
  4712. " return Object.keys(original).reduce(function (obj, key) {\n",
  4713. " if (typeof original[key] !== 'object')\n",
  4714. " obj[key] = original[key]\n",
  4715. " return obj;\n",
  4716. " }, {});\n",
  4717. "}\n",
  4718. "\n",
  4719. "mpl.figure.prototype.mouse_event = function(event, name) {\n",
  4720. " var canvas_pos = mpl.findpos(event)\n",
  4721. "\n",
  4722. " if (name === 'button_press')\n",
  4723. " {\n",
  4724. " this.canvas.focus();\n",
  4725. " this.canvas_div.focus();\n",
  4726. " }\n",
  4727. "\n",
  4728. " var x = canvas_pos.x * mpl.ratio;\n",
  4729. " var y = canvas_pos.y * mpl.ratio;\n",
  4730. "\n",
  4731. " this.send_message(name, {x: x, y: y, button: event.button,\n",
  4732. " step: event.step,\n",
  4733. " guiEvent: simpleKeys(event)});\n",
  4734. "\n",
  4735. " /* This prevents the web browser from automatically changing to\n",
  4736. " * the text insertion cursor when the button is pressed. We want\n",
  4737. " * to control all of the cursor setting manually through the\n",
  4738. " * 'cursor' event from matplotlib */\n",
  4739. " event.preventDefault();\n",
  4740. " return false;\n",
  4741. "}\n",
  4742. "\n",
  4743. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  4744. " // Handle any extra behaviour associated with a key event\n",
  4745. "}\n",
  4746. "\n",
  4747. "mpl.figure.prototype.key_event = function(event, name) {\n",
  4748. "\n",
  4749. " // Prevent repeat events\n",
  4750. " if (name == 'key_press')\n",
  4751. " {\n",
  4752. " if (event.which === this._key)\n",
  4753. " return;\n",
  4754. " else\n",
  4755. " this._key = event.which;\n",
  4756. " }\n",
  4757. " if (name == 'key_release')\n",
  4758. " this._key = null;\n",
  4759. "\n",
  4760. " var value = '';\n",
  4761. " if (event.ctrlKey && event.which != 17)\n",
  4762. " value += \"ctrl+\";\n",
  4763. " if (event.altKey && event.which != 18)\n",
  4764. " value += \"alt+\";\n",
  4765. " if (event.shiftKey && event.which != 16)\n",
  4766. " value += \"shift+\";\n",
  4767. "\n",
  4768. " value += 'k';\n",
  4769. " value += event.which.toString();\n",
  4770. "\n",
  4771. " this._key_event_extra(event, name);\n",
  4772. "\n",
  4773. " this.send_message(name, {key: value,\n",
  4774. " guiEvent: simpleKeys(event)});\n",
  4775. " return false;\n",
  4776. "}\n",
  4777. "\n",
  4778. "mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
  4779. " if (name == 'download') {\n",
  4780. " this.handle_save(this, null);\n",
  4781. " } else {\n",
  4782. " this.send_message(\"toolbar_button\", {name: name});\n",
  4783. " }\n",
  4784. "};\n",
  4785. "\n",
  4786. "mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
  4787. " this.message.textContent = tooltip;\n",
  4788. "};\n",
  4789. "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
  4790. "\n",
  4791. "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n",
  4792. "\n",
  4793. "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
  4794. " // Create a \"websocket\"-like object which calls the given IPython comm\n",
  4795. " // object with the appropriate methods. Currently this is a non binary\n",
  4796. " // socket, so there is still some room for performance tuning.\n",
  4797. " var ws = {};\n",
  4798. "\n",
  4799. " ws.close = function() {\n",
  4800. " comm.close()\n",
  4801. " };\n",
  4802. " ws.send = function(m) {\n",
  4803. " //console.log('sending', m);\n",
  4804. " comm.send(m);\n",
  4805. " };\n",
  4806. " // Register the callback with on_msg.\n",
  4807. " comm.on_msg(function(msg) {\n",
  4808. " //console.log('receiving', msg['content']['data'], msg);\n",
  4809. " // Pass the mpl event to the overridden (by mpl) onmessage function.\n",
  4810. " ws.onmessage(msg['content']['data'])\n",
  4811. " });\n",
  4812. " return ws;\n",
  4813. "}\n",
  4814. "\n",
  4815. "mpl.mpl_figure_comm = function(comm, msg) {\n",
  4816. " // This is the function which gets called when the mpl process\n",
  4817. " // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
  4818. "\n",
  4819. " var id = msg.content.data.id;\n",
  4820. " // Get hold of the div created by the display call when the Comm\n",
  4821. " // socket was opened in Python.\n",
  4822. " var element = $(\"#\" + id);\n",
  4823. " var ws_proxy = comm_websocket_adapter(comm)\n",
  4824. "\n",
  4825. " function ondownload(figure, format) {\n",
  4826. " window.open(figure.imageObj.src);\n",
  4827. " }\n",
  4828. "\n",
  4829. " var fig = new mpl.figure(id, ws_proxy,\n",
  4830. " ondownload,\n",
  4831. " element.get(0));\n",
  4832. "\n",
  4833. " // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
  4834. " // web socket which is closed, not our websocket->open comm proxy.\n",
  4835. " ws_proxy.onopen();\n",
  4836. "\n",
  4837. " fig.parent_element = element.get(0);\n",
  4838. " fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
  4839. " if (!fig.cell_info) {\n",
  4840. " console.error(\"Failed to find cell for figure\", id, fig);\n",
  4841. " return;\n",
  4842. " }\n",
  4843. "\n",
  4844. " var output_index = fig.cell_info[2]\n",
  4845. " var cell = fig.cell_info[0];\n",
  4846. "\n",
  4847. "};\n",
  4848. "\n",
  4849. "mpl.figure.prototype.handle_close = function(fig, msg) {\n",
  4850. " var width = fig.canvas.width/mpl.ratio\n",
  4851. " fig.root.unbind('remove')\n",
  4852. "\n",
  4853. " // Update the output cell to use the data from the current canvas.\n",
  4854. " fig.push_to_output();\n",
  4855. " var dataURL = fig.canvas.toDataURL();\n",
  4856. " // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
  4857. " // the notebook keyboard shortcuts fail.\n",
  4858. " IPython.keyboard_manager.enable()\n",
  4859. " $(fig.parent_element).html('<img src=\"' + dataURL + '\" width=\"' + width + '\">');\n",
  4860. " fig.close_ws(fig, msg);\n",
  4861. "}\n",
  4862. "\n",
  4863. "mpl.figure.prototype.close_ws = function(fig, msg){\n",
  4864. " fig.send_message('closing', msg);\n",
  4865. " // fig.ws.close()\n",
  4866. "}\n",
  4867. "\n",
  4868. "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
  4869. " // Turn the data on the canvas into data in the output cell.\n",
  4870. " var width = this.canvas.width/mpl.ratio\n",
  4871. " var dataURL = this.canvas.toDataURL();\n",
  4872. " this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
  4873. "}\n",
  4874. "\n",
  4875. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  4876. " // Tell IPython that the notebook contents must change.\n",
  4877. " IPython.notebook.set_dirty(true);\n",
  4878. " this.send_message(\"ack\", {});\n",
  4879. " var fig = this;\n",
  4880. " // Wait a second, then push the new image to the DOM so\n",
  4881. " // that it is saved nicely (might be nice to debounce this).\n",
  4882. " setTimeout(function () { fig.push_to_output() }, 1000);\n",
  4883. "}\n",
  4884. "\n",
  4885. "mpl.figure.prototype._init_toolbar = function() {\n",
  4886. " var fig = this;\n",
  4887. "\n",
  4888. " var nav_element = $('<div/>')\n",
  4889. " nav_element.attr('style', 'width: 100%');\n",
  4890. " this.root.append(nav_element);\n",
  4891. "\n",
  4892. " // Define a callback function for later on.\n",
  4893. " function toolbar_event(event) {\n",
  4894. " return fig.toolbar_button_onclick(event['data']);\n",
  4895. " }\n",
  4896. " function toolbar_mouse_event(event) {\n",
  4897. " return fig.toolbar_button_onmouseover(event['data']);\n",
  4898. " }\n",
  4899. "\n",
  4900. " for(var toolbar_ind in mpl.toolbar_items){\n",
  4901. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  4902. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  4903. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  4904. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  4905. "\n",
  4906. " if (!name) { continue; };\n",
  4907. "\n",
  4908. " var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
  4909. " button.click(method_name, toolbar_event);\n",
  4910. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  4911. " nav_element.append(button);\n",
  4912. " }\n",
  4913. "\n",
  4914. " // Add the status bar.\n",
  4915. " var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
  4916. " nav_element.append(status_bar);\n",
  4917. " this.message = status_bar[0];\n",
  4918. "\n",
  4919. " // Add the close button to the window.\n",
  4920. " var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
  4921. " var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
  4922. " button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
  4923. " button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
  4924. " buttongrp.append(button);\n",
  4925. " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
  4926. " titlebar.prepend(buttongrp);\n",
  4927. "}\n",
  4928. "\n",
  4929. "mpl.figure.prototype._root_extra_style = function(el){\n",
  4930. " var fig = this\n",
  4931. " el.on(\"remove\", function(){\n",
  4932. "\tfig.close_ws(fig, {});\n",
  4933. " });\n",
  4934. "}\n",
  4935. "\n",
  4936. "mpl.figure.prototype._canvas_extra_style = function(el){\n",
  4937. " // this is important to make the div 'focusable\n",
  4938. " el.attr('tabindex', 0)\n",
  4939. " // reach out to IPython and tell the keyboard manager to turn it's self\n",
  4940. " // off when our div gets focus\n",
  4941. "\n",
  4942. " // location in version 3\n",
  4943. " if (IPython.notebook.keyboard_manager) {\n",
  4944. " IPython.notebook.keyboard_manager.register_events(el);\n",
  4945. " }\n",
  4946. " else {\n",
  4947. " // location in version 2\n",
  4948. " IPython.keyboard_manager.register_events(el);\n",
  4949. " }\n",
  4950. "\n",
  4951. "}\n",
  4952. "\n",
  4953. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  4954. " var manager = IPython.notebook.keyboard_manager;\n",
  4955. " if (!manager)\n",
  4956. " manager = IPython.keyboard_manager;\n",
  4957. "\n",
  4958. " // Check for shift+enter\n",
  4959. " if (event.shiftKey && event.which == 13) {\n",
  4960. " this.canvas_div.blur();\n",
  4961. " event.shiftKey = false;\n",
  4962. " // Send a \"J\" for go to next cell\n",
  4963. " event.which = 74;\n",
  4964. " event.keyCode = 74;\n",
  4965. " manager.command_mode();\n",
  4966. " manager.handle_keydown(event);\n",
  4967. " }\n",
  4968. "}\n",
  4969. "\n",
  4970. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  4971. " fig.ondownload(fig, null);\n",
  4972. "}\n",
  4973. "\n",
  4974. "\n",
  4975. "mpl.find_output_cell = function(html_output) {\n",
  4976. " // Return the cell and output element which can be found *uniquely* in the notebook.\n",
  4977. " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
  4978. " // IPython event is triggered only after the cells have been serialised, which for\n",
  4979. " // our purposes (turning an active figure into a static one), is too late.\n",
  4980. " var cells = IPython.notebook.get_cells();\n",
  4981. " var ncells = cells.length;\n",
  4982. " for (var i=0; i<ncells; i++) {\n",
  4983. " var cell = cells[i];\n",
  4984. " if (cell.cell_type === 'code'){\n",
  4985. " for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
  4986. " var data = cell.output_area.outputs[j];\n",
  4987. " if (data.data) {\n",
  4988. " // IPython >= 3 moved mimebundle to data attribute of output\n",
  4989. " data = data.data;\n",
  4990. " }\n",
  4991. " if (data['text/html'] == html_output) {\n",
  4992. " return [cell, data, j];\n",
  4993. " }\n",
  4994. " }\n",
  4995. " }\n",
  4996. " }\n",
  4997. "}\n",
  4998. "\n",
  4999. "// Register the function which deals with the matplotlib target/channel.\n",
  5000. "// The kernel may be null if the page has been refreshed.\n",
  5001. "if (IPython.notebook.kernel != null) {\n",
  5002. " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
  5003. "}\n"
  5004. ],
  5005. "text/plain": [
  5006. "<IPython.core.display.Javascript object>"
  5007. ]
  5008. },
  5009. "metadata": {},
  5010. "output_type": "display_data"
  5011. },
  5012. {
  5013. "data": {
  5014. "text/html": [
  5015. "<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAEvklEQVR4nO3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4Mwu4AAehrHaUAAAAASUVORK5CYII=\" width=\"640\">"
  5016. ],
  5017. "text/plain": [
  5018. "<IPython.core.display.HTML object>"
  5019. ]
  5020. },
  5021. "metadata": {},
  5022. "output_type": "display_data"
  5023. },
  5024. {
  5025. "data": {
  5026. "application/javascript": [
  5027. "/* Put everything inside the global mpl namespace */\n",
  5028. "window.mpl = {};\n",
  5029. "\n",
  5030. "\n",
  5031. "mpl.get_websocket_type = function() {\n",
  5032. " if (typeof(WebSocket) !== 'undefined') {\n",
  5033. " return WebSocket;\n",
  5034. " } else if (typeof(MozWebSocket) !== 'undefined') {\n",
  5035. " return MozWebSocket;\n",
  5036. " } else {\n",
  5037. " alert('Your browser does not have WebSocket support.' +\n",
  5038. " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
  5039. " 'Firefox 4 and 5 are also supported but you ' +\n",
  5040. " 'have to enable WebSockets in about:config.');\n",
  5041. " };\n",
  5042. "}\n",
  5043. "\n",
  5044. "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
  5045. " this.id = figure_id;\n",
  5046. "\n",
  5047. " this.ws = websocket;\n",
  5048. "\n",
  5049. " this.supports_binary = (this.ws.binaryType != undefined);\n",
  5050. "\n",
  5051. " if (!this.supports_binary) {\n",
  5052. " var warnings = document.getElementById(\"mpl-warnings\");\n",
  5053. " if (warnings) {\n",
  5054. " warnings.style.display = 'block';\n",
  5055. " warnings.textContent = (\n",
  5056. " \"This browser does not support binary websocket messages. \" +\n",
  5057. " \"Performance may be slow.\");\n",
  5058. " }\n",
  5059. " }\n",
  5060. "\n",
  5061. " this.imageObj = new Image();\n",
  5062. "\n",
  5063. " this.context = undefined;\n",
  5064. " this.message = undefined;\n",
  5065. " this.canvas = undefined;\n",
  5066. " this.rubberband_canvas = undefined;\n",
  5067. " this.rubberband_context = undefined;\n",
  5068. " this.format_dropdown = undefined;\n",
  5069. "\n",
  5070. " this.image_mode = 'full';\n",
  5071. "\n",
  5072. " this.root = $('<div/>');\n",
  5073. " this._root_extra_style(this.root)\n",
  5074. " this.root.attr('style', 'display: inline-block');\n",
  5075. "\n",
  5076. " $(parent_element).append(this.root);\n",
  5077. "\n",
  5078. " this._init_header(this);\n",
  5079. " this._init_canvas(this);\n",
  5080. " this._init_toolbar(this);\n",
  5081. "\n",
  5082. " var fig = this;\n",
  5083. "\n",
  5084. " this.waiting = false;\n",
  5085. "\n",
  5086. " this.ws.onopen = function () {\n",
  5087. " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
  5088. " fig.send_message(\"send_image_mode\", {});\n",
  5089. " if (mpl.ratio != 1) {\n",
  5090. " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
  5091. " }\n",
  5092. " fig.send_message(\"refresh\", {});\n",
  5093. " }\n",
  5094. "\n",
  5095. " this.imageObj.onload = function() {\n",
  5096. " if (fig.image_mode == 'full') {\n",
  5097. " // Full images could contain transparency (where diff images\n",
  5098. " // almost always do), so we need to clear the canvas so that\n",
  5099. " // there is no ghosting.\n",
  5100. " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
  5101. " }\n",
  5102. " fig.context.drawImage(fig.imageObj, 0, 0);\n",
  5103. " };\n",
  5104. "\n",
  5105. " this.imageObj.onunload = function() {\n",
  5106. " fig.ws.close();\n",
  5107. " }\n",
  5108. "\n",
  5109. " this.ws.onmessage = this._make_on_message_function(this);\n",
  5110. "\n",
  5111. " this.ondownload = ondownload;\n",
  5112. "}\n",
  5113. "\n",
  5114. "mpl.figure.prototype._init_header = function() {\n",
  5115. " var titlebar = $(\n",
  5116. " '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
  5117. " 'ui-helper-clearfix\"/>');\n",
  5118. " var titletext = $(\n",
  5119. " '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
  5120. " 'text-align: center; padding: 3px;\"/>');\n",
  5121. " titlebar.append(titletext)\n",
  5122. " this.root.append(titlebar);\n",
  5123. " this.header = titletext[0];\n",
  5124. "}\n",
  5125. "\n",
  5126. "\n",
  5127. "\n",
  5128. "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
  5129. "\n",
  5130. "}\n",
  5131. "\n",
  5132. "\n",
  5133. "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
  5134. "\n",
  5135. "}\n",
  5136. "\n",
  5137. "mpl.figure.prototype._init_canvas = function() {\n",
  5138. " var fig = this;\n",
  5139. "\n",
  5140. " var canvas_div = $('<div/>');\n",
  5141. "\n",
  5142. " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
  5143. "\n",
  5144. " function canvas_keyboard_event(event) {\n",
  5145. " return fig.key_event(event, event['data']);\n",
  5146. " }\n",
  5147. "\n",
  5148. " canvas_div.keydown('key_press', canvas_keyboard_event);\n",
  5149. " canvas_div.keyup('key_release', canvas_keyboard_event);\n",
  5150. " this.canvas_div = canvas_div\n",
  5151. " this._canvas_extra_style(canvas_div)\n",
  5152. " this.root.append(canvas_div);\n",
  5153. "\n",
  5154. " var canvas = $('<canvas/>');\n",
  5155. " canvas.addClass('mpl-canvas');\n",
  5156. " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
  5157. "\n",
  5158. " this.canvas = canvas[0];\n",
  5159. " this.context = canvas[0].getContext(\"2d\");\n",
  5160. "\n",
  5161. " var backingStore = this.context.backingStorePixelRatio ||\n",
  5162. "\tthis.context.webkitBackingStorePixelRatio ||\n",
  5163. "\tthis.context.mozBackingStorePixelRatio ||\n",
  5164. "\tthis.context.msBackingStorePixelRatio ||\n",
  5165. "\tthis.context.oBackingStorePixelRatio ||\n",
  5166. "\tthis.context.backingStorePixelRatio || 1;\n",
  5167. "\n",
  5168. " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
  5169. "\n",
  5170. " var rubberband = $('<canvas/>');\n",
  5171. " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
  5172. "\n",
  5173. " var pass_mouse_events = true;\n",
  5174. "\n",
  5175. " canvas_div.resizable({\n",
  5176. " start: function(event, ui) {\n",
  5177. " pass_mouse_events = false;\n",
  5178. " },\n",
  5179. " resize: function(event, ui) {\n",
  5180. " fig.request_resize(ui.size.width, ui.size.height);\n",
  5181. " },\n",
  5182. " stop: function(event, ui) {\n",
  5183. " pass_mouse_events = true;\n",
  5184. " fig.request_resize(ui.size.width, ui.size.height);\n",
  5185. " },\n",
  5186. " });\n",
  5187. "\n",
  5188. " function mouse_event_fn(event) {\n",
  5189. " if (pass_mouse_events)\n",
  5190. " return fig.mouse_event(event, event['data']);\n",
  5191. " }\n",
  5192. "\n",
  5193. " rubberband.mousedown('button_press', mouse_event_fn);\n",
  5194. " rubberband.mouseup('button_release', mouse_event_fn);\n",
  5195. " // Throttle sequential mouse events to 1 every 20ms.\n",
  5196. " rubberband.mousemove('motion_notify', mouse_event_fn);\n",
  5197. "\n",
  5198. " rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
  5199. " rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
  5200. "\n",
  5201. " canvas_div.on(\"wheel\", function (event) {\n",
  5202. " event = event.originalEvent;\n",
  5203. " event['data'] = 'scroll'\n",
  5204. " if (event.deltaY < 0) {\n",
  5205. " event.step = 1;\n",
  5206. " } else {\n",
  5207. " event.step = -1;\n",
  5208. " }\n",
  5209. " mouse_event_fn(event);\n",
  5210. " });\n",
  5211. "\n",
  5212. " canvas_div.append(canvas);\n",
  5213. " canvas_div.append(rubberband);\n",
  5214. "\n",
  5215. " this.rubberband = rubberband;\n",
  5216. " this.rubberband_canvas = rubberband[0];\n",
  5217. " this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
  5218. " this.rubberband_context.strokeStyle = \"#000000\";\n",
  5219. "\n",
  5220. " this._resize_canvas = function(width, height) {\n",
  5221. " // Keep the size of the canvas, canvas container, and rubber band\n",
  5222. " // canvas in synch.\n",
  5223. " canvas_div.css('width', width)\n",
  5224. " canvas_div.css('height', height)\n",
  5225. "\n",
  5226. " canvas.attr('width', width * mpl.ratio);\n",
  5227. " canvas.attr('height', height * mpl.ratio);\n",
  5228. " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
  5229. "\n",
  5230. " rubberband.attr('width', width);\n",
  5231. " rubberband.attr('height', height);\n",
  5232. " }\n",
  5233. "\n",
  5234. " // Set the figure to an initial 600x600px, this will subsequently be updated\n",
  5235. " // upon first draw.\n",
  5236. " this._resize_canvas(600, 600);\n",
  5237. "\n",
  5238. " // Disable right mouse context menu.\n",
  5239. " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
  5240. " return false;\n",
  5241. " });\n",
  5242. "\n",
  5243. " function set_focus () {\n",
  5244. " canvas.focus();\n",
  5245. " canvas_div.focus();\n",
  5246. " }\n",
  5247. "\n",
  5248. " window.setTimeout(set_focus, 100);\n",
  5249. "}\n",
  5250. "\n",
  5251. "mpl.figure.prototype._init_toolbar = function() {\n",
  5252. " var fig = this;\n",
  5253. "\n",
  5254. " var nav_element = $('<div/>')\n",
  5255. " nav_element.attr('style', 'width: 100%');\n",
  5256. " this.root.append(nav_element);\n",
  5257. "\n",
  5258. " // Define a callback function for later on.\n",
  5259. " function toolbar_event(event) {\n",
  5260. " return fig.toolbar_button_onclick(event['data']);\n",
  5261. " }\n",
  5262. " function toolbar_mouse_event(event) {\n",
  5263. " return fig.toolbar_button_onmouseover(event['data']);\n",
  5264. " }\n",
  5265. "\n",
  5266. " for(var toolbar_ind in mpl.toolbar_items) {\n",
  5267. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  5268. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  5269. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  5270. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  5271. "\n",
  5272. " if (!name) {\n",
  5273. " // put a spacer in here.\n",
  5274. " continue;\n",
  5275. " }\n",
  5276. " var button = $('<button/>');\n",
  5277. " button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
  5278. " 'ui-button-icon-only');\n",
  5279. " button.attr('role', 'button');\n",
  5280. " button.attr('aria-disabled', 'false');\n",
  5281. " button.click(method_name, toolbar_event);\n",
  5282. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  5283. "\n",
  5284. " var icon_img = $('<span/>');\n",
  5285. " icon_img.addClass('ui-button-icon-primary ui-icon');\n",
  5286. " icon_img.addClass(image);\n",
  5287. " icon_img.addClass('ui-corner-all');\n",
  5288. "\n",
  5289. " var tooltip_span = $('<span/>');\n",
  5290. " tooltip_span.addClass('ui-button-text');\n",
  5291. " tooltip_span.html(tooltip);\n",
  5292. "\n",
  5293. " button.append(icon_img);\n",
  5294. " button.append(tooltip_span);\n",
  5295. "\n",
  5296. " nav_element.append(button);\n",
  5297. " }\n",
  5298. "\n",
  5299. " var fmt_picker_span = $('<span/>');\n",
  5300. "\n",
  5301. " var fmt_picker = $('<select/>');\n",
  5302. " fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
  5303. " fmt_picker_span.append(fmt_picker);\n",
  5304. " nav_element.append(fmt_picker_span);\n",
  5305. " this.format_dropdown = fmt_picker[0];\n",
  5306. "\n",
  5307. " for (var ind in mpl.extensions) {\n",
  5308. " var fmt = mpl.extensions[ind];\n",
  5309. " var option = $(\n",
  5310. " '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
  5311. " fmt_picker.append(option)\n",
  5312. " }\n",
  5313. "\n",
  5314. " // Add hover states to the ui-buttons\n",
  5315. " $( \".ui-button\" ).hover(\n",
  5316. " function() { $(this).addClass(\"ui-state-hover\");},\n",
  5317. " function() { $(this).removeClass(\"ui-state-hover\");}\n",
  5318. " );\n",
  5319. "\n",
  5320. " var status_bar = $('<span class=\"mpl-message\"/>');\n",
  5321. " nav_element.append(status_bar);\n",
  5322. " this.message = status_bar[0];\n",
  5323. "}\n",
  5324. "\n",
  5325. "mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
  5326. " // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
  5327. " // which will in turn request a refresh of the image.\n",
  5328. " this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
  5329. "}\n",
  5330. "\n",
  5331. "mpl.figure.prototype.send_message = function(type, properties) {\n",
  5332. " properties['type'] = type;\n",
  5333. " properties['figure_id'] = this.id;\n",
  5334. " this.ws.send(JSON.stringify(properties));\n",
  5335. "}\n",
  5336. "\n",
  5337. "mpl.figure.prototype.send_draw_message = function() {\n",
  5338. " if (!this.waiting) {\n",
  5339. " this.waiting = true;\n",
  5340. " this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
  5341. " }\n",
  5342. "}\n",
  5343. "\n",
  5344. "\n",
  5345. "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
  5346. " var format_dropdown = fig.format_dropdown;\n",
  5347. " var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
  5348. " fig.ondownload(fig, format);\n",
  5349. "}\n",
  5350. "\n",
  5351. "\n",
  5352. "mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
  5353. " var size = msg['size'];\n",
  5354. " if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
  5355. " fig._resize_canvas(size[0], size[1]);\n",
  5356. " fig.send_message(\"refresh\", {});\n",
  5357. " };\n",
  5358. "}\n",
  5359. "\n",
  5360. "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
  5361. " var x0 = msg['x0'] / mpl.ratio;\n",
  5362. " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
  5363. " var x1 = msg['x1'] / mpl.ratio;\n",
  5364. " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
  5365. " x0 = Math.floor(x0) + 0.5;\n",
  5366. " y0 = Math.floor(y0) + 0.5;\n",
  5367. " x1 = Math.floor(x1) + 0.5;\n",
  5368. " y1 = Math.floor(y1) + 0.5;\n",
  5369. " var min_x = Math.min(x0, x1);\n",
  5370. " var min_y = Math.min(y0, y1);\n",
  5371. " var width = Math.abs(x1 - x0);\n",
  5372. " var height = Math.abs(y1 - y0);\n",
  5373. "\n",
  5374. " fig.rubberband_context.clearRect(\n",
  5375. " 0, 0, fig.canvas.width, fig.canvas.height);\n",
  5376. "\n",
  5377. " fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
  5378. "}\n",
  5379. "\n",
  5380. "mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
  5381. " // Updates the figure title.\n",
  5382. " fig.header.textContent = msg['label'];\n",
  5383. "}\n",
  5384. "\n",
  5385. "mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
  5386. " var cursor = msg['cursor'];\n",
  5387. " switch(cursor)\n",
  5388. " {\n",
  5389. " case 0:\n",
  5390. " cursor = 'pointer';\n",
  5391. " break;\n",
  5392. " case 1:\n",
  5393. " cursor = 'default';\n",
  5394. " break;\n",
  5395. " case 2:\n",
  5396. " cursor = 'crosshair';\n",
  5397. " break;\n",
  5398. " case 3:\n",
  5399. " cursor = 'move';\n",
  5400. " break;\n",
  5401. " }\n",
  5402. " fig.rubberband_canvas.style.cursor = cursor;\n",
  5403. "}\n",
  5404. "\n",
  5405. "mpl.figure.prototype.handle_message = function(fig, msg) {\n",
  5406. " fig.message.textContent = msg['message'];\n",
  5407. "}\n",
  5408. "\n",
  5409. "mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
  5410. " // Request the server to send over a new figure.\n",
  5411. " fig.send_draw_message();\n",
  5412. "}\n",
  5413. "\n",
  5414. "mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
  5415. " fig.image_mode = msg['mode'];\n",
  5416. "}\n",
  5417. "\n",
  5418. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  5419. " // Called whenever the canvas gets updated.\n",
  5420. " this.send_message(\"ack\", {});\n",
  5421. "}\n",
  5422. "\n",
  5423. "// A function to construct a web socket function for onmessage handling.\n",
  5424. "// Called in the figure constructor.\n",
  5425. "mpl.figure.prototype._make_on_message_function = function(fig) {\n",
  5426. " return function socket_on_message(evt) {\n",
  5427. " if (evt.data instanceof Blob) {\n",
  5428. " /* FIXME: We get \"Resource interpreted as Image but\n",
  5429. " * transferred with MIME type text/plain:\" errors on\n",
  5430. " * Chrome. But how to set the MIME type? It doesn't seem\n",
  5431. " * to be part of the websocket stream */\n",
  5432. " evt.data.type = \"image/png\";\n",
  5433. "\n",
  5434. " /* Free the memory for the previous frames */\n",
  5435. " if (fig.imageObj.src) {\n",
  5436. " (window.URL || window.webkitURL).revokeObjectURL(\n",
  5437. " fig.imageObj.src);\n",
  5438. " }\n",
  5439. "\n",
  5440. " fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
  5441. " evt.data);\n",
  5442. " fig.updated_canvas_event();\n",
  5443. " fig.waiting = false;\n",
  5444. " return;\n",
  5445. " }\n",
  5446. " else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
  5447. " fig.imageObj.src = evt.data;\n",
  5448. " fig.updated_canvas_event();\n",
  5449. " fig.waiting = false;\n",
  5450. " return;\n",
  5451. " }\n",
  5452. "\n",
  5453. " var msg = JSON.parse(evt.data);\n",
  5454. " var msg_type = msg['type'];\n",
  5455. "\n",
  5456. " // Call the \"handle_{type}\" callback, which takes\n",
  5457. " // the figure and JSON message as its only arguments.\n",
  5458. " try {\n",
  5459. " var callback = fig[\"handle_\" + msg_type];\n",
  5460. " } catch (e) {\n",
  5461. " console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
  5462. " return;\n",
  5463. " }\n",
  5464. "\n",
  5465. " if (callback) {\n",
  5466. " try {\n",
  5467. " // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
  5468. " callback(fig, msg);\n",
  5469. " } catch (e) {\n",
  5470. " console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
  5471. " }\n",
  5472. " }\n",
  5473. " };\n",
  5474. "}\n",
  5475. "\n",
  5476. "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
  5477. "mpl.findpos = function(e) {\n",
  5478. " //this section is from http://www.quirksmode.org/js/events_properties.html\n",
  5479. " var targ;\n",
  5480. " if (!e)\n",
  5481. " e = window.event;\n",
  5482. " if (e.target)\n",
  5483. " targ = e.target;\n",
  5484. " else if (e.srcElement)\n",
  5485. " targ = e.srcElement;\n",
  5486. " if (targ.nodeType == 3) // defeat Safari bug\n",
  5487. " targ = targ.parentNode;\n",
  5488. "\n",
  5489. " // jQuery normalizes the pageX and pageY\n",
  5490. " // pageX,Y are the mouse positions relative to the document\n",
  5491. " // offset() returns the position of the element relative to the document\n",
  5492. " var x = e.pageX - $(targ).offset().left;\n",
  5493. " var y = e.pageY - $(targ).offset().top;\n",
  5494. "\n",
  5495. " return {\"x\": x, \"y\": y};\n",
  5496. "};\n",
  5497. "\n",
  5498. "/*\n",
  5499. " * return a copy of an object with only non-object keys\n",
  5500. " * we need this to avoid circular references\n",
  5501. " * http://stackoverflow.com/a/24161582/3208463\n",
  5502. " */\n",
  5503. "function simpleKeys (original) {\n",
  5504. " return Object.keys(original).reduce(function (obj, key) {\n",
  5505. " if (typeof original[key] !== 'object')\n",
  5506. " obj[key] = original[key]\n",
  5507. " return obj;\n",
  5508. " }, {});\n",
  5509. "}\n",
  5510. "\n",
  5511. "mpl.figure.prototype.mouse_event = function(event, name) {\n",
  5512. " var canvas_pos = mpl.findpos(event)\n",
  5513. "\n",
  5514. " if (name === 'button_press')\n",
  5515. " {\n",
  5516. " this.canvas.focus();\n",
  5517. " this.canvas_div.focus();\n",
  5518. " }\n",
  5519. "\n",
  5520. " var x = canvas_pos.x * mpl.ratio;\n",
  5521. " var y = canvas_pos.y * mpl.ratio;\n",
  5522. "\n",
  5523. " this.send_message(name, {x: x, y: y, button: event.button,\n",
  5524. " step: event.step,\n",
  5525. " guiEvent: simpleKeys(event)});\n",
  5526. "\n",
  5527. " /* This prevents the web browser from automatically changing to\n",
  5528. " * the text insertion cursor when the button is pressed. We want\n",
  5529. " * to control all of the cursor setting manually through the\n",
  5530. " * 'cursor' event from matplotlib */\n",
  5531. " event.preventDefault();\n",
  5532. " return false;\n",
  5533. "}\n",
  5534. "\n",
  5535. "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
  5536. " // Handle any extra behaviour associated with a key event\n",
  5537. "}\n",
  5538. "\n",
  5539. "mpl.figure.prototype.key_event = function(event, name) {\n",
  5540. "\n",
  5541. " // Prevent repeat events\n",
  5542. " if (name == 'key_press')\n",
  5543. " {\n",
  5544. " if (event.which === this._key)\n",
  5545. " return;\n",
  5546. " else\n",
  5547. " this._key = event.which;\n",
  5548. " }\n",
  5549. " if (name == 'key_release')\n",
  5550. " this._key = null;\n",
  5551. "\n",
  5552. " var value = '';\n",
  5553. " if (event.ctrlKey && event.which != 17)\n",
  5554. " value += \"ctrl+\";\n",
  5555. " if (event.altKey && event.which != 18)\n",
  5556. " value += \"alt+\";\n",
  5557. " if (event.shiftKey && event.which != 16)\n",
  5558. " value += \"shift+\";\n",
  5559. "\n",
  5560. " value += 'k';\n",
  5561. " value += event.which.toString();\n",
  5562. "\n",
  5563. " this._key_event_extra(event, name);\n",
  5564. "\n",
  5565. " this.send_message(name, {key: value,\n",
  5566. " guiEvent: simpleKeys(event)});\n",
  5567. " return false;\n",
  5568. "}\n",
  5569. "\n",
  5570. "mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
  5571. " if (name == 'download') {\n",
  5572. " this.handle_save(this, null);\n",
  5573. " } else {\n",
  5574. " this.send_message(\"toolbar_button\", {name: name});\n",
  5575. " }\n",
  5576. "};\n",
  5577. "\n",
  5578. "mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
  5579. " this.message.textContent = tooltip;\n",
  5580. "};\n",
  5581. "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
  5582. "\n",
  5583. "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n",
  5584. "\n",
  5585. "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
  5586. " // Create a \"websocket\"-like object which calls the given IPython comm\n",
  5587. " // object with the appropriate methods. Currently this is a non binary\n",
  5588. " // socket, so there is still some room for performance tuning.\n",
  5589. " var ws = {};\n",
  5590. "\n",
  5591. " ws.close = function() {\n",
  5592. " comm.close()\n",
  5593. " };\n",
  5594. " ws.send = function(m) {\n",
  5595. " //console.log('sending', m);\n",
  5596. " comm.send(m);\n",
  5597. " };\n",
  5598. " // Register the callback with on_msg.\n",
  5599. " comm.on_msg(function(msg) {\n",
  5600. " //console.log('receiving', msg['content']['data'], msg);\n",
  5601. " // Pass the mpl event to the overridden (by mpl) onmessage function.\n",
  5602. " ws.onmessage(msg['content']['data'])\n",
  5603. " });\n",
  5604. " return ws;\n",
  5605. "}\n",
  5606. "\n",
  5607. "mpl.mpl_figure_comm = function(comm, msg) {\n",
  5608. " // This is the function which gets called when the mpl process\n",
  5609. " // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
  5610. "\n",
  5611. " var id = msg.content.data.id;\n",
  5612. " // Get hold of the div created by the display call when the Comm\n",
  5613. " // socket was opened in Python.\n",
  5614. " var element = $(\"#\" + id);\n",
  5615. " var ws_proxy = comm_websocket_adapter(comm)\n",
  5616. "\n",
  5617. " function ondownload(figure, format) {\n",
  5618. " window.open(figure.imageObj.src);\n",
  5619. " }\n",
  5620. "\n",
  5621. " var fig = new mpl.figure(id, ws_proxy,\n",
  5622. " ondownload,\n",
  5623. " element.get(0));\n",
  5624. "\n",
  5625. " // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
  5626. " // web socket which is closed, not our websocket->open comm proxy.\n",
  5627. " ws_proxy.onopen();\n",
  5628. "\n",
  5629. " fig.parent_element = element.get(0);\n",
  5630. " fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
  5631. " if (!fig.cell_info) {\n",
  5632. " console.error(\"Failed to find cell for figure\", id, fig);\n",
  5633. " return;\n",
  5634. " }\n",
  5635. "\n",
  5636. " var output_index = fig.cell_info[2]\n",
  5637. " var cell = fig.cell_info[0];\n",
  5638. "\n",
  5639. "};\n",
  5640. "\n",
  5641. "mpl.figure.prototype.handle_close = function(fig, msg) {\n",
  5642. " var width = fig.canvas.width/mpl.ratio\n",
  5643. " fig.root.unbind('remove')\n",
  5644. "\n",
  5645. " // Update the output cell to use the data from the current canvas.\n",
  5646. " fig.push_to_output();\n",
  5647. " var dataURL = fig.canvas.toDataURL();\n",
  5648. " // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
  5649. " // the notebook keyboard shortcuts fail.\n",
  5650. " IPython.keyboard_manager.enable()\n",
  5651. " $(fig.parent_element).html('<img src=\"' + dataURL + '\" width=\"' + width + '\">');\n",
  5652. " fig.close_ws(fig, msg);\n",
  5653. "}\n",
  5654. "\n",
  5655. "mpl.figure.prototype.close_ws = function(fig, msg){\n",
  5656. " fig.send_message('closing', msg);\n",
  5657. " // fig.ws.close()\n",
  5658. "}\n",
  5659. "\n",
  5660. "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
  5661. " // Turn the data on the canvas into data in the output cell.\n",
  5662. " var width = this.canvas.width/mpl.ratio\n",
  5663. " var dataURL = this.canvas.toDataURL();\n",
  5664. " this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
  5665. "}\n",
  5666. "\n",
  5667. "mpl.figure.prototype.updated_canvas_event = function() {\n",
  5668. " // Tell IPython that the notebook contents must change.\n",
  5669. " IPython.notebook.set_dirty(true);\n",
  5670. " this.send_message(\"ack\", {});\n",
  5671. " var fig = this;\n",
  5672. " // Wait a second, then push the new image to the DOM so\n",
  5673. " // that it is saved nicely (might be nice to debounce this).\n",
  5674. " setTimeout(function () { fig.push_to_output() }, 1000);\n",
  5675. "}\n",
  5676. "\n",
  5677. "mpl.figure.prototype._init_toolbar = function() {\n",
  5678. " var fig = this;\n",
  5679. "\n",
  5680. " var nav_element = $('<div/>')\n",
  5681. " nav_element.attr('style', 'width: 100%');\n",
  5682. " this.root.append(nav_element);\n",
  5683. "\n",
  5684. " // Define a callback function for later on.\n",
  5685. " function toolbar_event(event) {\n",
  5686. " return fig.toolbar_button_onclick(event['data']);\n",
  5687. " }\n",
  5688. " function toolbar_mouse_event(event) {\n",
  5689. " return fig.toolbar_button_onmouseover(event['data']);\n",
  5690. " }\n",
  5691. "\n",
  5692. " for(var toolbar_ind in mpl.toolbar_items){\n",
  5693. " var name = mpl.toolbar_items[toolbar_ind][0];\n",
  5694. " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
  5695. " var image = mpl.toolbar_items[toolbar_ind][2];\n",
  5696. " var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
  5697. "\n",
  5698. " if (!name) { continue; };\n",
  5699. "\n",
  5700. " var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
  5701. " button.click(method_name, toolbar_event);\n",
  5702. " button.mouseover(tooltip, toolbar_mouse_event);\n",
  5703. " nav_element.append(button);\n",
  5704. " }\n",
  5705. "\n",
  5706. " // Add the status bar.\n",
  5707. " var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
  5708. " nav_element.append(status_bar);\n",
  5709. " this.message = status_bar[0];\n",
  5710. "\n",
  5711. " // Add the close button to the window.\n",
  5712. " var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
  5713. " var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
  5714. " button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
  5715. " button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
  5716. " buttongrp.append(button);\n",
  5717. " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
  5718. " titlebar.prepend(buttongrp);\n",
  5719. "}\n",
  5720. "\n",
  5721. "mpl.figure.prototype._root_extra_style = function(el){\n",
  5722. " var fig = this\n",
  5723. " el.on(\"remove\", function(){\n",
  5724. "\tfig.close_ws(fig, {});\n",
  5725. " });\n",
  5726. "}\n",
  5727. "\n",
  5728. "mpl.figure.prototype._canvas_extra_style = function(el){\n",
  5729. " // this is important to make the div 'focusable\n",
  5730. " el.attr('tabindex', 0)\n",
  5731. " // reach out to IPython and tell the keyboard manager to turn it's self\n",
  5732. " // off when our div gets focus\n",