- Python Data Analysis Cookbook
- Ivan Idris
- 535字
- 2021-07-14 11:05:36
Configuring IPython
IPython has an elaborate configuration and customization system. The components of the system are as follows:
- IPython provides default profiles, but we can create our own profiles
- Various settable options for the shell, kernel, Qt console, and notebook
- Customization of prompts and colors
- Extensions we saw in Keeping track of package versions and history in IPython notebooks
- Startup files per profile
I will demonstrate some of these components in this recipe.
Getting ready
You need IPython for this recipe, so (if necessary) have a look at the Getting ready section of Keeping track of package versions and history in IPython notebooks.
How to do it...
Let's start with a startup file. I have a directory in my home directory at .ipython/profile_default/startup
, which belongs to the default profile. This directory is meant for startup files. IPython detects Python files in this directory and executes them in lexical order of filenames. Because of the lexical order, it is convenient to name the startup files with a combination of digits and strings, for example, 0000-watermark.py
. Put the following code in the startup file:
get_ipython().magic('%load_ext watermark') get_ipython().magic('watermark -a "Ivan Idris" -v -p numpy,scipy,matplotlib -c \'%b %Y\' -w')
This startup file loads the extension we used in Keeping track of package versions and history in IPython notebooks and shows information about package versions. Other use cases include importing modules and defining functions. IPython stores commands in a SQLite database, so you could gather statistics to find common usage patterns. The following script prints source lines and associated counts from the database for the default profile sorted by counts (the code is in the ipython_history.py
file in this book's code bundle):
import sqlite3 from IPython.utils.path import get_ipython_dir import pprint import os def print_history(file): with sqlite3.connect(file) as con: c = con.cursor() c.execute("SELECT count(source_raw) as csr,\ source_raw FROM history\ GROUP BY source_raw\ ORDER BY csr") result = c.fetchall() pprint.pprint(result) c.close() hist_file = '%s/profile_default/history.sqlite' % get_ipython_dir() if os.path.exists(hist_file): print_history(hist_file) else: print("%s doesn't exist" % hist_file)
The highlighted SQL query does the bulk of the work. The code is self-explanatory. If it is not clear, I recommend reading Chapter 8, Text Mining and Social Network, of my book Python Data Analysis, Packt Publishing.
The other configuration option I mentioned is profiles. We can use the default profiles or create our own profiles on a per project or functionality basis. Profiles act as sandboxes and you can configure them separately. Here's the command to create a profile:
$ ipython profile create [newprofile]
The configuration files are Python files and their names end with _config.py
. In these files, you can set various IPython options. Set the option to automatically log the IPython session as follows:
c = get_config() c.TerminalInteractiveShell.logstart=True
The first line is usually included in configuration files and gets the root IPython configuration object. The last line tells IPython that we want to start logging immediately on startup so you don't have to type %logstart
.
Alternatively, you can also set the log file name with the following command:
c.TerminalInteractiveShell.logfile='mylog_file.py'
You can also use the following configuration line that ensures logging in append mode:
c.TerminalInteractiveShell.logappend='mylog_file.py'
See also
- Introduction to IPython configuration at http://ipython.org/ipython-doc/dev/config/intro.html#profiles (retrieved July 2015)
- Terminal IPython options documentation at http://ipython.org/ipython-doc/dev/config/options/terminal.html (retrieved July 2015)
- iOS面試一戰(zhàn)到底
- 程序員修煉之道:程序設(shè)計(jì)入門30講
- ASP.NET Core 2 and Vue.js
- 三維圖形化C++趣味編程
- 精通軟件性能測(cè)試與LoadRunner實(shí)戰(zhàn)(第2版)
- Mastering Swift 2
- Expert Data Visualization
- 一塊面包板玩轉(zhuǎn)Arduino編程
- Unity 3D/2D移動(dòng)開發(fā)實(shí)戰(zhàn)教程
- Python程序設(shè)計(jì)與算法基礎(chǔ)教程(第2版)(微課版)
- Microsoft 365 Certified Fundamentals MS-900 Exam Guide
- C++編程兵書
- .NET 4.0面向?qū)ο缶幊搪劊簯?yīng)用篇
- 大規(guī)模語言模型開發(fā)基礎(chǔ)與實(shí)踐
- Python Penetration Testing Essentials