PK p+K,|| tutormagic.py""" ========== tutormagic ========== Magics to display pythontutor.com in the notebook. Usage ===== To enable the magics below, execute ``%load_ext tutormagic``. ``%%tutormagic`` {tutormagic_DOC} """ __version__="0.3.0" #----------------------------------------------------------------------------- # Copyright (C) 2015-2017 Kiko Correoso and the pythontutor.com developers # # Distributed under the terms of the MIT License. The full license is in # the file LICENSE, distributed as part of this software. # # Contributors: # kikocorreoso #----------------------------------------------------------------------------- import webbrowser import warnings warnings.simplefilter("always") import sys if sys.version_info.major == 2 and sys.version_info.minor == 7: from urllib import quote elif sys.version_info.major == 3 and sys.version_info.minor >= 3: from urllib.parse import quote else: warnings.warn("This extension has not been tested on this Python version", UserWarning) from IPython.core.magic import Magics, magics_class, cell_magic from IPython.testing.skipdoctest import skip_doctest from IPython.core.magic_arguments import (argument, magic_arguments, parse_argstring) from IPython.utils.text import dedent from IPython.display import display, IFrame, HTML @magics_class class TutorMagics(Magics): """ A magic function to show pythontutor.com frame from a code cell. """ def __init__(self, shell): super(TutorMagics, self).__init__(shell) @skip_doctest @magic_arguments() @argument( '-l', '--lang', action='store', nargs = 1, help="Languages to be displayed within the iframe or in a new tab. " "Possible values are: " "python2, python3, java, javascript, typescript, ruby, c, c++" ) @argument( '-h', '--height', action='store', nargs=1, help="Change the height of the output area display in pixels" ) @argument( '-t', '--tab', action='store_true', help="Open pythontutor in a new tab", ) @argument( '-s', '--secure', action='store_true', help="Open pythontutor using https in a new tab", ) @argument( '-k', '--link', action='store_true', help="Just display a link to pythontutor", ) @argument( '-r', '--run', action='store_true', help="Run the cell code also in the notebook", ) @argument( '--cumulative', action='store_true', default=False, help="PythonTutor config: Set the cumulative option to True", ) @argument( '--heapPrimitives', action='store_true', default=False, help="PythonTutor config: Render objects on the heap", ) @argument( '--textReferences', action='store_true', default=False, help="PythonTutor config: Use text labels for references", ) @argument( '--curInstr', action='store', default=0, help="PythonTutor config: Start at the defined step", ) @argument( '--verticalStack', action='store_true', default=False, help="Set visualization to stack atop one another", ) #@needs_local_scope @argument( 'code', nargs='*', ) @cell_magic def tutor(self, line, cell=None, local_ns=None): ''' Create an iframe embedding the pythontutor.com page with the code included in the code cell:: In [1]: %%tutor -l 'python3' ....: a = 1 ....: b = 1 ....: a + b [You will see an iframe with the pythontutor.com page including the code above] ''' args = parse_argstring(self.tutor, line) if args.lang: if args.lang[0].lower() in ['python2', 'python3', 'java', 'javascript', 'typescript', 'ruby', 'c', 'c++']: lang = args.lang[0].lower() else: raise ValueError( "{} not supported. Only the following options are allowed: " "'python2', 'python3', 'java', 'javascript', " "'typescript', 'ruby', 'c', 'c++'".format(args.lang[0])) else: lang = "python3" # Sometimes user will want SSL pythontutor site if # jupyter/hub is using SSL as well protocol = 'http://' if args.secure: protocol = 'https://' url = protocol + "pythontutor.com/iframe-embed.html#code=" url += quote(cell) url += "&origin=opt-frontend.js" # Add custom pythontutor options, defaults to all false url += "&cumulative={}".format(str(args.cumulative).lower()) url += "&heapPrimitives={}".format(str(args.heapPrimitives).lower()) url += "&textReferences={}".format(str(args.textReferences).lower()) url += "&curInstr={}&".format(str(args.curInstr).lower()) url += "&verticalStack={}&".format(str(args.verticalStack).lower()) # Setup the language URL param if lang == "python3": url += "py=3" elif lang == "python2": url += "py=2" elif lang == "java": url += "py=java" elif lang == "javascript": url += "py=js" elif lang == "typescript": url += "py=ts" elif lang == "ruby": url += "py=ruby" elif lang == "c": url += "py=c" elif lang == "c++": url += "py=cpp" # Add the rest of the misc options to pythontutor url += "&rawInputLstJSON=%5B%5D&codeDivWidth=50%25&codeDivHeight=100%25" # Display in new tab, or in iframe, or link to it via anchor link? if args.link: # Create html link to pythontutor css = ("box-sizing: border-box; " "padding: 0 5px; border: " "1px solid #CFCFCF;") display( HTML( data=( '
' 'Python Tutor' '
'.format(css, url) ) ) ) elif args.tab: # Open up a new tab in the browser to pythontutor URL webbrowser.open_new_tab(url) else: # Display the results in the output area if args.height: display(IFrame( url, height = int(args.height[0]), width = "100%" )) else: display(IFrame(url, height = 350, width = "100%")) if args.run: # Run cell like normal self.shell.run_cell(cell) __doc__ = __doc__.format( tutormagic_DOC = dedent(TutorMagics.tutor.__doc__)) def load_ipython_extension(ip): """Load the extension in IPython.""" ip.register_magics(TutorMagics) PKl+Kqx88"tutormagic-0.3.0.dist-info/LICENSEThe MIT License (MIT) Copyright (c) 2015 kikocorreoso Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!H١Wd tutormagic-0.3.0.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,Q0343 /, (-JLR()*M ILR(4KM̫#DPK!H#tutormagic-0.3.0.dist-info/METADATAXmo6_A`4&k.Ьi5})XSFRq{zu"; bw||'O?RS<< LL+6_8hw8*͔l?q`L+t2YJQl+kcz;B"^.fނXd\)~7<+sF˓LqkB x;'"aD<l:e&eT""WuQ} N~w>kX.^e2_ /KX+yco?cs|h~c L8NfUEH,NDZ^ c8 BR~/ ^> 2&CVy 3!lG^1@ک%e&y5<g`VҦi',tZ~Uun[yX]d$ !z ^X!#; Q#̃!o1f fX3#{FF-K5D#b,by+ara:T1XI BBJ[6b 4ktq캪̇q$f:&-ތT^7>fIP~,"2m4֍t4ZOTivĕ(V#W}($.J|>e1NlKԂ NF 9t~G R7lYPG`S* ~9B0 YAK" Z Hg+asQeg?ڱ3%5\שٳٳ~7Nti>p_zyP)"{Qlldv`TًKg'f~i/)dP0" `ď~t4t_}0QRHѕXh!ѐܾ698:li'9-ȗ$t-NًcVT+ruSz{'i.]QDB UBe}B偬 ! Sj34t[׎ߨS3mb+zȠ_ }P0HP׾+lj+{?>47 L&AtHqiDmP7U;V`HYTl~c<-]I2)wnEdjW}&|py CѩZrRRz%s|L۝v{ P;uzlo3t׺~VdDঽѵk1,+NA#FNBYl_lz"@[\{(#h_MsrҰ-z }dzxnF-`1*C t[. /\IrłOWzd3?) z8;OTX0b]ݩƣF*5@iQg?!M{^pPvߒG+I#ԭT s|85YIv=hDmzˍ"]}t: 0] jРmk?=+t}[5fm>n5bgަgGG[F|>hUPK!HIy!tutormagic-0.3.0.dist-info/RECORD}Iv0нg ,JeUdcx*20ХuT=Ok1w⑾g{7.bёn; \FB}>!xQaOW=JIɴ9 74wÔ[{Gy`fyU_'ŧθ.5ۜ_q"5PZf#c _t