[IPython-dev] Ability to Create other Windows

John Omernik john at omernik.com
Tue Feb 10 10:43:33 EST 2015


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