Databricks Lab & Excercise – Notebook

Here’s my Top 15 commands to try first — grouped into environment checks, Spark basics, and data handling so you learn in a logical order.


1–5: Environment & Python Basics in Databricks

# 1. Check Python version
!python --version

# 2. List installed Python packages
%pip list

# 3. Display current working directory
%fs pwd

# 4. List files in DBFS root
%fs ls /

# 5. Display help for Databricks magic commands
%magic

6–10: Spark Session & Cluster Basics

# 6. Create a Spark session (already available by default in Databricks)
spark

# 7. Check Spark version
spark.version

# 8. View Spark configuration settings
spark.conf.getAll()

# 9. View list of databases
spark.sql("SHOW DATABASES").show()

# 10. Check available tables in default database
spark.sql("SHOW TABLES").show()

11–15: Data Creation, Querying, and Display

from pyspark.sql import Row

# 11. Create a simple DataFrame
data = [Row(name="Alice", age=29), Row(name="Bob", age=35)]
df = spark.createDataFrame(data)

# 12. Show DataFrame
df.show()

# 13. Describe DataFrame schema
df.printSchema()

# 14. Run SQL on the DataFrame
df.createOrReplaceTempView("people")
spark.sql("SELECT name, age FROM people WHERE age > 30").show()

# 15. Save DataFrame as table in default DB
df.write.saveAsTable("people_table")

Bonus Tips for First Run

  • Use %python explicitly if you’re mixing languages in the same notebook.
  • %fs is for DBFS (Databricks File System) commands — good for browsing uploaded data.
  • %sql lets you run SQL directly in a cell without spark.sql(...).
  • Try display(df) — it gives a nice interactive table UI instead of plain text.


IPython's 'magic' functions
===========================

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. There are two kinds of magics, line-oriented and cell-oriented.

Line magics are prefixed with the % character and work much like OS
command-line calls: they get as an argument the rest of the line, where
arguments are passed without parentheses or quotes.  For example, this will
time the given statement::

        %timeit range(1000)

Cell magics are prefixed with a double %%, and they are functions that get as
an argument not only the rest of the line, but also the lines below it in a
separate argument.  These magics are called with two arguments: the rest of the
call line and the body of the cell, consisting of the lines below the first.
For example::

        %%timeit x = numpy.random.randn((100, 100))
        numpy.linalg.svd(x)

will time the execution of the numpy svd routine, running the assignment of x
as part of the setup phase, which is not timed.

In a line-oriented client (the terminal or Qt console IPython), starting a new
input with %% will automatically enter cell mode, and IPython will continue
reading input until a blank line is given.  In the notebook, simply type the
whole cell as one entity, but keep in mind that the %% escape can only be at
the very start of the cell.

NOTE: If you have 'automagic' enabled (via the command line option or with the
%automagic function), you don't need to type in the % explicitly for line
magics; cell magics always require an explicit '%%' escape.  By default,
IPython ships with automagic on, so you should only rarely need the % escape.

Example: typing '%cd mydir' (without the quotes) changes your working directory
to 'mydir', if it exists.

For a list of the available magic functions, use %lsmagic. For a description
of any of them, type %magic_name?, e.g. '%cd?'.

Currently the magic system has the following functions:
%alias:
    Define an alias for a system command.
    
    '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
    
    Then, typing 'alias_name params' will execute the system command 'cmd
    params' (from your underlying operating system).
    
    Aliases have lower precedence than magic functions and Python normal
    variables, so if 'foo' is both a Python variable and an alias, the
    alias can not be executed until 'del foo' removes the Python variable.
    
    You can use the %l specifier in an alias definition to represent the
    whole line when the alias is called.  For example::
    
      In [2]: alias bracket echo "Input in brackets: <%l>"
      In [3]: bracket hello world
      Input in brackets: <hello world>
    
    You can also define aliases with parameters using %s specifiers (one
    per parameter)::
    
      In [1]: alias parts echo first %s second %s
      In [2]: %parts A B
      first A second B
      In [3]: %parts A
      Incorrect number of arguments: 2 expected.
      parts is an alias to: 'echo first %s second %s'
    
    Note that %l and %s are mutually exclusive.  You can only use one or
    the other in your aliases.
    
    Aliases expand Python variables just like system calls using ! or !!
    do: all expressions prefixed with '$' get expanded.  For details of
    the semantic rules, see PEP-215:
    https://peps.python.org/pep-0215/.  This is the library used by
    IPython for variable expansion.  If you want to access a true shell
    variable, an extra $ is necessary to prevent its expansion by
    IPython::
    
      In [6]: alias show echo
      In [7]: PATH='A Python string'
      In [8]: show $PATH
      A Python string
      In [9]: show $$PATH
      /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
    
    You can use the alias facility to access all of $PATH.  See the %rehashx
    function, which automatically creates aliases for the contents of your
    $PATH.
    
    If called with no parameters, %alias prints the current alias table
    for your system.  For posix systems, the default aliases are 'cat',
    'cp', 'mv', 'rm', 'rmdir', and 'mkdir', and other platform-specific
    aliases are added.  For windows-based systems, the default aliases are
    'copy', 'ddir', 'echo', 'ls', 'ldir', 'mkdir', 'ren', and 'rmdir'.
    
    You can see the definition of alias by adding a question mark in the
    end::
    
      In [1]: cat?
      Repr: <alias cat for 'cat'>
%alias_magic:
    ::
    
      %alias_magic [-l] [-c] [-p PARAMS] name target
    
    Create an alias for an existing line or cell magic.
    
    Examples
    --------
    ::
    
      In [1]: %alias_magic t timeit
      Created `%t` as an alias for `%timeit`.
      Created `%%t` as an alias for `%%timeit`.
    
      In [2]: %t -n1 pass
      107 ns ± 43.6 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)
    
      In [3]: %%t -n1
         ...: pass
         ...:
      107 ns ± 58.3 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)
    
      In [4]: %alias_magic --cell whereami pwd
      UsageError: Cell magic function `%%pwd` not found.
      In [5]: %alias_magic --line whereami pwd
      Created `%whereami` as an alias for `%pwd`.
    
      In [6]: %whereami
      Out[6]: '/home/testuser'
    
      In [7]: %alias_magic h history -p "-l 30" --line
      Created `%h` as an alias for `%history -l 30`.
    
    positional arguments:
      name                  Name of the magic to be created.
      target                Name of the existing line or cell magic.
    
    options:
      -l, --line            Create a line magic alias.
      -c, --cell            Create a cell magic alias.
      -p PARAMS, --params PARAMS
                            Parameters passed to the magic function.
%autoawait:
    
    Allow to change the status of the autoawait option.
    
    This allow you to set a specific asynchronous code runner.
    
    If no value is passed, print the currently used asynchronous integration
    and whether it is activated.
    
    It can take a number of value evaluated in the following order:
    
    - False/false/off deactivate autoawait integration
    - True/true/on activate autoawait integration using configured default
      loop
    - asyncio/curio/trio activate autoawait integration and use integration
      with said library.
    
    - `sync` turn on the pseudo-sync integration (mostly used for
      `IPython.embed()` which does not run IPython with a real eventloop and
      deactivate running asynchronous code. Turning on Asynchronous code with
      the pseudo sync loop is undefined behavior and may lead IPython to crash.
    
    If the passed parameter does not match any of the above and is a python
    identifier, get said object from user namespace and set it as the
    runner, and activate autoawait.
    
    If the object is a fully qualified object name, attempt to import it and
    set it as the runner, and activate autoawait.
    
    The exact behavior of autoawait is experimental and subject to change
    across version of IPython and Python.
%autocall:
    Make functions callable without having to type parentheses.
    
    Usage:
    
       %autocall [mode]
    
    The mode can be one of: 0->Off, 1->Smart, 2->Full.  If not given, the
    value is toggled on and off (remembering the previous state).
    
    In more detail, these values mean:
    
    0 -> fully disabled
    
    1 -> active, but do not apply if there are no arguments on the line.
    
    In this mode, you get::
    
      In [1]: callable
      Out[1]: <built-in function callable>
    
      In [2]: callable 'hello'
      ------> callable('hello')
      Out[2]: False
    
    2 -> Active always.  Even if no arguments are present, the callable
    object is called::
    
      In [2]: float
      ------> float()
      Out[2]: 0.0
    
    Note that even with autocall off, you can still use '/' at the start of
    a line to treat the first argument on the command line as a function
    and add parentheses to it::
    
      In [8]: /str 43
      ------> str(43)
      Out[8]: '43'
    
    # all-random (note for auto-testing)
%automagic:
    Make magic functions callable without having to type the initial %.
    
    Without arguments toggles on/off (when off, you must call it as
    %automagic, of course).  With arguments it sets the value, and you can
    use any of (case insensitive):
    
     - on, 1, True: to activate
    
     - off, 0, False: to deactivate.
    
    Note that magic functions have lowest priority, so if there's a
    variable whose name collides with that of a magic fn, automagic won't
    work for that function (you get the variable instead). However, if you
    delete the variable (del var), the previously shadowed magic function
    becomes visible to automagic again.
%autosave:
    Set the autosave interval in the notebook (in seconds).
    
    The default value is 120, or two minutes.
    ``%autosave 0`` will disable autosave.
    
    This magic only has an effect when called from the notebook interface.
    It has no effect when called in a startup file.
%bookmark:
    Manage IPython's bookmark system.
    
    %bookmark <name>       - set bookmark to current dir
    %bookmark <name> <dir> - set bookmark to <dir>
    %bookmark -l           - list all bookmarks
    %bookmark -d <name>    - remove bookmark
    %bookmark -r           - remove all bookmarks
    
    You can later on access a bookmarked folder with::
    
      %cd -b <name>
    
    or simply '%cd <name>' if there is no directory called <name> AND
    there is such a bookmark defined.
    
    Your bookmarks persist through IPython sessions, but they are
    associated with each profile.
%cat:
    Alias for `!cat`
%cd:
    Change the current working directory.
    
    This command automatically maintains an internal list of directories
    you visit during your IPython session, in the variable ``_dh``. The
    command :magic:`%dhist` shows this history nicely formatted. You can
    also do ``cd -<tab>`` to see directory history conveniently.
    Usage:
    
      - ``cd 'dir'``: changes to directory 'dir'.
      - ``cd -``: changes to the last visited directory.
      - ``cd -<n>``: changes to the n-th directory in the directory history.
      - ``cd --foo``: change to directory that matches 'foo' in history
      - ``cd -b <bookmark_name>``: jump to a bookmark set by %bookmark
      - Hitting a tab key after ``cd -b`` allows you to tab-complete
        bookmark names.
    
      .. note::
        ``cd <bookmark_name>`` is enough if there is no directory
        ``<bookmark_name>``, but a bookmark with the name exists.
    
    Options:
    
    -q               Be quiet. Do not print the working directory after the
                     cd command is executed. By default IPython's cd
                     command does print this directory, since the default
                     prompts do not display path information.
    
    .. note::
       Note that ``!cd`` doesn't work for this purpose because the shell
       where ``!command`` runs is immediately discarded after executing
       'command'.
    
    Examples
    --------
    ::
    
      In [10]: cd parent/child
      /home/tsuser/parent/child
%clear:
    Clear the terminal.
%code_wrap:
    ::
    
      %code_wrap [--remove] [--list] [--list-all] [name]
    
    Simple magic to quickly define a code transformer for all IPython's future input.
    
    ``__code__`` and ``__ret__`` are special variable that represent the code to run
    and the value of the last expression of ``__code__`` respectively.
    
    Examples
    --------
    
    .. ipython::
    
        In [1]: %%code_wrap before_after
           ...: print('before')
           ...: __code__
           ...: print('after')
           ...: __ret__
    
        In [2]: 1
        before
        after
        Out[2]: 1
    
        In [3]: %code_wrap --list
        before_after
    
        In [4]: %code_wrap --list-all
        before_after :
            print('before')
            __code__
            print('after')
            __ret__
    
        In [5]: %code_wrap --remove before_after
    
    positional arguments:
      name
    
    options:
      --remove    remove the current transformer
      --list      list existing transformers name
      --list-all  list existing transformers name and code template
%colors:
    Switch color scheme for prompts, info system and exception handlers.
    
    Currently implemented schemes: NoColor, Linux, LightBG.
    
    Color scheme names are not case-sensitive.
    
    Examples
    --------
    To get a plain black and white terminal::
    
      %colors nocolor
%conda:
    No documentation
%config:
    configure IPython
    
        %config Class[.trait=value]
    
    This magic exposes most of the IPython config system. Any
    Configurable class should be able to be configured with the simple
    line::
    
        %config Class.trait=value
    
    Where `value` will be resolved in the user's namespace, if it is an
    expression or variable name.
    
    Examples
    --------
    
    To see what classes are available for config, pass no arguments::
    
        In [1]: %config
        Available objects for config:
            AliasManager
            DisplayFormatter
            HistoryManager
            IPCompleter
            LoggingMagics
            MagicsManager
            OSMagics
            PrefilterManager
            ScriptMagics
            TerminalInteractiveShell
    
    To view what is configurable on a given class, just pass the class
    name::
    
        In [2]: %config LoggingMagics
        LoggingMagics(Magics) options
        ---------------------------
        LoggingMagics.quiet=<Bool>
            Suppress output of log state when logging is enabled
            Current: False
    
    but the real use is in setting values::
    
        In [3]: %config LoggingMagics.quiet = True
    
    and these values are read from the user_ns if they are variables::
    
        In [4]: feeling_quiet=False
    
        In [5]: %config LoggingMagics.quiet = feeling_quiet
%connect_info:
    Print information for connecting other clients to this kernel
    
    It will print the contents of this session's connection file, as well as
    shortcuts for local clients.
    
    In the simplest case, when called from the most recently launched kernel,
    secondary clients can be connected, simply with:
    
    $> jupyter <app> --existing
%cp:
    Alias for `!cp`
%debug:
    ::
    
      %debug [--breakpoint FILE:LINE] [statement ...]
    
    Activate the interactive debugger.
    
    This magic command support two ways of activating debugger.
    One is to activate debugger before executing code.  This way, you
    can set a break point, to step through the code from the point.
    You can use this mode by giving statements to execute and optionally
    a breakpoint.
    
    The other one is to activate debugger in post-mortem mode.  You can
    activate this mode simply running %debug without any argument.
    If an exception has just occurred, this lets you inspect its stack
    frames interactively.  Note that this will always work only on the last
    traceback that occurred, so you must call this quickly after an
    exception that you wish to inspect has fired, because if another one
    occurs, it clobbers the previous one.
    
    If you want IPython to automatically do this on every exception, see
    the %pdb magic for more details.
    
    .. versionchanged:: 7.3
        When running code, user variables are no longer expanded,
        the magic line is always left unmodified.
    
    positional arguments:
      statement             Code to run in debugger. You can omit this in cell
                            magic mode.
    
    options:
      --breakpoint <FILE:LINE>, -b <FILE:LINE>
                            Set break point at LINE in FILE.
%dhist:
    Print your history of visited directories.
    
    %dhist       -> print full history
    %dhist n     -> print last n entries only
    %dhist n1 n2 -> print entries between n1 and n2 (n2 not included)
    
    This history is automatically maintained by the %cd command, and
    always available as the global list variable _dh. You can use %cd -<n>
    to go to directory number <n>.
    
    Note that most of time, you should view directory history by entering
    cd -<TAB>.
%dirs:
    Return the current directory stack.
%doctest_mode:
    Toggle doctest mode on and off.
    
    This mode is intended to make IPython behave as much as possible like a
    plain Python shell, from the perspective of how its prompts, exceptions
    and output look.  This makes it easy to copy and paste parts of a
    session into doctests.  It does so by:
    
    - Changing the prompts to the classic ``>>>`` ones.
    - Changing the exception reporting mode to 'Plain'.
    - Disabling pretty-printing of output.
    
    Note that IPython also supports the pasting of code snippets that have
    leading '>>>' and '...' prompts in them.  This means that you can paste
    doctests from files or docstrings (even if they have leading
    whitespace), and the code will execute correctly.  You can then use
    '%history -t' to see the translated history; this will give you the
    input after removal of all the leading prompts and whitespace, which
    can be pasted back into an editor.
    
    With these features, you can switch into this mode easily whenever you
    need to do testing and changes to doctests, without having to leave
    your existing IPython session.
%ed:
    Alias for `%edit`.
%edit:
    Bring up an editor and execute the resulting code.
    
    Usage:
      %edit [options] [args]
    
    %edit runs an external text editor. You will need to set the command for
    this editor via the ``TerminalInteractiveShell.editor`` option in your
    configuration file before it will work.
    
    This command allows you to conveniently edit multi-line code right in
    your IPython session.
    
    If called without arguments, %edit opens up an empty editor with a
    temporary file and will execute the contents of this file when you
    close it (don't forget to save it!).
    
    Options:
    
    -n <number>
      Open the editor at a specified line number. By default, the IPython
      editor hook uses the unix syntax 'editor +N filename', but you can
      configure this by providing your own modified hook if your favorite
      editor supports line-number specifications with a different syntax.
    
    -p
      Call the editor with the same data as the previous time it was used,
      regardless of how long ago (in your current session) it was.
    
    -r
      Use 'raw' input. This option only applies to input taken from the
      user's history.  By default, the 'processed' history is used, so that
      magics are loaded in their transformed version to valid Python.  If
      this option is given, the raw input as typed as the command line is
      used instead.  When you exit the editor, it will be executed by
      IPython's own processor.
    
    Arguments:
    
    If arguments are given, the following possibilities exist:
    
    - The arguments are numbers or pairs of colon-separated numbers (like
      1 4:8 9). These are interpreted as lines of previous input to be
      loaded into the editor. The syntax is the same of the %macro command.
    
    - If the argument doesn't start with a number, it is evaluated as a
      variable and its contents loaded into the editor. You can thus edit
      any string which contains python code (including the result of
      previous edits).
    
    - If the argument is the name of an object (other than a string),
      IPython will try to locate the file where it was defined and open the
      editor at the point where it is defined. You can use ``%edit function``
      to load an editor exactly at the point where 'function' is defined,
      edit it and have the file be executed automatically.
    
      If the object is a macro (see %macro for details), this opens up your
      specified editor with a temporary file containing the macro's data.
      Upon exit, the macro is reloaded with the contents of the file.
    
      Note: opening at an exact line is only supported under Unix, and some
      editors (like kedit and gedit up to Gnome 2.8) do not understand the
      '+NUMBER' parameter necessary for this feature. Good editors like
      (X)Emacs, vi, jed, pico and joe all do.
    
    - If the argument is not found as a variable, IPython will look for a
      file with that name (adding .py if necessary) and load it into the
      editor. It will execute its contents with execfile() when you exit,
      loading any code in the file into your interactive namespace.
    
    Unlike in the terminal, this is designed to use a GUI editor, and we do
    not know when it has closed. So the file you edit will not be
    automatically executed or printed.
    
    Note that %edit is also available through the alias %ed.
%env:
    Get, set, or list environment variables.
    
    Usage:
    
      :``%env``: lists all environment variables/values
      :``%env var``: get value for var
      :``%env var val``: set value for var
      :``%env var=val``: set value for var
      :``%env var=$val``: set value for var, using python expansion if possible
%gui:
    Enable or disable IPython GUI event loop integration.
    
    %gui [GUINAME]
    
    This magic replaces IPython's threaded shells that were activated
    using the (pylab/wthread/etc.) command line flags.  GUI toolkits
    can now be enabled at runtime and keyboard
    interrupts should work without any problems.  The following toolkits
    are supported:  wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::
    
        %gui wx      # enable wxPython event loop integration
        %gui qt      # enable PyQt/PySide event loop integration
                     # with the latest version available.
        %gui qt6     # enable PyQt6/PySide6 event loop integration
        %gui qt5     # enable PyQt5/PySide2 event loop integration
        %gui gtk     # enable PyGTK event loop integration
        %gui gtk3    # enable Gtk3 event loop integration
        %gui gtk4    # enable Gtk4 event loop integration
        %gui tk      # enable Tk event loop integration
        %gui osx     # enable Cocoa event loop integration
                     # (requires %matplotlib 1.1)
        %gui         # disable all event loop integration
    
    WARNING:  after any of these has been called you can simply create
    an application object, but DO NOT start the event loop yourself, as
    we have already handled that.
%hist:
    Alias for `%history`.
%history:
    ::
    
      %history [-n] [-o] [-p] [-t] [-f FILENAME] [-g [PATTERN ...]]
                   [-l [LIMIT]] [-u]
                   [range ...]
    
    Print input history (_i<n> variables), with most recent last.
    
    By default, input history is printed without line numbers so it can be
    directly pasted into an editor. Use -n to show them.
    
    By default, all input history from the current session is displayed.
    Ranges of history can be indicated using the syntax:
    
    ``4``
        Line 4, current session
    ``4-6``
        Lines 4-6, current session
    ``243/1-5``
        Lines 1-5, session 243
    ``~2/7``
        Line 7, session 2 before current
    ``~8/1-~6/5``
        From the first line of 8 sessions ago, to the fifth line of 6
        sessions ago.
    
    Multiple ranges can be entered, separated by spaces
    
    The same syntax is used by %macro, %save, %edit, %rerun
    
    Examples
    --------
    ::
    
      In [6]: %history -n 4-6
      4:a = 12
      5:print(a**2)
      6:%history -n 4-6
    
    positional arguments:
      range
    
    options:
      -n                  print line numbers for each input. This feature is only
                          available if numbered prompts are in use.
      -o                  also print outputs for each input.
      -p                  print classic '>>>' python prompts before each input.
                          This is useful for making documentation, and in
                          conjunction with -o, for producing doctest-ready output.
      -t                  print the 'translated' history, as IPython understands
                          it. IPython filters your input and converts it all into
                          valid Python source before executing it (things like
                          magics or aliases are turned into function calls, for
                          example). With this option, you'll see the native
                          history instead of the user-entered version: '%cd /'


*** WARNING: max output size exceeded, skipping output. ***

ension`` function.
%who:
    Print all interactive variables, with some minimal formatting.
    
    If any arguments are given, only variables whose type matches one of
    these are printed.  For example::
    
      %who function str
    
    will only list functions and strings, excluding all other types of
    variables.  To find the proper type names, simply use type(var) at a
    command line to see how python prints type names.  For example:
    
    ::
    
      In [1]: type('hello')
      Out[1]: <type 'str'>
    
    indicates that the type name for strings is 'str'.
    
    ``%who`` always excludes executed names loaded through your configuration
    file and things which are internal to IPython.
    
    This is deliberate, as typically you may load many modules and the
    purpose of %who is to show you only what you've manually defined.
    
    Examples
    --------
    
    Define two variables and list them with who::
    
      In [1]: alpha = 123
    
      In [2]: beta = 'test'
    
      In [3]: %who
      alpha   beta
    
      In [4]: %who int
      alpha
    
      In [5]: %who str
      beta
%who_ls:
    Return a sorted list of all interactive variables.
    
    If arguments are given, only variables of types matching these
    arguments are returned.
    
    Examples
    --------
    Define two variables and list them with who_ls::
    
      In [1]: alpha = 123
    
      In [2]: beta = 'test'
    
      In [3]: %who_ls
      Out[3]: ['alpha', 'beta']
    
      In [4]: %who_ls int
      Out[4]: ['alpha']
    
      In [5]: %who_ls str
      Out[5]: ['beta']
%whos:
    Like %who, but gives some extra information about each variable.
    
    The same type filtering of %who can be applied here.
    
    For all variables, the type is printed. Additionally it prints:
    
      - For {},[],(): their length.
    
      - For numpy arrays, a summary with shape, number of
        elements, typecode and size in memory.
    
      - Everything else: a string representation, snipping their middle if
        too long.
    
    Examples
    --------
    Define two variables and list them with whos::
    
      In [1]: alpha = 123
    
      In [2]: beta = 'test'
    
      In [3]: %whos
      Variable   Type        Data/Info
      --------------------------------
      alpha      int         123
      beta       str         test
%xdel:
    Delete a variable, trying to clear it from anywhere that
    IPython's machinery has references to it. By default, this uses
    the identity of the named object in the user namespace to remove
    references held under other names. The object is also removed
    from the output history.
    
    Options
      -n : Delete the specified name from all namespaces, without
      checking their identity.
%xmode:
    Switch modes for the exception handlers.
    
    Valid modes: Plain, Context, Verbose, and Minimal.
    
    If called without arguments, acts as a toggle.
    
    When in verbose mode the value `--show` (and `--hide`)
    will respectively show (or hide) frames with ``__tracebackhide__ =
    True`` value set.
%%!:
    Shell execute - run shell command and capture output (!! is short-hand).
    
    %sx command
    
    IPython will run the given command using commands.getoutput(), and
    return the result formatted as a list (split on '\n').  Since the
    output is _returned_, it will be stored in ipython's regular output
    cache Out[N] and in the '_N' automatic variables.
    
    Notes:
    
    1) If an input line begins with '!!', then %sx is automatically
    invoked.  That is, while::
    
      !ls
    
    causes ipython to simply issue system('ls'), typing::
    
      !!ls
    
    is a shorthand equivalent to::
    
      %sx ls
    
    2) %sx differs from %sc in that %sx automatically splits into a list,
    like '%sc -l'.  The reason for this is to make it as easy as possible
    to process line-oriented shell output via further python commands.
    %sc is meant to provide much finer control, but requires more
    typing.
    
    3) Just like %sc -l, this is a list with special attributes:
    ::
    
      .l (or .list) : value as list.
      .n (or .nlstr): value as newline-separated string.
      .s (or .spstr): value as whitespace-separated string.
    
    This is very useful when trying to use such lists as arguments to
    system commands.
%%HTML:
    Alias for `%%html`.
%%SVG:
    Alias for `%%svg`.
%%bash:
    %%bash script magic
    
    Run cells with bash in a subprocess.
    
    This is a shortcut for `%%script bash`
%%capture:
    ::
    
      %capture [--no-stderr] [--no-stdout] [--no-display] [output]
    
    run the cell, capturing stdout, stderr, and IPython's rich display() calls.
    
    positional arguments:
      output        The name of the variable in which to store output. This is a
                    utils.io.CapturedIO object with stdout/err attributes for the
                    text of the captured output. CapturedOutput also has a show()
                    method for displaying the output, and __call__ as well, so you
                    can use that to quickly display the output. If unspecified,
                    captured output is discarded.
    
    options:
      --no-stderr   Don't capture stderr.
      --no-stdout   Don't capture stdout.
      --no-display  Don't capture IPython's rich display.
%%code_wrap:
    ::
    
      %code_wrap [--remove] [--list] [--list-all] [name]
    
    Simple magic to quickly define a code transformer for all IPython's future input.
    
    ``__code__`` and ``__ret__`` are special variable that represent the code to run
    and the value of the last expression of ``__code__`` respectively.
    
    Examples
    --------
    
    .. ipython::
    
        In [1]: %%code_wrap before_after
           ...: print('before')
           ...: __code__
           ...: print('after')
           ...: __ret__
    
        In [2]: 1
        before
        after
        Out[2]: 1
    
        In [3]: %code_wrap --list
        before_after
    
        In [4]: %code_wrap --list-all
        before_after :
            print('before')
            __code__
            print('after')
            __ret__
    
        In [5]: %code_wrap --remove before_after
    
    positional arguments:
      name
    
    options:
      --remove    remove the current transformer
      --list      list existing transformers name
      --list-all  list existing transformers name and code template
