Is there any way to obtain a Jupyter notebook's entire output history?
There are two objects in python In and Out objects.
That's how they work:
First wrote some code
import math
math.sin(2) #now, replacing with math.cos(3) and running again
It's Output:
0.9092974268256817 #when replaced with cos, O/P: -0.4161468365471424
Now to check input history:
In [4]: print(In)
['', 'import math', 'math.sin(2)', 'math.cos(2)', 'print(In)']
Now to check output history:
In [5]: Out
Out[5]: {2: 0.9092974268256817, 3: -0.4161468365471424}
Note: this will not get history when the notebook is closed and freshly started. It will work in the current session only.
Check this link for more explanation: https://jakevdp.github.io/PythonDataScienceHandbook/01.04-input-output-history.html
For example, you do something like this - In[1]: 1+2 you get Out[1]: 3.
So, you can do this in the next line :
In[2]: print(In[1])
And also for output
print(Out[1])
Check this - Output History
Related
IPython and Jupyter notebooks keep track of an 'execution count'. This is e.g. shown in the prompts for input and output: In[...] and Out[...].
How can you get the latest execution count programmatically?
The variable Out cannot be used for this:
Out is a dictionary, with execution counts as keys, but only for those cells that yielded a result; note that not all (cell) executions need to yield a result (e.g. print(...)).
The variable In seems to be usable:
In is a list of all inputs; it seems that In[0] is always primed with an empty string, so that you can use the 1-based execution count as index.
However, when using len(In) - 1, in your code, you need to take into account that the execution count seems to be updated before execution of that new code. So, actually, it seems you need to use len(In) - 2 for the execution count of the most recent already completed execution.
Questions:
Is there a better way to get the current execution count?
Can you rely on the above observations (the 'seems')?
After more digging, I found the (surprisingly simple) answer:
from IPython import get_ipython
ipython = get_ipython()
... ipython.execution_count ...
This does not show up in the IPython documentation, though could (should?) have been mentioned here: https://ipython.readthedocs.io/en/stable/api/generated/IPython.core.interactiveshell.html (I guess attributes are not documented). Here you do find run_line_magic (which was mentioned in a comment to my question).
The way I found this attribute is by defining ipython as above and then doing code completion (TAB) on ipython. (but it does not have documentation).
help(ipython)
gives you documentation about InteractiveShell, and it does mention execution_count (though it does not confirm its purpose):
| execution_count
| An int trait.
I was wondering if there was a way to modify Jupyter Lab or Notebook's tab-complete functionality. For example, if I type "\alpha", and then press the tab key, I will get the UTF-8 character "α" in the cell.
Is there any way that I can do a custom "[string without spaces]" to tab-complete into some specific UTF-8 character or string?
e.g. "\implies" + tab -> "⇒"
I can see a large number of use cases for this in my programming life, so I was wondering if Jupyter (Lab or Notebook) offered the ability to modify some settings (or load in a file) that maps strings to a tabbed output. Or is there a different idea that you could use to implement this?
Thanks!
Jupyter is (based on) IPython and it's completion engine - we can make use of it.
Update: you can override latex_symbols dict to get the behavior you want:
from IPython.core.latex_symbols import latex_symbols
latex_symbols['\\implies'] = '⇒'
You should be able to build upon this example to make it work with a whole range of characters.
Initial answer/another solution: you can hook into IPython completers, though it would not work exactly as you wish: it won't replace the text, but just append a special character. Here is an example:
from IPython import get_ipython
def implies_competer(ipython, event):
return ['⇒']
ipython = get_ipython()
ipython.set_hook('complete_command', implies_competer, re_key='.*implies')
Then when you type (the space after the keyword is important):
and then press tab, you will get:
I would like to be able to save the TEXT output of an iPython notebook cell into a file on disk.
I have 2 additional requirements/requests:
be able to re-run the cell and overwrite my output with whatever the latest is.
also display the output within the notebook.
I have figured out how to use the %%capture magic for some basic saving of an iPython notebook's cell into a file, but it does not seem flexible enough: it keeps appending every time I re-run the cell and I cannot get it to display within the same cell.
Here is what I have so far:
%%capture cap --no-stderr
print 'stuff'
with open('output.txt', 'w') as f:
f.write(cap.stdout)
# clear the cap by deleting the variable here?
# del cap
When I try to put cap.show() after the write, it does not seem to display. Instead, it puts the output into the cap variable twice.
You have a typo, missing d in cap.stout. It should be cap.stdout
I tested the following and it worked fine. cap.show() also printed "stuff" and re-running the cell overwrote the file.
%%capture cap --no-stderr
print 'stuff'
with open('output.txt', 'w') as f:
f.write(cap.stdout)
%%capture cap --no-stderr
print("a")
with open('output.txt', 'w') as f:
f.write(str(cap))
Sometimes I execute a method that takes long to compute
In [1]:
long_running_invocation()
Out[1]:
Often I am interested in knowing how much time it took, so I have to write this:
In[2]:
import time
start = time.time()
long_running_invocation()
end = time.time()
print end - start
Out[2]: 1024
Is there a way to configure my IPython notebook so that it automatically prints the execution time of every call I am making like in the following example?
In [1]:
long_running_invocation()
Out[1] (1.59s):
This ipython extension does what you want: https://github.com/cpcloud/ipython-autotime
load it by putting this at the top of your notebook:
%install_ext https://raw.github.com/cpcloud/ipython-autotime/master/autotime.py
%load_ext autotime
Once loaded, every subsequent cell execution will include the time it took to execute as part of its output.
i haven't found a way to have every cell output the time it takes to execute the code, but instead of what you have, you can use cell magics: %time or %timeit
ipython cell magics
You can now just use the %%time magic at the beginning of the cell like this:
%%time
data = sc.textFile(sample_location)
doc_count = data.count()
doc_objs = data.map(lambda x: json.loads(x))
which when executed will print out an output like:
CPU times: user 372 ms, sys: 48 ms, total: 420 ms
Wall time: 37.7 s
The Simplest way to configure your ipython notebook in a way it automatically shows the execution time without running any %%time or %%timelit or time.time() in each cell, is by using ipython-autotime package.
Install the package in the begining of the notebook
pip install ipython-autotime
and then load the extension by running below
%load_ext autotime
Once you have loaded it, any cell run after this ,will give you the execution time of the cell.
And dont worry if you want to turn it off, just unload the extension by running below
%unload_ext autotime
It is pretty simple and easy to use it whenever you want.
And if you want to check out more, can refer to ipython-autime documentation or its github source
Sometimes my Ipython notebooks crash because I left a print statement in a big loop or in a recursive function. The kernel shows busy and the stop button is usually unresponsive. Eventually Chrome asks me if I want to kill the page or wait.
Is there a way to limit the number of output lines in a given cell? Or any other way to avoid this problem?
You can suppress output using this command:
‘;’ at the end of a line
Perhaps create a condition in your loop to suppress output past a certain threshold.
For anyone else stumbling across:
If you want to see some of the output rather than suppress the output entirely, there is an extension called limit-output.
You'll have to follow the installation instructions for the extensions at the first link. Then I ran the following code to update the maximum number of characters output by each cell:
from notebook.services.config import ConfigManager
cm = ConfigManager().update('notebook', {'limit_output': 10})
Note: you'll need to run the block of code, then restart your notebook server entirely (not just the kernel) for the change to take effect.
Results on jupyter v4.0.6 running a Python 2.7.12 kernel
for i in range(0,100):
print i
0
1
2
3
4
limit_output extension: Maximum message size exceeded