My objective is to call ipython
, while also logging all input/output to IPython, and see something like:
stu@stumac ~ $ ipython
In [1]: exit
stu@stumac ~ $
The banner can be easily removed if I set
c.TerminalIPythonApp.display_banner = False
in my
~/.ipython/profile-default/ipython_config.py
file.
But how do I get this clean of a startup while also logging things?
On a fresh install, if I start IPython with no parameters I see:
sente@og ~ $ ipython
Python 2.7.3 (default, Jun 20 2013, 12:50:58)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: exit
sente@og ~ $
If I pass a logfile=logfile.txt
argument when invoking IPython I see:
sente@og ~ $ ipython --logfile=logfile.txt
Activating auto-logging. Current session state plus future input saved.
Filename : logfile.txt
Mode : backup
Output logging : False
Raw input log : False
Timestamping : False
State : active
Python 2.7.3 (default, Jun 20 2013, 12:50:58)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: exit
sente@og ~ $
How can I use logging without adding the extra clutter to my terminal:
Activating auto-logging. Current session state plus future input saved.
Filename : logfile.txt
Mode : backup
Output logging : False
Raw input log : False
Timestamping : False
State : active
On other machines I have IPython configured to automatically log things by having a .ipython/profile_default/startup/01-log-everything.py
which contains the lines:
from time import strftime
import os.path
ip = get_ipython()
ldir = ip.profile_dir.log_dir
fname = strftime('%Y-%m-%d-%H-%M-%S') + ".py"
filename = os.path.join(ldir, fname)
ip.run_line_magic('logstart', '-o %s append' % filename)
which results in the same clutter as when I add --logfile=logfile.txt
Any help on how to properly do this would be appreciated. I could if nothing else redirect sys.stdout, configure logging and then reset sys.stdout but I'm hoping there's a less hackish solution.
To log with no banner use the --no-banner flag.
ipython --no-banner --logfile='logfile.txt'
This can be found in the help file displayed with the --help-all flag.
ipython --help-all
After looking at the IPython source, namely:
It seems the only way to actually achieve what I want -- complete logging without anything echo'd upon startup -- is to redirect or mute sys.stdout
while initializing the logging.
I've created a startup file: ~/.ipython/profile_default/startup/01-log-everything.py which does this, the code is:
from time import strftime
import os.path
import contextlib
import cStringIO
import sys
# create a context which we can use for any block which we can use for any
# block which we do not want to print stdout
# -- taken from http://stackoverflow.com/a/2829036/217652
@contextlib.contextmanager
def nostdout():
save_stdout = sys.stdout
sys.stdout = cStringIO.StringIO()
yield
sys.stdout = save_stdout
ip = get_ipython()
ldir = ip.profile_dir.log_dir
fname = strftime('%Y-%m-%d-%H-%M-%S') + ".py"
filename = os.path.join(ldir, fname)
# stdout is now muted
with nostdout():
ip.run_line_magic('logstart', '-o %s append' % filename)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With