%%debug:
    ::
    
      %debug [--breakpoint FILE:LINE] [statement ...]
    
    Activate the interactive debugger.
    
    This magic command support two ways of activating debugger.
    One is to activate debugger before executing code.  This way, you
    can set a break point, to step through the code from the point.
    You can use this mode by giving statements to execute and optionally
    a breakpoint.
    
    The other one is to activate debugger in post-mortem mode.  You can
    activate this mode simply running %debug without any argument.
    If an exception has just occurred, this lets you inspect its stack
    frames interactively.  Note that this will always work only on the last
    traceback that occurred, so you must call this quickly after an
    exception that you wish to inspect has fired, because if another one
    occurs, it clobbers the previous one.
    
    If you want IPython to automatically do this on every exception, see
    the %pdb magic for more details.
    
    .. versionchanged:: 7.3
        When running code, user variables are no longer expanded,
        the magic line is always left unmodified.
    
    positional arguments:
      statement             Code to run in debugger. You can omit this in cell
                            magic mode.
    
    options:
      --breakpoint <FILE:LINE>, -b <FILE:LINE>
                            Set break point at LINE in FILE.
%%file:
    Alias for `%%writefile`.
%%html:
    ::
    
      %html [--isolated]
    
    Render the cell as a block of HTML
    
    options:
      --isolated  Annotate the cell as 'isolated'. Isolated cells are rendered
                  inside their own <iframe> tag
