Using Custom Magic and SVG Table Builder Classes to turn %quickref Magic into a SVG Table.

This notebook uses the SVG table classes here to build SGV tables of the %quickref text. The github project containing the external files used is here.


In [2]:
cd Git_quickref_project/


[Errno 2] No such file or directory: 'Git_quickref_project/'
/home/damon/Documents/CODE/Programing/Python/IPython/Quickref/Git_quickref_project

Grab the %quickref Text with a New Magic Command

Creating custom(s) command documentation.


In [1]:
from IPython.core.magic import (magics_class, line_magic)
from IPython.core.magics.basic import BasicMagics #Where _magic_docs is defined, also BasicMagics inherits the Magics already
@magics_class
class MyMagics(BasicMagics):
    @line_magic
    def quickref_text(self, line): 
        """ Return the quickref text to be assigned to a variable """
        from IPython.core.usage import quick_reference
        qr = quick_reference + self._magic_docs(brief=True)
        return qr
            
ip = get_ipython()
ip.register_magics(MyMagics)

quickref_text = %quickref_text

Convert the %quickref Text to a Dictionary


In [2]:
from Build_dict import build_dict #to build a dictionary out of the %quickref text file

lines = quickref_text.split('\n')
Quick_ref_dic = build_dict(lines) #this parses the text and builds an ordered dict out of it.

#make a list of all the headings
heading =list(Quick_ref_dic.keys())

Dictionary to SVG Table


In [3]:
def quick_dict_to_SVG(Table_dict, width=500, L_width=200, x_location=0, y_location=0):
    title_color = (173,216,230) #lightblue
    header_color = (213,224,197) #a grey green
    font_family="Arial"
    font_size=12 #font_size=font_size
    header_font_size = 14
    line_width=1 #, line_width=line_width
    #good site for color translations: http://www.yellowpipe.com/yis/tools/hex-to-rgb/color-converter.php
    TL = [x_location, y_location]
    row_horz_shift = 5 #shift the row text over to line up with the header text
    display_text = ''
    for header, values in Table_dict.items():
        entries = list(values.keys())
        #Types = values['_type_'] 
        heading = True #default since most row groups have a heading
        #There are at least one entry unter each header
        for entry, explained in values.items():
            if entry =='_type_': 
                #This entry indicates what type of group of rows this is.
                
                #Select the header color
                if explained == 'Heading':
                    bg = header_color
                elif explained == 'Title':
                    bg = title_color
                else: #for comment or other headerless row groups
                    heading = False #
                    
                if heading:    #Build a header for the table
                    head=Table_Header(text=header, width=width, TL=list(TL), background=bg, font_family=font_family, size=header_font_size, line_width=line_width)
                    bg=header_color
                    display_text += head.get_SVG_header() #add the header
                    TL[1]=head.bottom #Move the top left cordinates
                    spacer = Table_rows(font_size=3, width=width, top_left=list(TL), line_width=line_width)
                    spacer.set_count(1)
                    display_text += spacer.get_SVG_rows() #add a spacer after the header
                    TL[1]=spacer.bottom #Move the top left cordinates
                    
            elif "_Comment_starts_at_" in entry: #This is a block of comments
                
                #make a row with one column and multiple lines of text
                txt = clean_text('\n'.join(explained))
                text_list = [[txt]] #
                rows = Table_rows(top_left=list(TL),width=width,font_family=font_family,font_size=font_size, line_width=line_width)
                rows.set_text_list(text_list)
                rows.x_shift=row_horz_shift
                display_text += rows.get_SVG_rows()
                TL[1]=rows.bottom #Move the top left cordinates
                
            elif "_Multiline_Flag_" in entry: #this is an example that spans multiple lines
                
                #Make a row with two columns and multiple lines of text
                multi_left=[]
                multi_right=[]
                #Multiline row entries are lists of tuple pairs
                for line in explained:
                    left = clean_text(line[0])
                    multi_left.append(left)
                    right = clean_text(line[1])
                    multi_right.append(right)
                multi_left_text = '\n'.join(multi_left)
                multi_right_text = '\n'.join(multi_right)
                text_list = [[multi_left_text,multi_right_text]]
                rows = Table_rows(top_left=list(TL),width=width,font_family=font_family,font_size=font_size, line_width=line_width)
                rows.set_text_list(text_list)
                rows.column_locations=[0,L_width]
                rows.x_shift=row_horz_shift
                display_text += rows.get_SVG_rows()
                TL[1]=rows.bottom      #Move the top left cordinates  
                
            else: #this must be rows of examples and explanation
                rows = Table_rows(top_left=list(TL),width=width,font_family=font_family,font_size=font_size, line_width=line_width)
                text_list = [[clean_text(entry), clean_text(explained)]]
                rows.set_text_list(text_list)
                rows.column_locations=[0,L_width]
                rows.x_shift=row_horz_shift
                display_text += rows.get_SVG_rows()
                TL[1]=rows.bottom #Move the top left cordinates
        
        #Add a spacer at the bottom of the row group
        bot_spacer = Table_rows(font_size=3, width=width, top_left=list(TL), line_width=line_width)
        bot_spacer.set_count(1)
        display_text += bot_spacer.get_SVG_rows()
        TL[1]=bot_spacer.bottom #Move the top left cordinates
        
    return (display_text, width+2*line_width+TL[0], TL[1]+2*line_width)

