question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reflecting trait changes in JavaScript back to Python

See original GitHub issue

Hi all!

I’ve got a custom widget with some traits. This widget is basically a live stream, so I’m getting the data into JavaScript directly. I then programatically update a trait with this.model.set('mytrait', some_value). This works great and triggers the corresponding change:mytrait event.

What I’d really like would be for a user in a notebook to be able to get the value of mytrait from Python. Ideally, this would only do the serialization when the user calls widget.mytrait in Python. Right now, if I do that, all I get is the initial default value set in Python (None).

I thought this.touch() or this.model.save_changes() would help, but they did not. So how can I set a model value programatically in JavaScript and have the user in Python able to get that value on demand?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
jasongroutcommented, Nov 1, 2017

And his work with the mediastream widget: https://github.com/jupyter-widgets/ipywidgets/pull/1685

1reaction
pbugnioncommented, Oct 30, 2017

Is the serialization from JavaScript to Python done lazily then

Both the Python model and the JavaScript model maintain their own copy of the state. Any state change is propagated by passing events. Changes in Python are pushed to the browser straightaway, and changes in the browser are pushed to the Python side using model.touch().

When you call widget.mytrait, this just looks at the current value of the mytrait attribute in the Python side. It doesn’t call out to the JavaScript.

For a fairly simple example, I suggest looking at the implementation of checkboxes:

When the user changes the value trait of a checkbox Python-side, a websocket message like this is sent to the browser:

{state: {value: true}, method: "update"}

When the user clicks on the checkbox in the browser (line 116), the corresponding value attribute in the browser-side model is updated. The model is then touched, which causes it to propagate its updated state back to the Python by sending this message:

{method: "update", state: {value: false}}

You can view the websocket frames in your browser console (at least on Chrome).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Widget Events — Jupyter Widgets 7.6.2 documentation
Widget properties are IPython traitlets and traitlets are eventful. To handle changes, the observe method of the widget can be used to register...
Read more >
Reflecting changes in class for back/forward - Stack Overflow
You have to make a function that selects the tabs for you and then call it from $.address.change event. $.address.change(function (e) { var ......
Read more >
A Brief History of JavaScript - Auth0
We take a look at the evolution of JavaScript, arguably one of the most important languages of today, and tomorrow.
Read more >
Reflected XSS Vulnerability in Depth - GeeksforGeeks
In this article, we will discuss one of the most seen vulnerabilities in web-based applications, which is — Reflected XSS.
Read more >
Ipywidgets with matplotlib - Kapernikov
This function takes two tuples of the form (widget, trait) and links the given traits of the given widgets. In the example below,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found