[IPython-dev] Editor widget and data persistence

Pablo Winant pablo.winant at gmail.com
Fri Oct 24 15:54:47 EDT 2014


I am not sure about that, I´m using the widget interface to edit
structures, which have many fields only some of them being text (like
https://www.dropbox.com/l/4TJTCjPMiVnZ2WTfIO1BUp). I also plan to have some
kind of consistency checks for user entered values.
In the meantime I draw some inspiration from this thread
http://python.6.x6.nabble.com/API-to-Modify-Notebook-Metadata-td5071535.html
and am now trying to call a javascript code to modify and read the notebook
metadata. It’s not completely working yet. I guess I’m having trouble to
control when the javascript code is called. Ideally, I’d like to query the
state of the javascript intepreter structure and get the result within the
same Python function. Here are the two functions:

def set_metadata(val):

    # serialize data
    import json
    valstring = json.dumps(val)

    from IPython.display import Javascript, display
    js = """
    IPython.notebook.metadata.modfile = '{}';
    """.format(valstring)

    display ( Javascript(js) )

    return None

and

def get_metadata():

    from IPython.display import Javascript, display
    import json

    js = """
    var command = "meta = " + JSON.stringify(IPython.notebook.metadata);
    var kernel = IPython.notebook.kernel;
    kernel.execute(command);
    """
    display(Javascript(js)) # this seems to be evaluated after the
function has returned so doesn't give the good result

    valstring = globals()['meta']['modfile']
    return json.loads(valstring)

In[10] set_metadata({'arbitrary': 'data', 0:1})
In[11] resp = get_metadata()   # incorrect or no result
In[12] resp = get_metadata()   # correct result

Best,

Pablo
​

On Fri, Oct 24, 2014 at 2:42 PM, Brian Granger <ellisonbg at gmail.com> wrote:

> I agree with @takluyver that in this particular case a widget is
> probably the wrong answer. I think that the `%%writefile` magic of the
> upcoming text editor page is a much better solution...
>
> Cheers,
>
> Brian
>
> On Wed, Oct 22, 2014 at 4:56 PM, Adam Hughes <hughesadam87 at gmail.com>
> wrote:
> > If you have a notebook that calls the GUI, you can launch it in a second
> > notebook just using the %run magic as well.  IE, if you had:
> >
> > mywidget.ipynb
> >
> > Then you can launch it in a separate notebook with:
> >
> > %run mywidget.ipynb
> >
> > On Wed, Oct 22, 2014 at 3:42 PM, Pablo Winant <pablo.winant at gmail.com>
> > wrote:
> >>
> >> Thank you Thomas.
> >>
> >> On Wed, Oct 22, 2014 at 12:28 PM, Thomas Kluyver <takowl at gmail.com>
> wrote:
> >>>
> >>> Hi Pablo,
> >>> IPython.html.nbextensions.> I wonder if it is possible to save the
> state
> >>> of these widgets when saving the notebook
> >>>
> >>> Currently, no. We're working on ways to do that, but it turns out to be
> >>> more complicated than it seems.
> >>>
> >>> > More specifically, I'd like to use the text editor widget to edit a
> >>> > text file that comes with a notebook
> >>>
> >>> It's possible to do this with widgets, but I don't think it's the best
> >>> fit for the problem. It sounds like what you want is the %%writefile
> cell
> >>> magic. Just make a code cell containing the text you want, with
> %%writefile
> >>> filename.txt at the top. When you run it, it will save that content to
> the
> >>> file.
> >>>
> >>
> >> Actually my usecase is trying to provide users with an easy interface to
> >> edit the content of simple structure that is also serializable as a
> >> JSON/YAML file. I would be happy if I could find way to store
> information
> >> produced by the code in the notebook and then read it back. Maybe,
> there is
> >> a way to edit cell of notebook metadata from the notebook itself ?
> >>
> >>
> >>>
> >>> We are also planning to put a text editor component inside the notebook
> >>> server, but that's not quite ready yet.
> >>>
> >>> > the IPython widgets are becoming one of the very few alternatives for
> >>> > python web-guis
> >>>
> >>> Yes, and this is a much larger scope than the widget architecture was
> >>> really designed for. We're trying to work out how to handle this.
> >>
> >>
> >> I can smell some big architectural work here... Good luck with that !
> >>
> >> Pablo
> >>
> >>
> >>>
> >>>
> >>> Thanks,
> >>> Thomas
> >>>
> >>> On 22 October 2014 07:58, Pablo Winant <pablo.winant at gmail.com> wrote:
> >>>>
> >>>> Hi,
> >>>>
> >>>> The new widgets are absolutely wonderful !
> >>>>
> >>>> I wonder if it is possible to save the state of these widgets when
> >>>> saving the notebook. More specifically, I'd like to use the text
> editor
> >>>> widget to edit a text file that comes with a notebook. That would
> kill two
> >>>> birds with one stone for me: avoid the need to ship a separate file
> with the
> >>>> notebook (btw, is there already a canonical way to do that ?), and be
> able
> >>>> to edit it from within the notebook. Are there plans in this
> direction ? Any
> >>>> idea about how I could implement that ?
> >>>>
> >>>> A somewhat related question: the IPython widgets are becoming one of
> the
> >>>> very few alternatives for python web-guis. Do you think it would be
> possible
> >>>> to use it for a relatively simple application and hide the notebook
> from
> >>>> user's sight so that it looks like an independent application ?
> >>>>
> >>>> Best,
> >>>>
> >>>> Pablo
> >>>>
> >>>> _______________________________________________
> >>>> IPython-dev mailing list
> >>>> IPython-dev at scipy.org
> >>>> http://mail.scipy.org/mailman/listinfo/ipython-dev
> >>>>
> >>>
> >>>
> >>> _______________________________________________
> >>> IPython-dev mailing list
> >>> IPython-dev at scipy.org
> >>> http://mail.scipy.org/mailman/listinfo/ipython-dev
> >>>
> >>
> >>
> >> _______________________________________________
> >> IPython-dev mailing list
> >> IPython-dev at scipy.org
> >> http://mail.scipy.org/mailman/listinfo/ipython-dev
> >>
> >
> >
> > _______________________________________________
> > IPython-dev mailing list
> > IPython-dev at scipy.org
> > http://mail.scipy.org/mailman/listinfo/ipython-dev
> >
>
>
>
> --
> Brian E. Granger
> Cal Poly State University, San Luis Obispo
> @ellisonbg on Twitter and GitHub
> bgranger at calpoly.edu and ellisonbg at gmail.com
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20141024/ea66aced/attachment.html>


More information about the IPython-dev mailing list