Make a table of the Basic commands


In [4]:
from IPython.display import SVG #to display the results
from collections import OrderedDict as Or_dict

In [9]:
#load the SVG Table classes
%run SVG_Table_Classes #used instead of import during editing
#Set the table size
width=552
Left_column_width=170
#Build the SVG text
start_stop = 4 #number of headings to have on the left
Table_dict = Or_dict()
for i in range(0,start_stop): #Grab some headings
    Table_dict[heading[i]]=Quick_ref_dic[heading[i]] 
    
display_text, right, bottom = quick_dict_to_SVG(Table_dict, width=width, L_width=Left_column_width)
running_text = display_text
Bottom = bottom
#print("Right = %d"%right)
Table_dict = Or_dict()
for i in range(start_stop,8): #Grab some headings
    Table_dict[heading[i]]=Quick_ref_dic[heading[i]] 
width=525
Left_column_width=130
display_text, right, bottom = quick_dict_to_SVG(Table_dict, width=width, L_width=Left_column_width, x_location=right+10)
Bottom =max(Bottom , bottom)
running_text += display_text

Text=Set_SVG_view(right, Bottom, running_text)

Save and display the results...


In [10]:
#Save the result

with open("Basic_Help.svg",'w') as f: 
    f.write(Text)

#Display the results
SVG(Text)


Out[10]:
IPython -- An enhanced Interactive Python - Quick Reference Card ipython Open IPython terminal console juypter console Open terminal console (4.0 and later) ipython qtconsole Open IPython qtconsole juptyer qtconsole Open qtconsole (4.0 and later) ipython notebook Open IPython Notebook (deprecated) jupyter notebook Open Notebook (browser interface, 4.0 and later) ipython --help-all Show all IPython start flags Within IPython: obj?, obj?? Get help, or more help for object (also works as?obj, ??obj). ?foo.*abc* List names in 'foo' containing 'abc' in them. %magic Information about IPython's 'magic' % functions. Magic functions are prefixed by % or %%, and typically take their argumentswithout parentheses, quotes or even commas for convenience. Line magics take asingle % and cell magics are prefixed with two %%. Example magic function calls: %alias d ls -F 'd' is now an alias for 'ls -F' alias d ls -F Works if 'alias' not a python name alist = %alias Get list of aliases to 'alist' cd /usr/share Obvious. cd -<tab> to choose from visited dirs. %cd?? See help AND source for magic %cd %timeit x=10 time the 'x=10' statement with high precision. %%timeit x=2**100x**100 time 'x**100' with a setup of 'x=2**100'; setup code is notcounted. This is an example of a cell magic. System commands: !cp a.txt b/ System command escape, calls os.system() cp a.txt b/ after %rehashx, most system commands work without ! cp ${f}.txt $bar Variable expansion in magics and system commands files = !ls /usr Capture sytem command output files.s, files.l, files.n "a b c", ['a','b','c'], 'a\nb\nc' History: _i, _ii, _iii Previous, next previous, next next previous input _i4, _ih[2:5] Input history line 4, lines 2-4 exec _i81 Execute input history line #81 again %rep 81 Edit input history line #81 _, __, ___ previous, next previous, next next previous output _dh Directory history _oh Output history %hist Command history of current session. %hist -g foo Search command history of (almost) all sessions for 'foo'. %hist -g Command history of (almost) all sessions. %hist 1/2-8 Command history containing lines 2-8 of session 1. %hist 1/ ~2/ Command history of session 1 and 2 sessions before current. %hist ~8/1-~6/5 Command history from line 1 of 8 sessions ago toline 5 of 6 sessions ago. %edit 0/ Open editor to execute code with history of current session. Autocall: f 1,2 f(1,2) # Off by default, enable with %autocall magic. /f 1,2 f(1,2) (forced autoparen) ,f 1 2 f("1","2") ;f 1 2 f("1 2") Remember: TAB completion works in many contexts, not just file namesor python names.

