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

why cannot I get the url in one cell ? #2

Closed
12rambau opened this issue Jan 11, 2022 · 10 comments
Closed

why cannot I get the url in one cell ? #2

12rambau opened this issue Jan 11, 2022 · 10 comments

Comments

@12rambau
Copy link

In the example the workflow is the following :

w = Url()
w
w.url

I tested it on my end and it works as expected.

Now if i try to run:

w = Url()
w.url

in 1 cell, I get ''as a result. Why isn't it working in one cell (and by extension in a python script) ?

@davidbrochart
Copy link
Owner

Because you need to instantiate the widget. That is what this does:

w

This cannot work in a Python script because it is a Jupyter notebook feature.
But there is another way:

url = None
async def foo():
    global url
    w = Url()
    display(w)
    url = await w.get_url()
asyncio.ensure_future(foo())

url will be set after some time it your environment is async.

@12rambau
Copy link
Author

so using display is not instanciating anything correct ?

@davidbrochart
Copy link
Owner

It's displaying the widget w, then you await the URL.

@12rambau
Copy link
Author

ok, then If i want to get the url in a notebook let's say like this one:

w = Url()
w
# do stuff with url 
w.url

How can I launch it in voila ?

@davidbrochart
Copy link
Owner

Voilà is another beast, it will launch all the cells one after the other, so the URL might not be retrieved from the front-end when you do w.url. Something like that could work:

url = None
async def foo():
    global url
    w = Url()
    display(w)
    url = await w.get_url()
await asyncio.ensure_future(foo())

@12rambau
Copy link
Author

12rambau commented Jan 11, 2022

Voilà is another beast

Unfortunately for me it's the beast I need to focus on ;-)

Something like that could work

arf ok so I tried your last piece of code and even in a jupyter notebook it awaits forever. It seems that the widget cannot be instanciated if I don't leave the cell

@davidbrochart
Copy link
Owner

@12rambau
Copy link
Author

thank you for the links, I'll explore the solutions and share what work for me before closing the issue

@artttt
Copy link

artttt commented Jan 20, 2023

Take a look at https://github.com/Kirill888/jupyter-ui-poll
I believe this will solve this issue

edited to add example using this package now i have my install issues sorted out.

from ipyurl import Url
from IPython.display import display
from jupyter_ui_poll import run_ui_poll_loop

def window_url():
    """returns the url for the window that jupyter is running in"""
    w = Url()
    display(w)
    func = lambda: None if w.url == "" else w.url
    url = run_ui_poll_loop(func)
    return url

out of interest the same can also be done with ipyleaflet providing the url

import ipyleaflet
from IPython.display import display
from jupyter_ui_poll import run_ui_poll_loop


def window_url():
    """returns the url for the window that jupyter is running in"""

    m = ipyleaflet.Map(zoom=0)
    m.layout.display = "none"
    display(m)
    func = lambda: None if m.window_url == "" else m.window_url
    url = run_ui_poll_loop(func)
    m.close()
    return url

@12rambau
Copy link
Author

thanks @artttt for the suggestions. I think with this + @davidbrochart insight, I have everything I need to make it work from my side.

@12rambau 12rambau closed this as not planned Won't fix, can't repro, duplicate, stale Feb 19, 2023
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

3 participants