#!/usr/bin/env python # coding: utf-8 # # IPython: beyond plain Python # When executing code in IPython, all valid Python syntax works as-is, but IPython provides a number of features designed to make the interactive experience more fluid and efficient. # ## First things first: running code, getting help # In the notebook, to run a cell of code, hit `Shift-Enter`. This executes the cell and puts the cursor in the next cell below, or makes a new one if you are at the end. Alternately, you can use: # # - `Alt-Enter` to force the creation of a new cell unconditionally (useful when inserting new content in the middle of an existing notebook). # - `Control-Enter` executes the cell and keeps the cursor in the same cell, useful for quick experimentation of snippets that you don't need to keep permanently. # In[1]: print("Hi") # Getting help: # In[2]: get_ipython().show_usage() # Typing `object_name?` will print all sorts of details about any object, including docstrings, function definition lines (for call arguments) and constructor details for classes. # In[3]: import collections get_ipython().run_line_magic('pinfo', 'collections.namedtuple') # In[4]: get_ipython().run_line_magic('pinfo2', 'collections.Counter') # In[5]: get_ipython().run_line_magic('psearch', '*int*') # An IPython quick reference card: # In[6]: get_ipython().run_line_magic('quickref', '') # ## Tab completion # Tab completion, especially for attributes, is a convenient way to explore the structure of any object you’re dealing with. Simply type `object_name.` to view the object’s attributes. Besides Python objects and keywords, tab completion also works on file and directory names. # In[ ]: collections. # ## The interactive workflow: input, output, history # In[7]: 2+10 # In[8]: _+10 # You can suppress the storage and rendering of output if you append `;` to the last cell (this comes in handy when plotting with matplotlib, for example): # In[9]: 10+20; # In[10]: _ # The output is stored in `_N` and `Out[N]` variables: # In[11]: _10 == Out[10] # And the last three have shorthands for convenience: # In[12]: from __future__ import print_function print('last output:', _) print('next one :', __) print('and next :', ___) # In[13]: In[11] # In[14]: _i # In[15]: _ii # In[16]: print('last input:', _i) print('next one :', _ii) print('and next :', _iii) # In[17]: get_ipython().run_line_magic('history', '-n 1-5') # **Exercise** # # Write the last 10 lines of history to a file named `log.py`. # ## Accessing the underlying operating system # In[18]: get_ipython().system('pwd') # In[19]: files = get_ipython().getoutput('ls') print("My current directory's files:") print(files) # In[20]: get_ipython().system('echo $files') # In[21]: get_ipython().system('echo {files[0].upper()}') # Note that all this is available even in multiline blocks: # In[22]: import os for i,f in enumerate(files): if f.endswith('ipynb'): get_ipython().system('echo {"%02d" % i} - "{os.path.splitext(f)[0]}"') else: print('--') # ## Beyond Python: magic functions # The IPyhton 'magic' functions are a set of commands, invoked by prepending one or two `%` signs to their name, that live in a namespace separate from your normal Python variables and provide a more command-like interface. They take flags with `--` and arguments without quotes, parentheses or commas. The motivation behind this system is two-fold: # # - To provide an orthogonal namespace for controlling IPython itself and exposing other system-oriented functionality. # # - To expose a calling mode that requires minimal verbosity and typing while working interactively. Thus the inspiration taken from the classic Unix shell style for commands. # In[23]: get_ipython().run_line_magic('magic', '') # Line vs cell magics: # In[24]: get_ipython().run_line_magic('timeit', 'list(range(1000))') # In[25]: get_ipython().run_cell_magic('timeit', '', 'list(range(10))\nlist(range(100))\n') # Line magics can be used even inside code blocks: # In[26]: for i in range(1, 5): size = i*100 print('size:', size, end=' ') get_ipython().run_line_magic('timeit', 'list(range(size))') # Magics can do anything they want with their input, so it doesn't have to be valid Python: # In[27]: get_ipython().run_cell_magic('bash', '', 'echo "My shell is:" $SHELL\necho "My disk usage is:"\ndf -h\n') # Another interesting cell magic: create any file you want locally from the notebook: # In[28]: get_ipython().run_cell_magic('writefile', 'test.txt', 'This is a test file!\nIt can contain anything I want...\n\nAnd more...\n') # In[29]: get_ipython().system('cat test.txt') # Let's see what other magics are currently defined in the system: # In[30]: get_ipython().run_line_magic('lsmagic', '') # ## Running normal Python code: execution and errors # Not only can you input normal Python code, you can even paste straight from a Python or IPython shell session: # In[31]: # Fibonacci series: # the sum of two elements defines the next a, b = 0, 1 while b < 10: print(b) a, b = b, a+b # In[32]: for i in range(10): print(i, end=' ') # And when your code produces errors, you can control how they are displayed with the `%xmode` magic: # In[33]: get_ipython().run_cell_magic('writefile', 'mod.py', '\ndef f(x):\n return 1.0/(x-1)\n\ndef g(y):\n return f(y+1)\n') # Now let's call the function `g` with an argument that would produce an error: # In[34]: import mod mod.g(0) # In[35]: get_ipython().run_line_magic('xmode', 'plain') mod.g(0) # In[36]: get_ipython().run_line_magic('xmode', 'verbose') mod.g(0) # The default `%xmode` is "context", which shows additional context but not all local variables. Let's restore that one for the rest of our session. # In[37]: get_ipython().run_line_magic('xmode', 'context') # ## Running code in other languages with special `%%` magics # In[38]: get_ipython().run_cell_magic('perl', '', '@months = ("July", "August", "September");\nprint $months[0];\n') # In[39]: get_ipython().run_cell_magic('ruby', '', 'name = "world"\nputs "Hello #{name.capitalize}!"\n') # ## Raw Input in the notebook # Since 1.0 the IPython notebook web application support `raw_input` which for example allow us to invoke the `%debug` magic in the notebook: # In[40]: mod.g(0) # In[41]: get_ipython().run_line_magic('debug', '') # Don't foget to exit your debugging session. Raw input can of course be use to ask for user input: # In[42]: enjoy = input('Are you enjoying this tutorial? ') print('enjoy is:', enjoy) # ## Plotting in the notebook # This magic configures matplotlib to render its figures inline: # In[43]: get_ipython().run_line_magic('matplotlib', 'inline') # In[44]: import numpy as np import matplotlib.pyplot as plt # In[45]: x = np.linspace(0, 2*np.pi, 300) y = np.sin(x**2) plt.plot(x, y) plt.title("A little chirp") fig = plt.gcf() # let's keep the figure object around for later... # ## The IPython kernel/client model # In[46]: get_ipython().run_line_magic('connect_info', '') # We can connect automatically a Qt Console to the currently running kernel with the `%qtconsole` magic, or by typing `ipython console --existing ` in any terminal: # In[47]: get_ipython().run_line_magic('qtconsole', '')