Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run javascript from widget callback #2458

Open
DYSpider13 opened this issue Jun 26, 2019 · 10 comments
Open

Run javascript from widget callback #2458

DYSpider13 opened this issue Jun 26, 2019 · 10 comments

Comments

@DYSpider13
Copy link

Hello,

Is it possible to execute a javascript inside a widget callback (a button for instance) ? If not, is there an alternative for that ?

Best

@hainm
Copy link

hainm commented Jun 26, 2019

I think so, using from IPython.display import Javascript

from IPython.display import display, Javascript

Javascript("console.log('hello')")

@SylvainCorlay
Copy link
Member

I think so, using from IPython.display import Javascript

Yes, although this kind of javascript injection is not allowed in jupyterlab.

Is it possible to execute a javascript inside a widget callback (a button for instance)

We have a solution for this that was used in ipysheet using web workers to sandbox the unsafe js, although the executed javascript does not have access to the DOM of the main page or the js context of the main page.

@hainm
Copy link

hainm commented Jun 26, 2019

Yes, although this kind of javascript injection is not allowed in jupyterlab.

@SylvainCorlay which lab version that does not allow this? It work fine with lab 0.35

ok

@SylvainCorlay
Copy link
Member

OK, my information is outdated. That used to not be allowed, which seems to be a good thing to me.

@DYSpider13
Copy link
Author

Thank you all. Using Javascript("console.log('hello')") did not work, I had to put it into display like this:

display(Javascript("console.log('hello')"))

Any idea why ?

@SylvainCorlay
Copy link
Member

@DYSpider13 I really do not recommand using this though. Injecting javascript is a big security red flag and may be disabled in many deployments of Jupyter.

@renefritze
Copy link

It looks like Bokeh has a solution for this here

Me personally I would prefer a solution closer to plain ipywidgets since I would have no actual use for Bokeh besides the CustomJS callbacks.

Somewhat related would be #1109

@jasongrout
Copy link
Member

Me personally I would prefer a solution closer to plain ipywidgets since I would have no actual use for Bokeh besides the CustomJS callbacks.

You can with the display Javascript trick above. That's a generic Jupyter thing, nothing to do with ipywidgets (living below the layer of ipywidgets).

To Sylvain's point - a problem with executing javascript is that it most often makes your code only work in a single frontend (and often only a single major version of that frontend), so it has limited usefulness. The approach of ipywidgets in general is to get desired functionality into the frontend through whatever the frontend extension system there is and then triggering the functionality through a message to the frontend.

@sruthiiyer
Copy link

How to substitute variable value inside code tag of HTML in ipywidgets

`object_fit = [A.ISONoise(always_apply=False, p=1.0, intensity=(0.1, 0.5), color_shift=(0.01, 0.05))]

h = HTML(value='<{size}>ISONoise(always_apply=False, p=1.0, intensity=(0.1, 0.5), color_shift=(0.01, 0.05)) </{size}>'.format(size='h4'))`

The value inside code keeps verying. So, how to substitute it. I'm new to HTML

@sruthiiyer
Copy link

How to substitute variable value inside code tag of HTML in ipywidgets

`object_fit = [A.ISONoise(always_apply=False, p=1.0, intensity=(0.1, 0.5), color_shift=(0.01, 0.05))]

h = HTML(value='<{size}>ISONoise(always_apply=False, p=1.0, intensity=(0.1, 0.5), color_shift=(0.01, 0.05)) </{size}>'.format(size='h4'))`

The value inside code keeps verying. So, how to substitute it. I'm new to HTML

I want substitute the object_fit variable inside the code tag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants