How to run IPython notebook server with a specified URL route - python

I want to run a IPython notebook server on the a predefined URL like: http://localhost:8888/my_notebook.
I've tried
$ ipython notebook --ip=localhost/my_notebook
which didn't work.
Can anyone please help?

You can set the configurable value from command line like this:
ipython notebook --NotebookApp.base_url='my_notebook'
Alternatively, you can set it in a config file for your profile. You can read more about it here.

Related

How to debug Jupyter Notebook profile_default/startup File?

I am placing a .py file in Jupyter Notebook profile_default/startup folder. It has some problems, I want to debug and see the logs. Is there any way I see the output or logs generated by that file. I am on windows 10. There are a few methods .py file. I am using keyboard module to generate a hotkey whenever the notebook starts. It isn't working for me. Please suggest a good way to at least debug that file. My ipykernel is 5.3.4 and ipython 7.16.1
The easiest way I've found so far is to fire up ipython from your console.
Assuming the error is not specific to how Jupyter starts the kernel, you will see the error with the stacktrace before the ipython shell starts.
If the error is specific to jupyter kernel, use jupyter kernel. It will start the kernel you specify, and drop a log line if there's an error.

Automatically running a Jupyter notebook

I have a Jupyter notebook that scrapes web data and saves a dataframe to a csv. I would like to run this every day automatically. I am using a mac for this project.
I have looked around a lot (including here: how to run a python jupyter notebook daily automatically), but as of yet I have not found a clear enough answer. I am quite new to all this, so I am looking for a step-by-step: like how you'd explain it to someone with no knowledge on cron etc.
Any advice would be much appreciated! Thank you!
Why don't you just convert the jupyter notebook into a raw python file? You can use this command: jupyter nbconvert --to script "[YOUR_NOTEBOOK].ipynb" (replace [YOUR_NOTEBOOK] with your notebook name)
EDIT: You could also use Juptext, as pointed out by #Wayne in the comments below.
If you need a jupyter notebook:
Use datalab or papermill
Use the SeekWall Chrome Extenstion
Create a custom python script to launch jupyter notebook, and run that python script using the Automator app in your Mac

Jupyter Notebook: Change Data Rate Limit Inside Active Notebook

I have a jupyter notebook where an executed cell gives the following error:
IOPub data rate exceeded...
I understand this is an option:
jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10
However, I would really prefer to just to set this along with my import statements and other notebook settings instead of tweaking configuration files or command line when starting notebooks. Is there an easy way to do this?
I would recommend creating an alias jn to launch Jupyter notebook with these settings every time. You do it once for all and do not have to tweak with command line after.
Under UNIX system you run in terminal ::
alias jn="jupyter notebook --NotebookApp.iopub_data_rate_limit=2147483647"
To enable this alias at every session you have to append this command line to your shell config file, by default ~/.bash_profile
If you run Windows check the equivalence to aliases

Run Jupyter Notebook (.ipynb) from command line as if it were a .py file

I am authoring a Jupyter notebook on my local machine that will eventually be run on a remote server (which is running Ubuntu). Every time I need to make a change I must export the notebook as a .py file and then call it from the command line of the server.
I'd like to be able to run this on the fly, calling one command that takes the current .ipynb file and executes it on the command line as if it were a .py, showing all the print statements and output you'd expect if the .py were run. I thought nbconverter might do the trick using something like the following command:
jupyter nbconvert --to script --execute nbconvert_test.ipynb
As it turnout, this does not convert the .ipynb to a .py file to be executed on the command line as I would like, but rather it creates a new file called nbconvert_test.py in the same directory which I would then have to run in a separate command. I'd really like to prevent the creation of that file every time I make even a small change, and to skip the extra step on the command line.
Any help is appreciated!
You can send the jupyter nbconvert to stranded output and pipe that to python.
jupyter nbconvert --to script --execute --stdout test_nbconvert.ipynb | python
A workaround is a small shell script that has three parts
converting the notebook
executing created script
removing the script
create a file runnb.sh
#!/bin/sh
# First argument is the notebook you would like to run
notebook=$1
scriptname="$(basename $notebook .ipynb)".py
jupyter nbconvert --to script --execute ${notebook} && python ${scriptname}
rm ${scriptname}
use as such:
$ ./runnb.sh nbconvert_test.ipynb
EDIT:
According to this answer, this command should do just fine jupyter nbconvert --execute test_nbconvert.ipynb (just leav out the --to flag
With the boar package, you can run your notebook within a python code, using:
from boar.running import run_notebook
outputs = run_notebook("nbconvert_test.ipynb")
For more information, see:
https://github.com/alexandreCameron/boar/blob/master/USAGE.md
I had a similar error
xyz.py not found for rm command
Then I ran the script inside the conda virtual environment and it worked.
conda activate

Launch IPython notebook with selected browser

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.

Categories