Launch IPython notebook with selected browser - python

I am trying to start IPython with a non default browser (in my case Firefox)
and thought I could replicate the replicate the script given in this blog
I am on Windows 7
I put the following code in a file say "module.py"
import subprocess
subprocess.call("ipython notebook --no-browser", shell=True)
subprocess.call([r'C:\Program Files (x86)\Mozilla Firefox\Firefox.exe', '-new-tab', 'http://127.0.0.1:8888/'])
However when I run it from the command line
python C:\Users\mugabal\Desktop\module1.py
It execute the first line but not the second one (both lines work fine individually)
My question (in a more general term) how can I launch a process and tell it not to highjack the console window?
I apologize in advance if I have overseen an obvious explanation but I looked both in the subprocess documentation and on this platform
----- UPDATE -----
I should have added that I tried to launch IPython with selected browser but could not figure out how to get it work
>ipython notebook --browser='C:\Program Files (x86)\Mozilla Firefox\Firefox.exe'
...
[NotebookApp] The IPython Notebook is running at: http://127.0.0.1:8888/
...
**[NotebookApp] No web browser found: could not locate runnable browser.**
To be precise, the following command in a Windows command prompt window works as expected:
start firefox
but
ipython notebook --browser=firefox
does not work (same error as above).

I had the same problem on windows and got it work this way:
Create a config file with command
ipython profile create default
Edit ipython_notebook_config.py file, search for line
#c.NotebookApp.browser =''
and replace it with
import webbrowser
webbrowser.register('firefox', None, webbrowser.GenericBrowser('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe'))
c.NotebookApp.browser = 'firefox'
then it works for me.
Hope it will help you.
JPG

On my mac, I got the following command to use Firefox instead of my default Chrome:
jupyter notebook --browser firefox

This is not a real answer. I just want to share with the less computer savvy what JPG's answer looks like step-by-step. Presumably, on Windows Explorer (screen capture attached below), the file jupyter_notebook_config.py is listed:
In my case, the directory for the file (on top menu of Explorer) was C:\Users\My_name\.jupyter
The second part of the answer can be implemented by simply pasting:
import webbrowser
webbrowser.register('firefox', None, webbrowser.GenericBrowser('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe'))
c.NotebookApp.browser = 'firefox'
in the space on the space seen on the screen capture below, corresponding to the jupyter_notebook_config.py opened within PyCharm:
... only that I set it up to open in Opera:
import webbrowser
webbrowser.register('opera', None, webbrowser.GenericBrowser('C:\\Program Files (x86)\\Opera\\launcher.exe'))
c.NotebookApp.browser = 'opera'

Why not use
--browser=<Unicode> (NotebookApp.browser)
Specify what command to use to invoke a web browser when opening the
notebook. If not specified, the default browser will be determined by the
`webbrowser` standard library module, which allows setting of the BROWSER

I uncomment this line and change into False, not to make ipython notebook open a web browser at start, so we can point the ipython notebook address in an active web browser.
# Whether to open in a browser after starting. The specific browser used is
# platform dependent and determined by the python standard library `webbrowser`
# module, unless it is overridden using the --browser (NotebookApp.browser)
# configuration option.
c.NotebookApp.open_browser = False
Better still, I pin the address in my Firefox to make it active everytime I open the browser.

to make it work with brave and chrome you need to append a %s param that represents the URL. Tested the following on Linux terminal and works great:
jupyter notebook --browser="google-chrome %s"
jupyter notebook --browser="brave-browser %s"
firefox doesn't need %s
jupyter notebook --browser=firefox

I set the environment variable BROWSER to the the executable of the browser (in my case Google Chrome), and Ipython Notebook started in the browser I liked.
PS H:\> $env:BROWSER = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
PS H:\> $env:BROWSER
C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
PS H:\>
PS H:\> ipython notebook
2015-02-19 14:05:01.690 [NotebookApp] Using existing profile dir: C:\\Users\\abc\\.ipython\\profile_default'
2015-02-19 14:05:01.832 [NotebookApp] Using MathJax from CDN: http://cdn.mathjax.org/mathjax/latest/MathJax.js
2015-02-19 14:05:01.901 [NotebookApp] The port 8888 is already in use, trying another random port.
2015-02-19 14:05:01.908 [NotebookApp] Serving notebooks from local directory: H:\
2015-02-19 14:05:01.908 [NotebookApp] 0 active kernels
2015-02-19 14:05:01.910 [NotebookApp] The IPython Notebook is running at: http://localhost:8889/
2015-02-19 14:05:01.910 [NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

When you execute the below code in command prompt it gives the result link which you can copy in any browser to open Jupiter notebook.
jupyter notebook --browser firefox

I tried what JPG and norfeldt suggested. It worked perfectly on my Windows 7 computer. Here is a copy of the modified section of ipython_notebook_config.py (located under C:\Users\'your username'\.ipython to use Safari as the default browser for notebook. As norfeldt said, please notice the u before 'C:\...)
# c.NotebookApp.certfile = u''
import webbrowser
webbrowser.register('safari', None, webbrowser.GenericBrowser(u'C:\\Program Files (x86)\\Safari\\safari.exe'))
c.NotebookApp.browser = 'safari'

Without coding, you can just set your default browser to Chrome or Firefox etc. It works for my windows system.

Related

Is there a way to make a desktop shortcut for Jupyter Notebook?

Currently, I access Jupyter notebook by opening Anocanda Navigator then opening the notebook link. I have tried to bookmark the webpage but most of the time it doesn't work since it is a local page and I think some of the files are still "running" (I am clearly not an expert here and well aware of how stupid this may sound). Is there any way to have Jupyter Notebook as its own desktop link without having to open it via Anaconda?
For Windows, you can create a shortcut pointing to the following path:
%PY_HOME%\python.exe %PY_HOME%\cwp.py %PY_HOME% %PY_HOME%\python.exe -m jupyter notebook
Where %PY_HOME% is my environment variable pointing to the Anaconda folder (example: C:\Users\myuser\AppData\Local\Continuum\anaconda3). It's useful to use the environment variable because you may run out of characters.
If you want to run a especific environment:
%PY_HOME%\python.exe %PY_HOME%\cwp.py %PY_HOME%\envs\myenvironment %PY_HOME%\envs\myenvironment\python.exe -m jupyter notebook
You could make a bat file like so and put it in your desktop
start_jupyter.bat
:: enable anaconda environment
set root=C:\ProgramData\Anaconda3
call %root%\Scripts\activate.bat %root%
:: start jupyter
call cd "C:\Users\<USER>\<JUPYTER DOCUMENT ROOT>"
call jupyter notebook
pause
If you are on windows, open the search function on your nav bar on the bottom. Type "Jupyter Notebook". It should populate with your notebook. Then, right click on it and select "Open file location". This will take you to the file explorer. Finally, on the Notebook application that you use to run the Jupyter Notebook, right click "Copy". Then go to your desktop, and right click. Select "Paste shortcut", and Eureka! You should have the application shortcut on your desktop.
you can launch your jupyter notebook from remote server by trying on cmd prompt:
jupyter notebook --no-browser --port=8080
or run the following command to launch with default port:
jupyter notebook --no-browser
You can create a text file on your desktop and paste the following line into it:
jupyter notebook
Then save the file as Jupyter Notebook.bat and use it as a shortcut for Jupyter Notebook.
On linuxmint
<default_path_to_launcher> = /home/editUserName/anaconda3/bin/jupyter-notebook
<launch_in_a_specific_folder> = /home/editUserName/anaconda3/bin/jupyter-notebook --notebook-dir /editPathToDirectory/
By editing main menu
1.1 Right click menu
1.2 Select Configure
1.3 Open Menu tab
1.4 In Layout and content, at the last, click Open the menu editor
1.5 Select any folder where you want to place the launcher
1.6 In the right sidebar, select New Item
1.7 In the Launcher Properties window, enter following
- Name = Jupyter Notebook
- Command = <path to launcher> or you can <launch in a specific folder>
- Comment = Open Jupyter Notebook
- click Icon, search jupyter, select any
- OK
1.8 Now open menu, go to the folder, et voila Jupyter Notebook, click it
1.9 If it launches, then open menu, go to that folder
1.10 Right click the launcher
- Add to desktop
- Add to panel
- Add to favorites
On desktop
2.1 Right click on desktop
2.2 Select +Create new launcher here
2.1 Follow step 1.7
Note 1: Replace jupyter-notebook with jupyter-lab in commands above, and that will launch jupyter-lab
Note 2: Do not edit the launcher using text editors. In my experience this breaks the launcher. Follow steps 1.1 to 1.5, in the right sidebar, click Properties to edit command or icon. On desktop, right click the launcher, select Properties.
Note 3: For Windows, creating .bat file as per this works on Windows 10. You can even remove call cd line, and modify next line as `call jupyter notebook --notebook-dir /editPathToDirectory/

Change the starting folder of Jupyter

I am having issues with changing the starting folder of Jupyter notebooks, as I would like to run the notebooks on a shared server. I am running Windows 10. I have already tried several of the options online:
I have tried changing the "Start in" property of the jupyter
shortcut: now the shortcut just quickly opens the terminal, then
immediately closes again and nothing more happens.
I have tried running the "jupyter notebook --generate-config"
command, both in and outside python: in the first case it tells me
that jupyter is not a recognised command, and in the second that
there's a syntax error at notebook.
I have made a .bat file with the instructions of the shortcut ('''C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\python.exe
C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\cwp.py
C:\Users\USERNAME\AppData\Local\Continuum\anaconda3
C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\python.exe
C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\Scripts\jupyter-notebook-script.py
Z:\Test''') but it
simply opens the terminal in python, and nothing more.
If I try to launch jupyter from the anaconda interface it works, and I can also start it from the terminal, but not in any other way.
The "jupyter notebook --generate-config" method, explained well in this answer from #marneylc, worked, I just had to start in the Anaconda Promt, rather than in cmd.
I hope this is the right way of answering my own question...

Vpython using Spyder : how to prevent browser tab from opening?

I am using vpython library in spyder. After importing the library when I call simple function like print('x') or carry out any assignment operation and execute the program, immediately a browser tab named localhost and port address opens up and I get the output in console {if I used print function}.
I would like to know if there is any option to prevent the tab from opening and is it possible to make the tab open only when it is required.
PS : I am using windows 10, chrome as browser, python 3.5 and spyder 3.1.4.
There is work in progress to prevent the opening of a browser tab when there are no 3D objects or graph to display. I don't know when this will be released.

How to disable jupyter notebook history

I'm using Jupyter Notebook to code in Python 2.
I'm invoking it as:
c:\python27\scripts\jupyter-notebook --no-browser
At the same time I use IPython console, launched with:
c:\python27\scripts\ipython
The problem I have is that Jupyter history is saved and is mixed with IPython history.
I don't want Jupyter Notebook history at all - is there a way to disable it, while retaining IPython** history?
Platform: win32
Update:
I have tried to use suggested setting digest approach.
But when I enter "c.Session.digest_history_size = 0" to the config, restart notebook, write "print 'next test'" in some cell, restart separate IPython and after pressing up the first thing I get is "print 'next test'".
How can I get rid of it?
See this Jupyter issue on Github for the origin of this solution.
In the Introduction to IPython Configuration using configuration scripts located in your home directory at ~/.ipython/profile_default/ is discussed. This is the relevant directory for the default profile, other similar directories appear if one creates other profiles.
Inside that directory one can include the file ipython_config.py which will run on every usages of IPython. However, the file ipython_kernel_config.py will run upon invocation of an IPython kernel, not when invoking the IPython interpreter itself. One can test this by doing ipython kernel --debug.
Jupyter notebooks use this style of kernel invocation. Therefore including a script ipython_kernel_config.py in the directory ~/.ipython/profile_default/ (assuming the default profile) with the following lines:
# Configuration file for ipython-kernel.
c = get_config()
c.HistoryManager.enabled = False
Should disable the history manager completely when using that style of kernel invocation. Therefore one should not populate command history from Jupyter calls.
Incidentally, the file history.sqlite in that same directory is the command history. So, deleting it or moving it to a different filename will clear the command history buffer.

Open jupyter in firefox window with no decorations

I am using Jupyter in Firefox on OSX. Is there a way I can ensure the notebook opens in a window without any of the toolbars etc of firefox?
I currently use it for julia by running
using IJulia
notebook()
but I guess the kernel should not matter.
Thanks.
Perhaps the following will be useful (untested):
https://support.mozilla.org/en-US/questions/1029353
By the way, you can launch the notebook directly from the terminal using
ipython notebook # IPython version < 4
or
jupyter notebook # Jupyter version >= 4
[
or
ipython notebook --profile julia
if you are running IPython < 3, in which case I recommend upgrading
]
Also note that in Chrome there is a nice mode with the toolbars hidden via
View -> Enter Presentation Mode
(I do not see anything like this in Firefox.)
You can then manually hide the IPython/Jupyter headers with View->Hide toolbar and View->Hide header from within the IPython/Jupyter menu.
In this way you can get the whole screen dedicated to your notebook with (almost) no visual clutter.

Categories