Make a table of the Magic commands


In [16]:
#load the SVG Table classes
%run SVG_Table_Classes #used instead of import during editing (not needed a second time but left in for clarification)
#Set the table lengths
stops = [32+4+5,65+8+10]

#Build the SVG text

#Make a tempory dictionary of the magic heading entries
magic_dict = Or_dict()
magic_dict[heading[-1]]=Quick_ref_dic[heading[-1]] 
entries = list(magic_dict[heading[-1]].keys())

#First table
Table_dict = Or_dict()
magic_dict_list = Or_dict()
for i in range(0,stops[0]): #grab some of the magic entries
    magic_dict_list[entries[i]] = magic_dict[heading[-1]][entries[i]]
#make a table dictionary to display
Table_dict[heading[-1]]=magic_dict_list

width=585
Left_column_width=145
display_text, right, bottom = quick_dict_to_SVG(Table_dict, width=width, L_width=Left_column_width)
Bottom = bottom #keep track of the bottom of the view port
running_text = display_text #start a running total of the display text

#Second table
Table_dict = Or_dict()
magic_dict_list = Or_dict()
for i in range(stops[0],stops[1]): #grab some of the magic entries
    magic_dict_list[entries[i]] = magic_dict[heading[-1]][entries[i]]
#make a table dictionary to display
Table_dict["Continuation"]=magic_dict_list
Table_dict["Continuation"]['_type_']="continuation"
 
width=600
Left_column_width=110#95
display_text, right, bottom = quick_dict_to_SVG(Table_dict, width=width, L_width=Left_column_width, x_location=right+10)
Bottom =max(Bottom , bottom) #keep track of the bottom of the view port
running_text += display_text #Add the diplay text to the running total of text

#Third table
Table_dict = Or_dict()
magic_dict_list = Or_dict()
last = len(entries)
for i in range(stops[1],last): #grab some of the magic entries
    magic_dict_list[entries[i]] = magic_dict[heading[-1]][entries[i]]
#make a table dictionary to display
Table_dict["Continuation"]=magic_dict_list
Table_dict["Continuation"]['_type_']="continuation"

width=575
Left_column_width=90
display_text, right, bottom = quick_dict_to_SVG(Table_dict, width=width, L_width=Left_column_width, x_location=right+10)
Bottom =max(Bottom , bottom) #keep track of the bottom of the view port
running_text += display_text#Add the diplay text to the running total of text

Text=Set_SVG_view(right, Bottom, running_text)

Save and display the results...


In [17]:
#Save the result

with open("Magic_only.svg",'w') as f:
    f.write(Text)

#Display the results
SVG(Text)


Out[17]:
The following magic functions are currently available: %alias Define an alias for a system command. %alias_magic %autocall Make functions callable without having to type parentheses. %automagic Make magic functions callable without having to type the initial %. %autosave Set the autosave interval in the notebook (in seconds). %bookmark Manage IPython's bookmark system. %cat Alias for `!cat` %cd Change the current working directory. %clear Clear the terminal. %colors Switch color scheme for prompts, info system and exception handlers. %config configure IPython %connect_info Print information for connecting other clients to this kernel %cp Alias for `!cp` %debug %dhist Print your history of visited directories. %dirs Return the current directory stack. %doctest_mode Toggle doctest mode on and off. %ed Alias for `%edit`. %edit Bring up an editor and execute the resulting code. %env Get, set, or list environment variables. %gui Enable or disable IPython GUI event loop integration. %hist Alias for `%history`. %history %killbgscripts Kill all BG processes started by %%script and its family. %ldir Alias for `!ls -F -o --color %l | grep /$` %less Show a file through the pager. %lf Alias for `!ls -F -o --color %l | grep ^-` %lk Alias for `!ls -F -o --color %l | grep ^l` %ll Alias for `!ls -F -o --color` %load Load code into the current frontend. %load_ext Load an IPython extension by its module name. %loadpy Alias of `%load` %logoff Temporarily stop logging. %logon Restart logging. %logstart Start logging anywhere in a session. %logstate Print the status of the logging system. %logstop Fully stop logging and close log file. %ls Alias for `!ls -F --color` %lsmagic List currently available magic functions. %lx Alias for `!ls -F -o --color %l | grep ^-..x` %macro Define a macro for future re-execution. It accepts ranges of history, %magic Print information about the magic function system. %man Find the man page for the given command and display in pager. %matplotlib %mkdir Alias for `!mkdir` %more Show a file through the pager. %mv Alias for `!mv` %notebook %page Pretty print the object and display it through a pager. %pastebin Upload code to Github's Gist paste bin, returning the URL. %pdb Control the automatic calling of the pdb interactive debugger. %pdef Print the call signature for any callable object. %pdoc Print the docstring for an object. %pfile Print (or run through pager) the file where an object is defined. %pinfo Provide detailed information about an object. %pinfo2 Provide extra detailed information about an object. %popd Change to directory popped off the top of the stack. %pprint Toggle pretty printing on/off. %precision Set floating point precision for pretty printing. %profile Print your currently active IPython profile. %prun Run a statement through the python code profiler. %psearch Search for object in namespaces by wildcard. %psource Print (or run through pager) the source code for an object. %pushd Place the current dir on stack and change directory. %pwd Return the current working directory path. %pycat Show a syntax-highlighted file through a pager. %pylab %qtconsole Open a qtconsole connected to this kernel. %quickref Show a quick reference sheet %quickref_text Return the quickref text to be assigned to a variable %recall Repeat a command, or get command to input line for editing. %rehashx Update the alias table with all executable files in $PATH. %reload_ext Reload an IPython extension by its module name. %rep Alias for `%recall`. %rerun Re-run previous input %reset Resets the namespace by removing all names defined by the user, if %reset_selective Resets the namespace by removing names defined by the user. %rm Alias for `!rm` %rmdir Alias for `!rmdir` %run Run the named file inside IPython as a program. %save Save a set of lines or a macro to a given filename. %sc Shell capture - run shell command and capture output (DEPRECATED use !). %set_env Set environment variables. Assumptions are that either "val" is a %store Lightweight persistence for python variables. %sx Shell execute - run shell command and capture output (!! is short-hand). %system Shell execute - run shell command and capture output (!! is short-hand). %tb Print the last traceback with the currently active exception mode. %time Time execution of a Python statement or expression. %timeit Time execution of a Python statement or expression %unalias Remove an alias %unload_ext Unload an IPython extension by its module name. %who Print all interactive variables, with some minimal formatting. %who_ls Return a sorted list of all interactive variables. %whos Like %who, but gives some extra information about each variable. %xdel Delete a variable, trying to clear it from anywhere that %xmode Switch modes for the exception handlers. %%! Shell execute - run shell command and capture output (!! is short-hand). %%HTML Alias for `%%html`. %%SVG Alias for `%%svg`. %%bash %%bash script magic %%capture %%debug %%file Alias for `%%writefile`. %%html Render the cell as a block of HTML %%javascript Run the cell block of Javascript code %%js Run the cell block of Javascript code %%latex Render the cell as a block of latex %%perl %%perl script magic %%prun Run a statement through the python code profiler. %%pypy %%pypy script magic %%python %%python script magic %%python2 %%python2 script magic %%python3 %%python3 script magic %%ruby %%ruby script magic %%script %%sh %%sh script magic %%svg Render the cell as an SVG literal %%sx Shell execute - run shell command and capture output (!! is short-hand). %%system Shell execute - run shell command and capture output (!! is short-hand). %%time Time execution of a Python statement or expression. %%timeit Time execution of a Python statement or expression %%writefile

In [11]:
%quickref

In [ ]: