Launch Jupyter Notebook in Safari from Anaconda Navigator? - python

I would like to change the default browser for starting the Jupyter Notebook directly from Anaconda Navigator (the current browser is Chrome, but I want to open the notebook in Safari).
I can start Jupyter Notebook in Safari by running the following code in Terminal: jupyter notebook --browser safari (based on this answer).
Is there a way to define a different browser for launching the notebook from the Navigator?

Open Anaconda Prompt and run upyter notebook --generate-config
This writes a file to C:\Users\username\.jupyter\jupyter_notebook_config.py
Copy the link C:\Users\username\.jupyter\ and browse to the file location and open it in IDLE editor
Search for the following line in the file: #c.NotebookApp.browser = ''
Remove the # at the beginning of the line to allow the line to execute.
Add the browser target link inside the single quotes c.NotebookApp.browser = 'ENTER TARGET LINK HERE'
To find the browser target link, go to search and look for the browser you want to use. In your case, Safari.
Right click and select “open file location”
Right click on the file, select properties and copy the Target link
Paste the target link inside the single quotes and make sure you delete the double quotes and use forward slashes in your path. (Backslashes are use in double quotes, Forward slashes in single quotes)
Add %s before closing the single quote, so it reads our path as a browser command and add the letter u before the first single quote to indicate this is a unicode string c.NotebookApp.browser = u'BROWSER TARGET LINK HERE %s' and save the file.
Go to Anaconda Navigator and launch jupyter notebook. You will see your selected browser being set to Jupyter notebook.

Right, so digging a bit deeper into the Anaconda documentation, I've found that it's pretty simple, actually. I found the config file at ~/.jupyter/jupyter_notebook_config.py, uncommented the default line # c.NotebookApp.browser = '' and modified it so that it points to the Safari's executable:c.NotebookApp.browser = u'open -a /Applications/Google\ Chrome.app %s'.

Related

"How do you want to open this file" when open Jupyter Notebook with Anaconda or Prompt

When I tried to open it, windows just pop up the regular "i don't know how to open this ext, how do you want to open it" dialog box.
The anaconda prompt gave me url so I can see jupyter, but just no editing functions like run.
conda prompt initiated
Using chrome to open
No edit capability
When you are trying to open Jupyter Notebook for first time after installation, you will see the How do you want to open this file popup, and this is not an error, because Jupyter Notebook will be opened in a browser and desktop does not know which one to use yet. Choose browser you like from the list and Jupyter will be opened in the selected browser as it should. Later if you want, you can change the default browser.

How to change the default browser to Microsoft Edge for Jupyter Notebook in Windows 10?

I've tried the following as suggested in this discussion but it isn't working.
c.NotebookApp.browser = '"C:\Windows\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\MicrosoftEdge.exe" %s'
c.NotebookApp.browser = '"C:/Windows/SystemApps/Microsoft.MicrosoftEdge_8wekyb3d8bbwe/MicrosoftEdge.exe" %s'
c.NotebookApp.browser = 'C:/Windows/SystemApps/Microsoft.MicrosoftEdge_8wekyb3d8bbwe/MicrosoftEdge.exe %s'
c.NotebookApp.browser = 'C:\Windows\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\MicrosoftEdge.exe %s'
After launching Jupyter Notebook the command prompt is opening but the Edge isn't starting with the notebook. Chrome is my default browser in the system and I'd like to keep it that way.
I'm getting the following message in the Jupyter Notebook command prompt window
[I 16:23:06.903 NotebookApp] JupyterLab extension loaded from C:\Users\Pradip\anaconda3\lib\site-packages\jupyterlab
[I 16:23:06.903 NotebookApp] JupyterLab application directory is C:\Users\Pradip\anaconda3\share\jupyter\lab
[I 16:23:06.918 NotebookApp] Serving notebooks from local directory: C:\Users\Pradip
[I 16:23:06.918 NotebookApp] The Jupyter Notebook is running at:
[I 16:23:06.918 NotebookApp] http://localhost:8888/?token=60755565b267fb954a464066911e94ca39fab27da2c067d2
[I 16:23:06.918 NotebookApp] or http://127.0.0.1:8888/?token=60755565b267fb954a464066911e94ca39fab27da2c067d2
[I 16:23:06.918 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 16:23:06.996 NotebookApp]
To access the notebook, open this file in a browser:
file:///C:/Users/Pradip/AppData/Roaming/jupyter/runtime/nbserver-12016-open.html
Or copy and paste one of these URLs:
http://localhost:8888/?token=60755565b267fb954a464066911e94ca39fab27da2c067d2
or http://127.0.0.1:8888/?token=60755565b267fb954a464066911e94ca39fab27da2c067d2
After a lot of searching and frustration at attempts that did not work, I found the solution with help from this post in conjunction with John Lang's response in the comments.
Step 1. Open Anaconda Prompt (CMD.exe Prompt) from the Anaconda Start Menu and type
a. for jupyter notebook:
jupyter notebook --generate-config
b. for jupyter lab:
jupyter lab --generate-config
Step 2. This will generate a file name (e.g. C:\Users......\.jupyter\jupyter_lab_config.py) that you have to locate on your computer and "open with" NotePad.
Step 3. Edit the line #c.NotebookApp.browser = '' or #c.LabApp.browser = '' to include the path of the browser you would like to open jupyter notebook or jupyter lab with (I have used FireFox in my example):
c.NotebookApp.browser = '"C:\\Program Files\\Mozilla Firefox\\firefox.exe" %s'
or
c.LabApp.browser = '"C:\\Program Files\\Mozilla Firefox\\firefox.exe" %s'
Notes for this step:
Pay attention to the single and double quotes
Make sure there are two backslashes between items
Don't forget the %s
Don't forget to remove the # at the start of the line
If you don't know what the path of your browser is, look it up on "windows start" button and open the file location. Right click on the app and click on properties. The path will be there.
Step 4. Save the NotePad file and relaunch jupyter notebook or jupyter lab. It should now be opening on your chosen browser, regardless of what your default browser is.
Paste the target link inside the single quotes and make sure you delete the double quotes and use forward slashes in your path. (Backslashes are use in double quotes, Forward slashes in single quotes)
Add %s before closing the single quote, so it reads the path as a browser command and add the letter u before the first single quote to indicate this is a unicode string c.NotebookApp.browser = u'BROWSER TARGET LINK HERE %s'
If you are using Jupyter Lab, with version 3.0.15, then following are the steps you can follow:
Open terminal/ CMD, and type jupyter-lab --generate-config
Navigate to C:\Users\user_name\.jupyter and open the Python file jupyter_lab_config.py
You will need to change c.ServerApp.browser
Put the path of the browser, followed by a space and %s all within a single ' '.
Remember to uncomment this line before saving the file.
Double check- this line should be commented out in this Python file: # c.ServerApp.open_browser = False
For example, I wanted to use Microsoft Edge as my default browser for Jupyter Lab. So, for me:
c.ServerApp.browser = 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe %s'
Now navigate to the folder you want to open your Jupyter Lab.
Open CMD at that location, and type jupyter-lab in CMD.
You should be good to go now with your selected browser opening for the Jupyter Lab.
"anaconda" opens the browser that you have by default in your operating system. So the easiest option is change the default browser of the system

Edit the Jupyter terminal's starting directory

when I launch a Jupyter notebook (which I installed through Anaconda), the explorer opens in C:\Users\Joe
However, when I open a terminal (New -> Terminal) it opens in C:\Users\Joe\Documents
Is it possible to change the starting directory of the terminal, so it matches the notebook explorer page?
I have already tried the following:
1) edit the 'Start in' field in windows PowerShell properties to my desired location. This didn't change anything - it's as if when jupyter opens the terminal it overrides this - though it seems to use PowerShell.
2) running jupyter notebook –generate-config, and inserting my desired path to c.NotebookApp.notebook_dir = "".
Any advice would be appreciated, thank you!
Resolved this myself - found the answer here:
Find the default local Jupyter directory
In the Anconda3 folder there is a file cwp.py, which determines where the working directory should be changed to. I commented out lines 26-30:
if exception:
documents_folder, exception = get_folder_path(FOLDERID.PublicDocuments)
if not exception:
os.chdir(documents_folder)
and the path in the terminal is no longer being modified.

Creating an ipynb from URL

really simple question.
I have a URL here that I'm trying to open in Jupyter lab as a jupyter notebook. What is the easiest way to do this? Is there a command line command somewhere?
Thanks.
step 1:
visiting the URL "ctrl + s" to download that file (you would get the file name as xxx.ipynb.txt)
step 2:
Start jupyter notebook. Find the directory where you save to and double click file to open it.
step 3: Change it's name from 'xxx.ipynb.txt' to 'xxx.ipynb'. (delete .txt in the last of file name)close the window and open it again.
I think it's work!!
Just download that file to a working directory - with a menu 'save link as...' or visiting the URL and doing save.
Start juypter notebook from shell (commandline).
In the directory window that it opens for you, double click the saved style.ipynb. Now you have the notebook running in a new window.

Sublime Text plugin can’t find my default browser

TL;DR: Sublime Text gets a different response from webbrowser._browsers than my terminal.
This has been driving me nuts. I use a plugin, GitLink which will open a GitHub link from your current file. It relies on Python’s webbrowser tool to open the url. The problem is my default browser is Chrome, but it keeps opening Firefox.
In my terminal, if I launch into python or python3, webbrowser.open_new_tab('https://stackoverflow.com') will correctly launch in Chrome. webbrowser._browsers will correctly list 'chrome' as one of my browsers.
However, in the Sublime Text console, webbrowser._browsers is missing Chrome. It lists all the other browser save for the one I actually want. What gives? How is Sublime Text getting a different list than when I run python in my terminal? How do I get it to match?
There are several things you could try.
1. Set the BROWSER environment variable:
As Keith Hall implied this problem might be solved by setting your BROWSER environment variable. If you don't know how look it up for your version - OSX changed how environment variables get set at some point so there are different ways of doing this for different versions of OSX. However, I am on Linux, and my BROWSER environment variable is not set and ST always opens urls in my default browser, this includes calls to webbrowser.open_new_tab() which (as I assume you saw) is what GitLink uses to open urls, so this may not solve the problem.
2. Modify GitLink (your installed version):
First test if this will work - works fine on Linux.
Copy and paste the following 2 lines into the ST console:
import webbrowser
webbrowser.get("chrome").open_new_tab("http://www.google.com")
If that does not open Chrome with Google.com try:
# google-chrome: Chrome variant.
webbrowser.get("google-chrome").open_new_tab("http://www.google.com")
# macosx: uses the OSX default browser.
webbrowser.get("macosx").open_new_tab("http://www.google.com")
# links: generic; doubtful but worth trying at this stage.
webbrowser.get("links").open_new_tab("http://www.google.com")
The full list of possible values may be helpful.
Another possibility is to use the full path instead, see this StackOverflow answer or try:
# Replace path with your path to Chrome if necessary.
webbrowser.register('chrome_path', None, webbrowser.BackgroundBrowser("/Applications/Google Chrome.app"))
webbrowser.get('chrome_path').open_new_tab(url)
Once that is working:
Install the PackageResourceViewer plugin; after it is installed...
Open the Command Palette and select: PackageResourceViewer: Open Resource
In the list of packages select: GitLink
In the list of files select: GitLink.py
The file GitLink.py will open...
If you save this file (nothing will happen at all if you close it without saving) then a copy of GitLink.py will get saved on your system in this location: ST_CONFIG/Packages/GitLink/GitLink.py - this version of the file will override the version of GitLink.py which is stored in the Gitlink.sublime-package file which Package Control would have installed in the Installed Packages folder. Even if the GitLink package gets updated the version in the .sublime-package file will still get overridden. Not a problem, all you need to do to get rid of the changes made is to delete the folder ST_CONFIG/Packages/GitLink/ which contains the GitLink.py file and ST will start using the version from the .sublime-package file again.
The modification is easy:
Scroll down to the bottom of GitLink.py where you will see the lines:
if(args['web']):
webbrowser.open_new_tab(url)
Just change the webbrowser.open_new_tab(url) line to the following (replacing "chrome" if necessary with the value which worked in the console):
if(args['web']):
webbrowser.get("chrome").open_new_tab(url)
Save the file, the plugin should be updated immediately by ST (check the console for the "reloading plugin" message to be sure if you want). The plugin should now open your urls in Chrome.
3. Open an issue on GitLink's GitHub page:
The issue page is here. State your problem and request a setting be added so that users can specify which browser Python's webbrowser module should use.
I suggest you do this anyway and add a link to this StackOverflow page to your issue for reference.

Categories