tqdm fails when detecting Jupyter notebook - python

I'm using tqdm to display a progress bar for my code. Sometimes I use the code in the terminal and sometimes I use the code in a Jupyter Notebook.
Unless I'm mistaken (probably am or I wouldn't be writing this question), tqdm is supposed to automatically determine if I'm in the Jupyter Notebook or in the terminal by using this code:
from tqdm.autonotebook import tqdm
def extend(index):
# some code here
# X is a pandas DataFrame defined elsewhere
with mp.Pool(N) as pool:
results = list(
tqdm(pool.imap(extend, X.index), total=len(X)))
The progress bar is shown nicely in the terminal like this:
100%|█████████████████████████████████████████| 15035/15035 [35:02<00:00, 7.15it/s]
But when the same code is called in a Jupyter Notebook, this is displayed instead:
HBox(children=(HTML(value=''), FloatProgress(value=0.0, max=15035.0), HTML(value='')))
Am I doing something wrong? What do I need to do to get tqdm to display nicely in Jupyter Notebook and in the terminal?

If you are editing the notebook in the JupyterLab, you should install jupyterlab_widgets.

Related

ModuleNotFoundError 'matplotlib_inline'

As the title suggests, I am trying to set my notebook to plot in separate window using the '%matplotlib qt' command. However, when I do I get a 'ModuleNotFoundError' for matplotlib.inline for some reason. I have checked my env using 'pip list' and I have all the modules I need installed (I know this is the case because I have other environments for separate projects where %matplotlib qt works just fine and the module imports are identical.
Hoping for a suggestion that I haven't tried yet. pic of my import cell on current notebook:
The problem seems to occur when I run the % command, even if I just run %matplotlib (without the qt) then I still get the same error. Could this be some sort of a path issue?
EDIT: I will add that if I remove the % command from my notebook I can plot just fine inline, but I don't have the plot tools to zoom in or move around which is the functionality I need to review my data.

Import variable from python script to jupyter ipython notebook

I am newbie to python and searched for 2 days to get solution but could not get find any solution. Please help me.
I have two python(newkmeans.py,newbisecting.py) files and one ipython jupyter notebook. I am running newbiecting.py from ipython notebook by calling it as %newbisecting.py which in turn calls newkmeans.py file. Now newkmeans.py file generates few set of variables like for example list values list1. I want to access this list1[] in ipython jupyter notebook. How can I do this? Please help me out.
I have tried using from newkmeans import list1 in ipython juypter notebook but could not get any results.

How to load Python script into Jupyter Notebook without revealing code and making it available to all cells

I'm trying to import a Python script as a module into one of my Notebooks, but the import or import importlib commands do not work in Jupyter Notebook like they do in a standard Python file and terminal.
I've come across the %load command but as far as I understand and have seen in my own Notebook, is that it only loads the contents of the script I'm trying to import into the current cell and is invisible to the other cells, and it also outputs the code in the script being imported for everyone to see.
I want to be able to load the script and make it available to all cells, as well as hiding the code for the purpose of encapsulation and also keeping the Notebook neat and tidy - only focusing on the code relevant to the topic of the Notebook. Is this possible?

Reloading python function

I am using a jupyter lab notebook and trying to modify code, reload it within the jupyter notebook and use the modified code without reloading the kernel. I am using python 3.5.5 and am running code like this:
(in file test.py)
def myTest():
print('hello')
(in jupyter)
from test import myTest
import importlib
importlib.reload(test)
myTest()
When I run the code in my jupyter lab notebook I get a NameError that name 'test' is not defined. From searching on stackoverflow the only references I find to this error is problems using older version of python. But the way I am using importlib.reload() seems to be correct.
Have you tried the built-in magic command autoreload?
At the beginning of your notebook, add:
%load_ext autoreload
%autoreload 2

Can %matplotlib notebook only be bused in Jupyter?

I have this and it works in jupyter in browser:
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy as sp
However, after I copied it to a python file in the PyDev IDE editor, it always complains the first line:
%matplotlib notebook
Error message:
Encountered "%" at line 1, column 1. Was expecting one of: <EOF> <NEWLINE> ... "(" ... "{" ...
Does this mean the magic method can only be used in jupyter notebook?
Yes, It's a jupyter notebook command.
Well, you can't use it in a file (as that's not valid Python syntax), but you can use it in the PyDev interactive console: http://www.pydev.org/manual_adv_interactive_console.html if you have IPython properly installed.
The command in question, %matplotlib notebook, can be used to specify how to display graphs in a Jupyter notebook. If you use %matplotlib inline, the graphs will be displayed as a part of the Jupyter notebook. Using the form of the command that you asked about will display the graph in an interactive form within the Jupyter notebook. Or using %matplotlib gtk will open another program to display each individual graph, which is how Python would display the graphs in any other situation. So the command does not have any use or meaning outside of a Jupyter notebook.

Categories

Resources