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

javascript-python communication not working when "Run All" cells #1622

Closed
epinadev opened this issue Jul 18, 2016 · 8 comments
Closed

javascript-python communication not working when "Run All" cells #1622

epinadev opened this issue Jul 18, 2016 · 8 comments

Comments

@epinadev
Copy link

epinadev commented Jul 18, 2016

Hi everyone, I have a notebook with this:

screenshot_2016_07_18_16_03_56

It turns out that it works well (the last print for num shows 43) but only when I execute this cell by cell. But if I execute it by clicking the "Run All" menu, or by calling in a js file the "notebook.execute_all_cells()" function, the num variable is never modified from the js code.

The idea is that I have some special notebooks where I need to execute every cell when it loads, but some cells depend on a python variable filled from javascript. I've tried to delay the last cell execution time enough so the previous cell is finished, but since the cells execution is linear, I don't think this could solve my problem.

Can someone point me a way on how to do this?

@takluyver
Copy link
Member

the num variable is never modified from the js code.

It probably is, but only after the cell printing it has run.

Depending on what you need it for, you may be able to define a function in Python that receives the value, and then call that function from the Javascript.

@epinadev
Copy link
Author

epinadev commented Jul 19, 2016

It probably is, but only after the cell printing it has run.

No, actually is not modified, because in that case, after every cell is executed, the modification should be available let's say on a new cell, or if I re-execute the last cell, but it isn't. And calling a defined python function from javascript, in my case, does not work either, because the data I need comes only from javascript (is a json object), and I'll have to pass it as a parameter to the function, which is the same thing that storing it into a python var.

*Update: *
@takluyver: In the example I did, you're right, the num var is being modified. But my case is a little more complex, but I've realized that what I'm doing is not too efficient since it is not a simple number, but it could be billions of data rows, so I'll have to drop this solution and close this issue. Thanks anyway!

@minrk minrk added this to the no action milestone Nov 23, 2016
@michelmetran
Copy link

Hello!,
I have the same problem.
Were you able to resolve it in any way?

When I try to apply a "Run All" ... it interrupts the script when it reads the magic line %%javascript.

Tks!

@TimoFriedri
Copy link

TimoFriedri commented Apr 15, 2020

Bump,

I have the same issue. I get the Notebook name in a Javacript function for archiving of the notebook as html. But for me also only the cell by cell execution works.

It is the same behaviour for code like this:

display.Javascript('IPython.notebook.kernel.execute("NB_NAME = " + "\'"+IPython.notebook.notebook_name+"\'");')

@adamraudonis
Copy link

I'm having the exact same issue! Any idea how this can be fixed?

@siwing
Copy link

siwing commented Oct 27, 2020

Is there a way to solve this issue now?

@Crypto-Spartan
Copy link

Having the same issue as well. Tried adding this in the cell after my %%javascript

while 'my_var' not in locals():
    await asyncio.sleep(1)

Unfortunately, this didn't work, the code was just in an indefinite await period. Definitely looking for a solution to this.

@kevin-bates
Copy link
Member

I believe this issue is more about ipykernel and its internal state/context than notebook and you might have better traction opening an issue in that repo.

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

No branches or pull requests

9 participants