Webbrowser module

Without having Jupyter notebook installed and just using the web version(at least that is what I call it) will the webbrowser module work to open a new tab? I am asking because I want to put on an introductory course for my work on Python and wanted to see what is possible without having everyone install anaconda.

Specifically the two line items I can’t seem to get to work on a notebook are below. They of course run without issue on my localhost using jupyter notebook.

import webbrowser

Hi Jareda,
When you say ‘just using the web version’, I wonder if you mean sessions served via MyBinder that can be launched from https://jupyter.org/try and other places? If that is the case, using ‘Binder’ or ‘MyBinder’ will help you search here better to find help.

I suspect that is what you are describing and running live training sessions is indeed one of the main use cases for MyBinder-served sessions, see here. You’ll need to do some preparation to maximize your use unless you want to be starting from square one entirely and make all notebooks to use. If the latter, then you can use the launches available at https://jupyter.org/try . Examples of the former:

And more can be seen here.

There are some things to be aware of though relying on that, as discussed here each repository is limited to 100 live instances, as discussed here. And if the session isn’t doing something that keeps the kernel active (busy) for more than 10 minutes, it will be culled and the user will need to start a new session. Some discussion of avoiding inactive here. Just typing in a cell isn’t keeping the kernel busy. This isn’t usually a problem for coding-heavy sessions; however, it can require some attention when there will be long exposition between running code.

Alternatively you could mean running sessions from your own JupyterHub? In that case, you would need to set up the hub to have the needed packages available to everyone. This would also obviate the need for installations.

What do you need to happen with opening the web browser to open a new tab? Depending on what you hope to do, you may just need to approach it differently to accomplish the same or similar outcome. If you set up a repo (or user environment on a JupyterHub) with sample notebooks, you can click and navigate them just like surfing the web. For example, if you go here and launch a Binder session using the badge, the first page that comes up is an index of some available notebooks. Clicking on those notebooks opens a new tab with those notebooks actively running. If you just want to expose a webpage, as part of the demo, you can use a frame within the Jupyter notebook itself.

import IPython
url = "https://www.ncbi.nlm.nih.gov/pubmed/23435085"
iframe = '<iframe src=' + url + ' width=700 height=350></iframe>'

Note that it will complain, but that method currently works whereas the one in the ‘Rich Display System’ notebook (available when launching ‘Try Classic Notebook’ from https://jupyter.org/try) under the section ‘External sites’ doesn’t work for me in Chrome.

1 Like

In addition to the nice comprehensive answer above, to directly answer your question here: using the python webbrowser module will only work on your local computer, it won’t work from a remote computer.


Thank you, jasongrout and fomightez.

fomightez, the short and quick training was going to be showing my team how they could use python to automate opening up of separate tabs in a browser, so while the iframe example you gave will work, the goal was that they could take this with them and execute this if they want to in the future, but really now that I think about it, it is really only useful if they can execute it outside of opening up jupyter so I can create an executable that they can run and during the demo just focus on the code I am creating.

Thank you again for the response.

1 Like

Because it will likely cause the target audience more confusion than it is worth (unless maybe attempted after a group-viewing and discussion of the movie Inception), I am only mentioning this in case someone else comes across this later and has a legitimate use (and because it is soooo cool what is possible with Binder :slight_smile: )…
If you really want to have them programmatically open browser tabs themselves with Python, an alternative that uses Jupyter and that wouldn’t require them to install Anaconda would be to guide them through opening a linux desktop within their browser and running your webbrowser.open()-based code, @Jareda . Illustrating how that is done:
If you surf in your browser to here and click on the launch binder, after it spins up you’ll get a linux desktop. At the prompt window that first comes up click on ‘Use default config’ and then at the bottom toolbar click on the ‘terminal’ icon (second from left). In the terminal that comes up, you can type ipython to get an ipython console and you can import webbrowser and run webbrowser.open() commands. Firefox will be launched with the called URL as a tab, and with each subsequent call from the console opening as additional tabs.

@fomightez, this 100% works for what I need. Thank you for this. I had no idea you could freely spin up a VM. You have blown my mind. Thank you for the education!

1 Like

I seem to have altered the link or something that I referenced in the last paragraph of my post. The new link is GitHub - fomightez/jupyter-desktop-server: Run a Linux Desktop on a JupyterHub . Go there and then follow the directions I had: