#!/usr/bin/env python # coding: utf-8 # # # # ### Jupyter-Notebook # a.k.a # ### IPython-Notebook # ### - Suraj Deshmukh # ### @surajssd009005 # ## IPython-Interpreter # - IPython is a command shell for interactive computing in multiple programming languages # - It provides a rich architecture for interactive computing # - It comes with advanced features, which default python interpreter lacks # - It offers introspection, rich media, shell syntax, tab completion, and history. # ## IPython-Notebook # # - IPython Notebook is a web-based interactive computational environment for creating IPython notebooks # - It's an interactive computational environment, in which you can combine code execution, rich text, mathematics, plots and rich media # - A browser-based notebook with support for code, text, mathematical expressions, inline plots and other media # # # # - It started as a Python specific tool # - People started writing kernel for other languages # - Also they wanted to make it language agnostic so the project was renamed # - *Julia + Python + R = Jupyter* not limited to these three languages, but supports more than 40 programming languages # ### Features: # # - Execellent tool that combines code and documentation # - So can be called as *dynamic documentation* tool # - Tool for doing reproducible research # - Insert images, text, graphs, etc. # - Convert the notebook to HTML, Markdown, RST, PDF, Latex, etc. # - Display rich data representations (e.g. HTML / LaTeX / SVG) in the browser as a result of computations. # - Compose rich text using Markdown and HTML. # - Include mathematical equations, rendered directly in the browser by MathJax. # - Import standard Python scripts. # - In-browser editing, syntax highlighting, tab completion and autoindentation. # - Inline figures rendered by the matplotlib library with publication quality, in a range of formats (SVG / PDF / PNG). # ### Working: # # # ### Modes of execution # # - Command mode # - Editing mode # # Pressing **esc** in editing mode will take you to command mode. # Pressing **enter** on any cell will take you into editing mode. # ## Sample Python # # Python Interpreter behavior on Notebook # In[36]: print 'Hello World' 1 + 3 # - Press **Ctrl + Enter** to execute current cell # - Press **Shift + Enter** to execute current cell and select below # In[2]: 22 * 36 # In[1]: ans = raw_input('Do you find IPython Awesome? : ') print ans # Stop execution in the middle using I + I # In[3]: import time for _ in xrange(1000): time.sleep(10) # ## Specialities of IPython # ### Auto-complete # # Press **tab**, when you are stuck and it will auto-complete. # In[4]: import random # In[ ]: random.randint # Auto-complete also works with file names # In[5]: open('IPython Notebook.ipynb') # ### Getting help in IPython # List IPython Features # In[6]: get_ipython().show_usage() # In[7]: import time # Get help related to the module # In[8]: get_ipython().run_line_magic('pinfo', 'time') # Get function help # In[9]: get_ipython().run_line_magic('pinfo', 'time.sleep') # Open '**source code**' of the module or function # In[10]: get_ipython().run_line_magic('pinfo2', 'random') # In[11]: get_ipython().run_line_magic('pinfo2', 'random.randint') # ### Using OS commands in IPython # Use '**!**'*(bang)* infront of the shell command and execute it. # In[12]: get_ipython().system('pwd') # In[13]: for _ in xrange(2): get_ipython().system("espeak 'Hello World'") # Mix python code and shell code # In[14]: files = get_ipython().getoutput('ls') print files # Add python code in shell code using curly braces # In[16]: files = get_ipython().getoutput('ls') print files print get_ipython().system('echo {files[0].upper()}') # ### Other features # Wildcard search # In[17]: import os get_ipython().run_line_magic('psearch', 'os.*path*') # Output history # In[20]: Out # Input History # In[23]: In # History in bash style # In[ ]: get_ipython().run_line_magic('history', '-n') # It can also interpret commands copied from interpreter # In[24]: from collections import defaultdict s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] d = defaultdict(list) for k, v in s: d[k].append(v) d.items() # ## IPython Magics # # The magic function system provides a series of functions which allow you to control the behavior of IPython itself, plus a lot of system-type features. # # Ref: %magic # Open IPython quick reference # In[25]: get_ipython().run_line_magic('quickref', '') # Show help for all IPython Magic functions # In[26]: get_ipython().run_line_magic('magic', '') # List currently available magic functions. # In[27]: get_ipython().run_line_magic('lsmagic', '') # ### Create and Edit files in Notebook # Using cell magic to add text to a file # In[28]: get_ipython().run_cell_magic('file', 'file.txt', '\nYou can create a new file in this way.\nJust include the syntax above and then write the content below it and file will be created in the server\ndirectory.\n') # Reading file the python way # In[30]: #print open('file.txt').read() get_ipython().system('cat file.txt') # ### Timeit python functions # Time execution of a Python statement or expression (This is line magic) # In[31]: get_ipython().run_line_magic('timeit', 'range(100)') # In[32]: get_ipython().run_line_magic('timeit', 'xrange(100)') # This is a cell magic # In[33]: get_ipython().run_cell_magic('timeit', 'range(100)', 'range(1000) \n') # ### Run pure shell script # In[34]: get_ipython().run_cell_magic('bash', '', "echo 'I am in :' $PWD\necho 'Name of this pc is '\nwhoami\necho 'Files and directories in current directory include: '\nls\n") # ### IPython Exception Handling # In[ ]: def a(): p = 1 q = 'hi' print p + q def b(): a() b() # This command will make *trace* more verbose than it is. # In[ ]: get_ipython().run_line_magic('xmode', 'verbose') # In[ ]: def a(): p = 1 q = 'hi' print p + q def b(): a() b() # This is default mode of the trace. # In[ ]: get_ipython().run_line_magic('xmode', 'context') # ## References # # - http://ipython.org # - http://ipython.org/notebook.html # - https://en.wikipedia.org/wiki/IPython # - https://docs.python.org/2/library/collections.html#defaultdict-examples # - https://github.com/ipython/ipython-in-depth # - https://github.com/TwistedHardware/mltutorial/blob/master/notebooks/jupyter/1.Introduction.ipynb # # Thanks !! # # - github: surajssd # - bitbucket: suraj_deshmukh # - twitter: surajssd009005 # - email: surajssd009005@gmail.com # - site: https://deshmukhsuraj.wordpress.com/ # ## QA