%%javascript:
    Run the cell block of Javascript code
    
    Starting with IPython 8.0 %%javascript is pending deprecation to be replaced
    by a more flexible system
    
    Please See https://github.com/ipython/ipython/issues/13376
%%js:
    Run the cell block of Javascript code
    
    Alias of `%%javascript`
    
    Starting with IPython 8.0 %%javascript is pending deprecation to be replaced
    by a more flexible system
    
    Please See https://github.com/ipython/ipython/issues/13376
%%latex:
    Render the cell as a block of LaTeX
    
    The subset of LaTeX which is supported depends on the implementation in
    the client.  In the Jupyter Notebook, this magic only renders the subset
    of LaTeX defined by MathJax
    [here](https://docs.mathjax.org/en/v2.5-latest/tex.html).
%%markdown:
    Render the cell as Markdown text block
%%perl:
    %%perl script magic
    
    Run cells with perl in a subprocess.
    
    This is a shortcut for `%%script perl`
%%prun:
    Run a statement through the python code profiler.
    
    Usage, in line mode:
      %prun [options] statement
    
    Usage, in cell mode:
      %%prun [options] [statement]
      code...
      code...
    
    In cell mode, the additional code lines are appended to the (possibly
    empty) statement in the first line.  Cell mode allows you to easily
    profile multiline blocks without having to put them in a separate
    function.
    
    The given statement (which doesn't require quote marks) is run via the
    python profiler in a manner similar to the profile.run() function.
    Namespaces are internally managed to work correctly; profile.run
    cannot be used in IPython because it makes certain assumptions about
    namespaces which do not hold under IPython.
    
    Options:
    
    -l <limit>
      you can place restrictions on what or how much of the
      profile gets printed. The limit value can be:
    
         * A string: only information for function names containing this string
           is printed.
    
         * An integer: only these many lines are printed.
    
         * A float (between 0 and 1): this fraction of the report is printed
           (for example, use a limit of 0.4 to see the topmost 40% only).
    
      You can combine several limits with repeated use of the option. For
      example, ``-l __init__ -l 5`` will print only the topmost 5 lines of
      information about class constructors.
    
    -r
      return the pstats.Stats object generated by the profiling. This
      object has all the information about the profile in it, and you can
      later use it for further analysis or in other functions.
    
    -s <key>
      sort profile by given key. You can provide more than one key
      by using the option several times: '-s key1 -s key2 -s key3...'. The
      default sorting key is 'time'.
    
      The following is copied verbatim from the profile documentation
      referenced below:
    
      When more than one key is provided, additional keys are used as
      secondary criteria when the there is equality in all keys selected
      before them.
    
      Abbreviations can be used for any key names, as long as the
      abbreviation is unambiguous.  The following are the keys currently
      defined:
    
      ============  =====================
      Valid Arg     Meaning
      ============  =====================
      "calls"       call count
      "cumulative"  cumulative time
      "file"        file name
      "module"      file name
      "pcalls"      primitive call count
      "line"        line number
      "name"        function name
      "nfl"         name/file/line
      "stdname"     standard name
      "time"        internal time
      ============  =====================
    
      Note that all sorts on statistics are in descending order (placing
      most time consuming items first), where as name, file, and line number
      searches are in ascending order (i.e., alphabetical). The subtle
      distinction between "nfl" and "stdname" is that the standard name is a
      sort of the name as printed, which means that the embedded line
      numbers get compared in an odd way.  For example, lines 3, 20, and 40
      would (if the file names were the same) appear in the string order
      "20" "3" and "40".  In contrast, "nfl" does a numeric compare of the
      line numbers.  In fact, sort_stats("nfl") is the same as
      sort_stats("name", "file", "line").
    
    -T <filename>
      save profile results as shown on screen to a text
      file. The profile is still shown on screen.
    
    -D <filename>
      save (via dump_stats) profile statistics to given
      filename. This data is in a format understood by the pstats module, and
      is generated by a call to the dump_stats() method of profile
      objects. The profile is still shown on screen.
    
    -q
      suppress output to the pager.  Best used with -T and/or -D above.
    
    If you want to run complete programs under the profiler's control, use
    ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts
    contains profiler specific options as described here.
    
    You can read the complete documentation for the profile module with::
    
      In [1]: import profile; profile.help()
    
    .. versionchanged:: 7.3
        User variables are no longer expanded,
        the magic line is always left unmodified.
%%pypy:
    %%pypy script magic
    
    Run cells with pypy in a subprocess.
    
    This is a shortcut for `%%script pypy`
%%python:
    %%python script magic
    
    Run cells with python in a subprocess.
    
    This is a shortcut for `%%script python`
%%python2:
    %%python2 script magic
    
    Run cells with python2 in a subprocess.
    
    This is a shortcut for `%%script python2`
%%python3:
    %%python3 script magic
    
    Run cells with python3 in a subprocess.
    
    This is a shortcut for `%%script python3`
%%ruby:
    %%ruby script magic
    
    Run cells with ruby in a subprocess.
    
    This is a shortcut for `%%script ruby`
%%script:
    ::
    
      %shebang [--no-raise-error] [--proc PROC] [--bg] [--err ERR] [--out OUT]
    
    Run a cell via a shell command
    
    The `%%script` line is like the #! line of script,
    specifying a program (bash, perl, ruby, etc.) with which to run.
    
    The rest of the cell is run by that program.
    
    Examples
    --------
    ::
    
        In [1]: %%script bash
           ...: for i in 1 2 3; do
           ...:   echo $i
           ...: done
        1
        2
        3
    
    options:
      --no-raise-error  Whether you should raise an error message in addition to a
                        stream on stderr if you get a nonzero exit code.
      --proc PROC       The variable in which to store Popen instance. This is
                        used only when --bg option is given.
      --bg              Whether to run the script in the background. If given, the
                        only way to see the output of the command is with
                        --out/err.
      --err ERR         The variable in which to store stderr from the script. If
                        the script is backgrounded, this will be the stderr
                        *pipe*, instead of the stderr text itself and will not be
                        autoclosed.
      --out OUT         The variable in which to store stdout from the script. If
                        the script is backgrounded, this will be the stdout
                        *pipe*, instead of the stderr text itself and will not be
                        auto closed.
%%sh:
    %%sh script magic
    
    Run cells with sh in a subprocess.
    
    This is a shortcut for `%%script sh`
%%sql:
    No documentation
%%svg:
    Render the cell as an SVG literal
%%sx:
    Shell execute - run shell command and capture output (!! is short-hand).
    
    %sx command
    
    IPython will run the given command using commands.getoutput(), and
    return the result formatted as a list (split on '\n').  Since the
    output is _returned_, it will be stored in ipython's regular output
    cache Out[N] and in the '_N' automatic variables.
    
    Notes:
    
    1) If an input line begins with '!!', then %sx is automatically
    invoked.  That is, while::
    
      !ls
    
    causes ipython to simply issue system('ls'), typing::
    
      !!ls
    
    is a shorthand equivalent to::
    
      %sx ls
    
    2) %sx differs from %sc in that %sx automatically splits into a list,
    like '%sc -l'.  The reason for this is to make it as easy as possible
    to process line-oriented shell output via further python commands.
    %sc is meant to provide much finer control, but requires more
    typing.
    
    3) Just like %sc -l, this is a list with special attributes:
    ::
    
      .l (or .list) : value as list.
      .n (or .nlstr): value as newline-separated string.
      .s (or .spstr): value as whitespace-separated string.
    
    This is very useful when trying to use such lists as arguments to
    system commands.
