[IPython-dev] Ability to Create other Windows

John Omernik john at omernik.com
Wed Feb 11 11:17:55 EST 2015


So after some hacking around this is what I came up with. I'd be
interested on feed back if this is doing something fundamentally
"wrong" I was able to achieve what I was looking for though, which was
a pandas outputted data frame in a new window so I could resize and
scroll through .


from IPython.display import Javascript
from IPython.html import widgets # Widget definitions

 # Used to display widgets in the notebook
from IPython.display import display, HTML, display_pretty,
display_html, display_jpeg, display_png, display_json, display_latex,
display_svg

results = {}
@register_cell_magic
def sql(line, cell):
    global results
    pd.set_option('max_colwidth', 100000)
    pd.set_option('display.max_rows', None)
    out = runSQL(cell)
    df = pd.DataFrame(out)
    # Assume this is a working happy Pandas Dataframe
    gridhtml = df.to_html()
    window_options = "toolbar=no, location=no, directories=no,
status=no, menubar=no, scrollbars=yes, resizable=yes, width=780,
height=200, top=0, left=0"
    fmd5 = md5(gridhtml) #I've imported the hashlib hexdigest into my own md5()
    base = """var win = window.open("", "&~&", "*~*");
    win.document.body.innerHTML = `%~%`;
    """
    JS = base.replace('%~%', gridhtml) # Put the results in the output
    JS = JS.replace('&~&', fmd5) # Set the title to be the md5
    JS = JS.replace('*~*', window_options) # Include the window options
    results[fmd5] = JS # Store the results in a global dictionary by
using an MD5 key


    button = widgets.Button(description="Results")
    button.tooltip = fmd5 # Set the tool tip to be the Md5 so we can
"pass" this to the call back
    button.on_click(on_button_clicked) # Set the call back

    display(button)


def on_button_clicked(b):
    global results
    fmd5 = b.tooltip # get the md5 from the tooltop
    createwindow(results[fmd5]) #Call the create window

def createwindow(JS):
    j = Javascript(JS)
    display(j)

On Tue, Feb 10, 2015 at 11:28 AM, John Omernik <john at omernik.com> wrote:
> So here is an example of it working without the button, just having
> the window pop up when the the magic is run. So, the idea here is when
> the data query is loaded, the button is added, and every time the
> button is pressed, the window can be reopened. Now it's just a matter
> of playing with the widgets... how do I pass a variable to the button
> on_click event? :)
>
>
> On Tue, Feb 10, 2015 at 9:43 AM, John Omernik <john at omernik.com> wrote:
>> This is a weird question I think.
>>
>> I am trying to write a magic function that does something simple with
>> a widget. Basically I want to be able to query a database and when the
>> query is returned have a button that "contains" results.  Like this:
>>
>>
>> %%sql
>> select * from table where field = 1
>>
>>
>> When that is is done, a Button widget appears, with the word results
>> on it. The data is "in there" likely a panda dataframe to_html.  But
>> basically I'd like to be able to, when I press the button have a new
>> window spawned (maybe with a a javascript new window) That has the
>> data in a different window that I can move to a different screen and
>> look at as I am writing more code in my iPython notebooks. (Further
>> enhancements may come out of that down the line)
>>
>> What I have is below.  What happens is when I add a variable to the
>> callback function it seems to click it right away, and there are no
>> results.  I am trying a bunch of stuff here, and still hacking on it,
>> but I thought I'd toss it out here. I am using Jupyter (iPython 3)
>> with Python2 Shell.
>>
>> Note, once the new window is filled with data, and good to go, iPython
>> has no more connection (back and forth data) Basically it's a fly away
>> window, it can stay open, closed etc, it's  independent of the iPython
>> (in my head at least)
>>
>> @register_cell_magic
>> def sql(line, cell):
>>     pd.set_option('max_colwidth', 100000)
>>     pd.set_option('display.max_rows', None)
>>     out = runSQL(cell)
>>     df = pd.DataFrame(out)
>>     # Assume this is a working happy Pandas Dataframe
>>     gridhtml = df.to_html()
>>
>>     JS = """
>> var newwindow=window.open();
>> newwindow.document.open()
>> newwindow.document.write(`%~%`);
>> newwindow.close();
>> """
>>     JS = JS.replace('%~%', gridhtml)
>>
>>     button = widgets.Button(description="Results")
>>     display(button)
>>
>>     button.on_click(on_button_clicked(button, JS))
>> def on_button_clicked(b, JS):
>>     print "I've been clicked"
>>     createWindow(JS)
>> def createWindow(JS)
>>     j = Javascript(JS)
>>     display(k)



More information about the IPython-dev mailing list