Debugging a function being called from notebook in jupyter-lab - debugging

I have a notebook in jupyter-lab, lets say it is as simple as this:
import importlib
import data_exploration as de
importlib.reload(de)
de.test(3,5)
In the data_exploration.py file I have the following function:
def test(a,b):
c=a+b
d=a-b
return c,d
I am able to set a breakpoint in the .py file, but when I run the code from the notebook the execution doesn't stop at the breakpoint. How can I connect the debugger in the notebook and in the source file. Please also see the attached images:
[

Related

Jmeter OsProcessSampler

I'm doing a project and I'm trying to test it via JMeter with OS Process Sampler, but this error keeps popping up, I suppose it because there is no input given.
JMeter error
This is the menu I'm using
Menu
My question is, how can I set the input that is going to be used in the test?
Here are some images that may help:
Main page
Main(what I'm running
The error comes from your Python script, JMeter executes the script but it fails somewhere somehow (cannot read a file)
Your configuration looks correct.
Given this Python script:
import sys
def hello():
print('Hello, ' + sys.argv[1])
hello()
it can be executed successfully in the OS Process Sampler
As you can see the View Results Tree listener contains Python script output.
So maybe you should set the "Working Directory" to where you Python script lives because with your settings it's executed in JMeter's "bin" directory context hence most probably it cannot read some file.
More information: How to Run External Commands and Programs Locally and Remotely from JMeter

Activating virtualenv and opening Jupyter notebooks from a single terminal command

I'd like to be able to open my projects easily using one command in terminal instead of doing what I normally do. I tried creating a command like this in my .zshrc:
my_project() {
cd my_folder
source venv/bin/activate
jupyter notebook
}
Normally if I enter these commands one at a time it opens fine. But I'd rather just run my command and it do everything for me and automatically launch the notebook. When I run this however, it just goes into the folder and stops without running the rest of the commands after it. How do I make it run them one after another?
Figured it out! You can run commands after one another by using a semicolon (;). For example using the above code:
my_project() {
cd my_folder;
source venv/bin/activate;
jupyter notebook;
}

Timeout when running iPython nbconvert from command line

I'm trying to automate IPython scripts using batch files and the Windows Task Manager. After much digging I've found that you can use Task Manager to trigger a BAT and through that execute the nbconvert package in order to run the Jupyter Notebook without the interactive bits.
In the BAT my code first "turns the Windows cmd into an Anaconda Prompt" with the code: <anaconda3 path>\Scripts\activate.bat <anaconda3 path>
(No formal CS education, so bear with me on this.)
That line allows me to run conda and jupyter commands out of the Windows cmd. I then run
jupyter nbconvert --execute --ExecutePreprocessor.enabled=True --ExecutePreprocessor.timeout=60 <Notebook>
I keep receiving a timeout error when this occurs. Yes, I could adjust the timer, but before setting the timer to 10 hours and coming back in the morning I'm hoping for some understanding of what is going on behind the scenes in nbconvert, and maybe some insights into how to get this working. The .ipynb file works fine when run through Jupyter Notebook. There is however a point at which the script is attempting to load several gb of data, which has taken ~20 minutes each time I've run it.
To that end - when nbconvert engages the Pre-processors, are they running the entire script preemptively? I'm struggling to find a reason as to why nothing opens, pings, runs, updates, etc... when executing the nbconvert command. I would expect at least an uptick in RAM usage, but as far as I can tell that isn't happening either.
How can I work around this, and what is going on behind the scenes when nbconvert is executed?
Traceback:
C:\Users\User>jupyter nbconvert --execute "C:\Users\User\Desktop\_for_Python\CBSA.ipynb"
[NbConvertApp] Converting notebook C:\Users\User\Desktop\_for_Python\CBSA.ipynb to html
[NbConvertApp] Executing notebook with kernel: python3
[NbConvertApp] ERROR | Timeout waiting for execute reply (30s).
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Continuum\anaconda3\lib\site-packages\nbconvert\preprocessors\execute.py", line 423, in _wait_for_reply
msg = self.kc.shell_channel.get_msg(timeout=timeout)
File "C:\Users\User\AppData\Local\Continuum\anaconda3\lib\site-packages\jupyter_client\blocking\channels.py", line 57, in get_msg
raise Empty
_queue.Empty
For anyone else facing this error with a correct path, I was able to extend the timeout using the flag --ExecutePreprocessor.timeout=300
According to the documentation:
The timeout traitlet defines the maximum time (in seconds) each
notebook cell is allowed to run, if the execution takes longer an
exception will be raised. The default is 30 s, so in cases of
long-running cells you may want to specify an higher value.
Fixed by adding the Anaconda3/Library/Bin path to c/windows/system32 PATH. Timeout was occurring due to DLL error, being returned as Timeout in command line.
According to the documentation: "The timeout option can also be set to None or -1 to remove any restriction on execution time.". So that would be: --ExecutePreprocessor.timeout=-1

Code for .zshrc or .bash_profile that sets jupyter notebook Alias to type in terminal as shortcut jn

I develop in Anaconda3 and Jupyter Notebook with Python for my coding environment on Mac OSX, with Visual Studio Code.
I typically type jupyter notebook numerous times throughout the day to launch new instances, yet this is inefficient for keystrokes.
I am looking to write a script or alias inside my .zshrc file for OhMyZsh (similar to .bash_profile) that will let me simply type jn, as desired to save time in Terminal to launch a new jupyter notebook instance.
If I understand correctly
alias jn="jupyter notebook"
Though, I would suggest using a function that you can define a notebook directory
function jn() {
local notedir="$1"
# Add extra logic here to give a standard directory, if no param given
jupyter notebook "$notedir"
}
Sidenote: Bundling a Docker container with all your needed kernels and combining that with this function allows for good portability, for example sharing notebooks with others

How to change the default browser used by jupyter notebook in windows

I'm on a windows machine without admin right and I would like to run jupyter on chrome, while the default browser is another.
I have a local installation of the Anaconda distribution and my first option to start jupyter would be through the Anaconda Navigator, but maybe I have to do something else. Because it is a local installation the command line jupyter notebook produces no results.
When I paste the url address in the default browser I have (something like http://localhost:8892/notebooks/Home/Exercices/Testing1.ipynb the chrome page asks me for a password or token. I have no password and I do not know what a token is.
Is there a way to change the browser of the Anaconda Navigator? or how can I start jupyter with Chrome?
Thanks to #Darthbith and this post How to change the default browser used by the ipython/jupyter notebook in Linux? I was able to figure it out:
Step 1: To open Anaconda Prompt from the Start Menu and type
# for old notebook and JupyterLab < 3.0, or
jupyter notebook --generate-config
# for new nbclassic and JupyterLab >= 3.0
jupyter server --generate-config
This will generate the file ~/.jupyter/jupyter_notebook_config.py (or jupyter_server_config.py for nbclassic/new JupyterLab)
Step 2: Edit this file and change the following line (chrome is also is also in a local installation)
# for old notebook and JupyterLab < 3.0
c.NotebookApp.browser = u'C:/Home/AppData/Local/Google/Chrome/Application/chrome.exe %s'
# OR for new nbclassic and JupyterLab >= 3.0
c.ServerApp.browser = u'C:/Home/AppData/Local/Google/Chrome/Application/chrome.exe %s'
In Windows, write in cmd/ Anaconda Prompt:
jupyter notebook --generate-config
The jupyter_notebook_config.py file generated is situated in "C:\Users\YourName\.jupyter\" folder.
Open it using a text editor and change #c.NotebookApp.browser = '' to
import webbrowser
webbrowser.register('chrome', None, webbrowser.GenericBrowser(u'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'))
c.NotebookApp.browser = 'chrome'
and save the file.
Now execute the jupyter-notebook command and the set browser will be used.
I don't know the precise details for Windows, but this is how to set the default browser on a Mac:
jupyter notebook --generate-config
This creates a file jupyter_notebook_config.py in ~/.jupyter. Edit the line
#c.NotebookApp.browser = ''
On a Mac I set it to:
c.NotebookApp.browser = u'open -a /Applications/Gooogle\ Chrome.app %s'
You just need to figure out how to point it to Chrome on Windows.
As far as I know, there's no way to change the default browser that opens. However, you can find the token for the Notebook server by opening Anaconda Prompt from the Start Menu and typing
jupyter notebook list
This will give you a URL with the token that you can copy/paste into any other browser. The output of the list command looks like
Currently running servers:
http://localhost:8888/?token=41429d3dcf554d0dde69498aac0950654a590664ba02b3cd :: /path/to/home/folder
So you can either type http://localhost:8888 into the browser and then copy/paste the token into the field, or just copy/paste the whole URL with the token.
The following also works for me. I give it a full path to chrome, plus %s at the end.
jupyter notebook --browser='C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
If chrome is in the PATH environment variable, the following might work too.
jupyter notebook --browser=chrome
You don't need to change anything in the jupyter_notebook_config file. check whether your default web browser(if it's chrome) or reset and again choose as a web browser(chrome for me)as a default browser. it worked for me.
I'd like to offer a little more information about what to put in your jupyter_notebook_config.py file than is included in any of the other answers. jupyter is using python's webrowser module to launch the browser by passing the value for c.NotebookApp.browser to the webbrowser.get(using=None) function. If no value is specified, the function selects the user's default browser. If you do specify a value here, it can be interpreted in one of two ways, depending on whether or not the value you specified ends with the characters %s.
If the string does not contain the characters %s
it is interpreted as a browser name and the module checks if it has a browser registered with that name (see the python documentation for which browsers are registered by default). This is why Abhirup Das's answer works, first the webbrowser module is imported
import webbrowser
the chrome browser is registered with the module
webbrowser.register('chrome', None, webbrowser.GenericBrowser(u'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'))
and finally, the jupyter server is fed the browser name
c.NotebookApp.browser = 'chrome'
This browser registration does not persist, so the process must be repeated every time the server is launched.
Alternatively, if the string does contain the characters %s, it is interpreted as a literal browser command. Since this question is about how to run the browser on Windows, the browser command will probably contain backslashes. The backslash is used in python string literals to escape any characters that otherwise have any special meaning (e.g., to include a quote or double quote inside the string literal). Any backslashes in the browser command need to be be escaped or replaced. The easiest way is to replace the backslashes in the command with foward slashes, e.g.,
'C:/Home/AppData/Local/Google/Chrome/Application/chrome.exe %s'
rather than
'C:\Home\AppData\Local\Google\Chrome\Application\chrome.exe %s'
I for the life of me couldn't get unicode/raw string commands or commands where I escaped each backslash with an extra backslash to work, so replacing the backslashes with forward slashes may be the only option. I verified that the strings I tried all worked in python, so the only way to be sure would be to look at the jupyter source code.
Anyway, since registering a browser with the module does not persist, if your browser isn't already registered by default, it is probably best to use a literal browser command with the backslashes replaced with forward slashes.
On Mac this works:
1) Generate a config file from within your environment:
jupyter notebook --generate-config
This will place jupyter_notebook_config.py in ~/.jupyter.
2) Modify the following line in jupyter_notebook_config.py:
c.NotebookApp.browser = 'open -a /Applications/Google\ Chrome.app %s'
For linux users:
First generate config file using:
jupyter notebook --generate-config
Then open the generated file and look for #c.NotebookApp.browser = ''
Edit it to : c.NotebookApp.browser = '/bin/brave %s'
Replace /bin/brave with whatever your browser executable location is.
The explanations above didn't work for me, I guess, I mistyped something. Actually it was easier for me to change default browser to Chrome and then Jupiter automatically starts in Chrome after next launch. (Search Windows - change default browser).
Jupyter looks for the BROWSER environment variable when choosing which browser to launch.
I recommend setting BROWSER over configuring Jupyter specifically, because setting BROWSER is the default way to specify which browser you prefer, regardless of which application it applies to.
To choose the browser for a single session, set the BROWSER environment variable when running the jupyter process.
BROWSER=chromium-browser jupyter notebook when you have chromium-browser when you prefer to use chromium-browser on PATH. Typical for Linux.
BROWSER=C:/Home/AppData/Local/Google/Chrome/Application/chrome.exe jupyter notebook when you don't have the application on PATH. Typical for Windows.
BROWSER=<your browser> jupyter notebook otherwise.
To choose browser for your whole system, set the BROWSER environment variable globally.
Find .../jupyter/runtime/nbserver-11596-open.html file, or whatever the file name is, you can find the file name when jupyter notebook starts, and associate it with Chorme worked for me.
Open anaconda prompt and type
jupyter notebook --generate-config
then go to "jupyter_notebook_config.py" path and add following line
c.NotebookApp.browser = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
Is there any way to run jupyter on chrome in /tmp ?
something like that:
jupyter notebook --browser='google-chrome --user-data-dir=/tmp/'
There is a much simpler way than typing commands in the command window, you can use the Windows file explorer!
Simply navigate to the following path C:\Users\**YourUser**\AppData\Roaming\jupyter\runtime\, as below:
There, among other files, you will see the corresponding .html files of your jupyter jobs. You can right-click on any .html file, select "Open As" as then select other application (as shown on the image below - apologies my default language is in Spanish).
From here, you can select the most suitable navigator for you. In my case I am using Firefox, but you can choose Chrome or whatever (as shown below). Make sure to click the "Use always this application to open .html files" checkbox to set Chrome as the default navigator.
From now on, Jupyter Notebooks will always open in Chrome. Hope it helped!
Make sure to activate the line by removing the # comment indicator.
After considerable thrashing about trying to launch a jupyter notebook in chrome from Anaconda in Win10 when chrome was not my default browser, I combined several of the suggestions above and, in the jupyter_notebook_config.py file under .jupyter in my home directory put in these lines in place of the default c.NotebookApp.browser line, and it finally worked!:
import webbrowser
webbrowser.register('chrome', None, webbrowser.GenericBrowser(u'C:/PROGRA~2/Google/Chrome/Application/chrome.exe'))
c.NotebookApp.browser = 'chrome'
Note the use of Unix-style directory separators (this is apparently a bug in webbrowser) and the use of DOS-style "PROGRA~2" --- both of these seem to be necessary. Adding "%s" after "chrome.exe" seemed not to help.
To achieve this on Windows 10, I had to do the following:
For a temporarily choose/specify a browser from the Anaconda Prompt CLI (note the order/type of quotes, they seem to be different to most other answers as those answers failed to work for me):
jupyter notebook --browser="'C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe' %s"
To set it permanently, edit the jupyter_notebook_config.py file in your .jupyter folder. I'm not certain that you need to escape the backslashes (i.e. \ vs just ), but I used the following and it worked (again, note that the order/type of quotes is different):
c.NotebookApp.browser = '"C:\\Program Files (x86)\\BraveSoftware\\Brave-Browser\\Application\\brave.exe" %s'
In my case, macOS 10.15.4 with anaconda 1.9.12, finally, I found an effective one as below:
c.NotebookApp.browser = u'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome %s'
I hope this helps someone. :-)
Jupyterlab 3 migrated from notebook server to plain jupyter server. To select the browser jupyter lab will open, put the config in .jupyter/jupyter_server_config.py and replace NotebookApp by ServerApp. For example:
c.ServerApp.browser = '/usr/bin/firefox -P notebook --new-window %s'
if you didn't specify a browser for your jupyter notebook, maybe just changing the default browser of your operating system can solve your issue; like it did for me.
check default browser in window:
default apps: web browser
Check your default browser's configurations, The default browser is where the Jupyter Notebook will open in.
on Windows, Simply change it doing:
Search in the start menu "Default apps", open it.
Under Web browser, select the browser currently listed, and then simply change it to your desired browser (where you want the Jupyter Notebook to open).
here are the steps
Open Anaconda promt and write:
jupyter notebook --generate-config
go to that path and open with a text editor the .py file
In that file look for the line that constains the follow text:
#c.NotebookApp.browser = ''
Befor that line write the follow code
import webbrowser
webbrowser.register('chrome', None, webbrowser.GenericBrowser(u'C:/PROGRA~2/Google/Chrome/Application/chrome.exe'))
drop de symbol # in the lines to set the browser, so it looks like:
c.NotebookApp.browser = ''
save the file, this makes Chrome as a default browser to launch jupyter notebook
use this command(windows cmd):
jupyter notebook --browser NotebookApp.browser
it generates a link (localhost link), copy paste it in any browser that you need and use you notebook.
Microsoft have setup Edge as a persistent virus on Windows. Even if you set the default browser to Chrome in Settings, you still get edge when opening up Jupyter.. This is because Microsoft have set Edge as the default app for .htm and .html files. In the settings for app defaults, find that one and change it to Chrome and you are all set..
Easy steps:
1. Uninstall the current browser which notebook picks on launch.
2. Launch the notebook again, it will ask for browser: choose the required one and enable the clause which says : (something like) Always choose this application for this types of files.
It will work.
Install back you uninstalled browser.

Resources