%%system:
    Shell execute - run shell command and capture output (!! is short-hand).
    
    %sx command
    
    IPython will run the given command using commands.getoutput(), and
    return the result formatted as a list (split on '\n').  Since the
    output is _returned_, it will be stored in ipython's regular output
    cache Out[N] and in the '_N' automatic variables.
    
    Notes:
    
    1) If an input line begins with '!!', then %sx is automatically
    invoked.  That is, while::
    
      !ls
    
    causes ipython to simply issue system('ls'), typing::
    
      !!ls
    
    is a shorthand equivalent to::
    
      %sx ls
    
    2) %sx differs from %sc in that %sx automatically splits into a list,
    like '%sc -l'.  The reason for this is to make it as easy as possible
    to process line-oriented shell output via further python commands.
    %sc is meant to provide much finer control, but requires more
    typing.
    
    3) Just like %sc -l, this is a list with special attributes:
    ::
    
      .l (or .list) : value as list.
      .n (or .nlstr): value as newline-separated string.
      .s (or .spstr): value as whitespace-separated string.
    
    This is very useful when trying to use such lists as arguments to
    system commands.
%%time:
    Time execution of a Python statement or expression.
    
    The CPU and wall clock times are printed, and the value of the
    expression (if any) is returned.  Note that under Win32, system time
    is always reported as 0, since it can not be measured.
    
    This function can be used both as a line and cell magic:
    
    - In line mode you can time a single-line statement (though multiple
      ones can be chained with using semicolons).
    
    - In cell mode, you can time the cell body (a directly
      following statement raises an error).
    
    This function provides very basic timing functionality.  Use the timeit
    magic for more control over the measurement.
    
    .. versionchanged:: 7.3
        User variables are no longer expanded,
        the magic line is always left unmodified.
    
    Examples
    --------
    ::
    
      In [1]: %time 2**128
      CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
      Wall time: 0.00
      Out[1]: 340282366920938463463374607431768211456L
    
      In [2]: n = 1000000
    
      In [3]: %time sum(range(n))
      CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
      Wall time: 1.37
      Out[3]: 499999500000L
    
      In [4]: %time print('hello world')
      hello world
      CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
      Wall time: 0.00
    
    .. note::
        The time needed by Python to compile the given expression will be
        reported if it is more than 0.1s.
    
        In the example below, the actual exponentiation is done by Python
        at compilation time, so while the expression can take a noticeable
        amount of time to compute, that time is purely due to the
        compilation::
    
            In [5]: %time 3**9999;
            CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
            Wall time: 0.00 s
    
            In [6]: %time 3**999999;
            CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
            Wall time: 0.00 s
            Compiler : 0.78 s
%%timeit:
    Time execution of a Python statement or expression
    
    Usage, in line mode:
      %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
    or in cell mode:
      %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
      code
      code...
    
    Time execution of a Python statement or expression using the timeit
    module.  This function can be used both as a line and cell magic:
    
    - In line mode you can time a single-line statement (though multiple
      ones can be chained with using semicolons).
    
    - In cell mode, the statement in the first line is used as setup code
      (executed but not timed) and the body of the cell is timed.  The cell
      body has access to any variables created in the setup code.
    
    Options:
    -n<N>: execute the given statement <N> times in a loop. If <N> is not
    provided, <N> is determined so as to get sufficient accuracy.
    
    -r<R>: number of repeats <R>, each consisting of <N> loops, and take the
    average result.
    Default: 7
    
    -t: use time.time to measure the time, which is the default on Unix.
    This function measures wall time.
    
    -c: use time.clock to measure the time, which is the default on
    Windows and measures wall time. On Unix, resource.getrusage is used
    instead and returns the CPU user time.
    
    -p<P>: use a precision of <P> digits to display the timing result.
    Default: 3
    
    -q: Quiet, do not print result.
    
    -o: return a TimeitResult that can be stored in a variable to inspect
        the result in more details.
    
    .. versionchanged:: 7.3
        User variables are no longer expanded,
        the magic line is always left unmodified.
    
    Examples
    --------
    ::
    
      In [1]: %timeit pass
      8.26 ns ± 0.12 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
    
      In [2]: u = None
    
      In [3]: %timeit u is None
      29.9 ns ± 0.643 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
      In [4]: %timeit -r 4 u == None
    
      In [5]: import time
    
      In [6]: %timeit -n1 time.sleep(2)
    
    The times reported by %timeit will be slightly higher than those
    reported by the timeit.py script when variables are accessed. This is
    due to the fact that %timeit executes the statement in the namespace
    of the shell, compared with timeit.py, which uses a single setup
    statement to import function or create variables. Generally, the bias
    does not matter as long as results from timeit.py are not mixed with
    those from %timeit.
%%writefile:
    ::
    
      %writefile [-a] filename
    
    Write the contents of the cell to a file.
    
    The file will be overwritten unless the -a (--append) flag is specified.
    
    positional arguments:
      filename      file to write
    
    options:
      -a, --append  Append contents of the cell to an existing file. The file will
                    be created if it does not exist.

Summary of magic functions (from %lsmagic):
Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %code_wrap  %colors  %conda  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %mamba  %man  %matplotlib  %micromamba  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %restart_python  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%code_wrap  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%sql  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

Leave a Comment