##// END OF EJS Templates
docs: refer to 'embed_kernel' in 'start_kernel's docstring...
docs: refer to 'embed_kernel' in 'start_kernel's docstring When 'start_kernel' was added in a10986ac3 (add IPython.start_kernel, 2013-07-04), its docstring was probably copied from that of 'start_ipython', such that instead of referencing its counterpart 'embed_kernel', it references 'start_ipython's counterpart 'embed'. Correctly refer to 'embed_kernel'.

File last commit:

r28107:2fce830c
r28184:92c98813
Show More
details.rst
323 lines | 12.0 KiB | text/x-rst | RstLexer
Thomas Kluyver
Remove outdated config information
r13490 =======================
Specific config details
=======================
Thomas Kluyver
Improve release notes and config details
r22611 .. _custom_prompts:
Custom Prompts
==============
.. versionchanged:: 5.0
From IPython 5, prompts are produced as a list of Pygments tokens, which are
tuples of (token_type, text). You can customise prompts by writing a method
which generates a list of tokens.
There are four kinds of prompt:
* The **in** prompt is shown before the first line of input
(default like ``In [1]:``).
* The **continuation** prompt is shown before further lines of input
(default like ``...:``).
* The **rewrite** prompt is shown to highlight how special syntax has been
interpreted (default like ``----->``).
* The **out** prompt is shown before the result from evaluating the input
(default like ``Out[1]:``).
Custom prompts are supplied together as a class. If you want to customise only
some of the prompts, inherit from :class:`IPython.terminal.prompts.Prompts`,
which defines the defaults. The required interface is like this:
.. class:: MyPrompts(shell)
Prompt style definition. *shell* is a reference to the
:class:`~.TerminalInteractiveShell` instance.
.. method:: in_prompt_tokens(cli=None)
continuation_prompt_tokens(self, cli=None, width=None)
rewrite_prompt_tokens()
out_prompt_tokens()
Return the respective prompts as lists of ``(token_type, text)`` tuples.
For continuation prompts, *width* is an integer representing the width of
the prompt area in terminal columns.
*cli*, where used, is the prompt_toolkit ``CommandLineInterface`` instance.
This is mainly for compatibility with the API prompt_toolkit expects.
Thomas Kluyver
Unify docs on custom prompts...
r22690 Here is an example Prompt class that will show the current working directory
in the input prompt:
.. code-block:: python
from IPython.terminal.prompts import Prompts, Token
import os
class MyPrompt(Prompts):
def in_prompt_tokens(self, cli=None):
return [(Token, os.getcwd()),
(Token.Prompt, ' >>>')]
To set the new prompt, assign it to the ``prompts`` attribute of the IPython
shell:
.. code-block:: python
In [2]: ip = get_ipython()
...: ip.prompts = MyPrompt(ip)
/home/bob >>> # it works
anakimluke
fix minor typo in details.rst
r27827 See ``IPython/example/utils/cwd_prompt.py`` for an example of how to write
Thomas Kluyver
Unify docs on custom prompts...
r22690 extensions to customise prompts.
Thomas Kluyver
Improve release notes and config details
r22611 Inside IPython or in a startup script, you can use a custom prompts class
by setting ``get_ipython().prompts`` to an *instance* of the class.
In configuration, ``TerminalInteractiveShell.prompts_class`` may be set to
either the class object, or a string of its full importable name.
Thomas Kluyver
Document colours and escapes in prompts...
r22778 To include invisible terminal control sequences in a prompt, use
``Token.ZeroWidthEscape`` as the token type. Tokens with this type are ignored
when calculating the width.
Colours in the prompt are determined by the token types and the highlighting
style; see below for more details. The tokens used in the default prompts are
``Prompt``, ``PromptNum``, ``OutPrompt`` and ``OutPromptNum``.
Thomas Kluyver
Remove outdated config information
r13490 .. _termcolour:
Terminal Colors
===============
Thomas Kluyver
Improve release notes and config details
r22611 .. versionchanged:: 5.0
There are two main configuration options controlling colours.
``InteractiveShell.colors`` sets the colour of tracebacks and object info (the
output from e.g. ``zip?``). It may also affect other things if the option below
is set to ``'legacy'``. It has four case-insensitive values:
``'nocolor', 'neutral', 'linux', 'lightbg'``. The default is *neutral*, which
should be legible on either dark or light terminal backgrounds. *linux* is
optimised for dark backgrounds and *lightbg* for light ones.
memeplex
Allow to pass a pygments class to highlighting_style....
r22766 ``TerminalInteractiveShell.highlighting_style`` determines prompt colours and
syntax highlighting. It takes the name (as a string) or class (as a subclass of
``pygments.style.Style``) of a Pygments style, or the special value ``'legacy'``
to pick a style in accordance with ``InteractiveShell.colors``.
Matthias Bussonnier
Restore the ability to use %colors to switch terminal theme....
r22578
Thomas Kluyver
Improve release notes and config details
r22611 You can see the Pygments styles available on your system by running::
import pygments
list(pygments.styles.get_all_styles())
dongweiming
fix highlighting_style typo
r22694 Additionally, ``TerminalInteractiveShell.highlighting_style_overrides`` can override
Thomas Kluyver
Improve release notes and config details
r22611 specific styles in the highlighting. It should be a dictionary mapping Pygments
token types to strings defining the style. See `Pygments' documentation
<http://pygments.org/docs/styles/#creating-own-styles>`__ for the language used
to define styles.
Thomas Kluyver
Remove outdated config information
r13490
Colors in the pager
-------------------
On some systems, the default pager has problems with ANSI colour codes.
To configure your default pager to allow these:
1. Set the environment PAGER variable to ``less``.
2. Set the environment LESS variable to ``-r`` (plus any other options
you always want to pass to less by default). This tells less to
properly interpret control sequences, which is how color
information is given to your terminal.
Thomas Kluyver
Move editor config info into details.rst
r13495
.. _editors:
Editor configuration
====================
IPython can integrate with text editors in a number of different ways:
* Editors (such as `(X)Emacs`_, vim_ and TextMate_) can
send code to IPython for execution.
* IPython's ``%edit`` magic command can open an editor of choice to edit
a code block.
The %edit command (and its alias %ed) will invoke the editor set in your
environment as :envvar:`EDITOR`. If this variable is not set, it will default
to vi under Linux/Unix and to notepad under Windows. You may want to set this
variable properly and to a lightweight editor which doesn't take too long to
start (that is, something other than a new instance of Emacs). This way you
can edit multi-line code quickly and with the power of a real editor right
inside IPython.
You can also control the editor by setting :attr:`TerminalInteractiveShell.editor`
in :file:`ipython_config.py`.
Vim
---
Paul Ivanov's `vim-ipython <https://github.com/ivanov/vim-ipython>`_ provides
powerful IPython integration for vim.
.. _emacs:
(X)Emacs
--------
If you are a dedicated Emacs user, and want to use Emacs when IPython's
``%edit`` magic command is called you should set up the Emacs server so that
new requests are handled by the original process. This means that almost no
time is spent in handling the request (assuming an Emacs process is already
running). For this to work, you need to set your EDITOR environment variable
to 'emacsclient'. The code below, supplied by Francois Pinard, can then be
Thomas Kluyver
Fix some whitespace, and use lisp highlightling for emacs code
r13533 used in your :file:`.emacs` file to enable the server:
.. code-block:: common-lisp
Thomas Kluyver
Move editor config info into details.rst
r13495
(defvar server-buffer-clients)
(when (and (fboundp 'server-start) (string-equal (getenv "TERM") 'xterm))
(server-start)
(defun fp-kill-server-with-buffer-routine ()
(and server-buffer-clients (server-done)))
(add-hook 'kill-buffer-hook 'fp-kill-server-with-buffer-routine))
Thanks to the work of Alexander Schmolck and Prabhu Ramachandran,
currently (X)Emacs and IPython get along very well in other ways.
Min RK
remove description of long-dead ipython.el...
r19927 With (X)EMacs >= 24, You can enable IPython in python-mode with:
Thomas Kluyver
Fix some whitespace, and use lisp highlightling for emacs code
r13533
.. code-block:: common-lisp
Thomas Kluyver
Move editor config info into details.rst
r13495
Min RK
remove description of long-dead ipython.el...
r19927 (require 'python)
(setq python-shell-interpreter "ipython")
Thomas Kluyver
Move editor config info into details.rst
r13495
.. _`(X)Emacs`: http://www.gnu.org/software/emacs/
.. _TextMate: http://macromates.com/
.. _vim: http://www.vim.org/
Thomas Kluyver
Docs on adding keyboard shortcuts to terminal IPython...
r22688
Matthias Bussonnier
Fix a couple of warnings/errors in doc builds.
r23477 .. _custom_keyboard_shortcuts:
Thomas Kluyver
Docs on adding keyboard shortcuts to terminal IPython...
r22688
Keyboard Shortcuts
==================
krassowski
Now targeting 8.11
r28107 .. versionadded:: 8.11
krassowski
Improve documentation for shortcuts
r28076
You can modify, disable or modify keyboard shortcuts for IPython Terminal using
:std:configtrait:`TerminalInteractiveShell.shortcuts` traitlet.
The list of shortcuts is available in the Configuring IPython :ref:`terminal-shortcuts-list` section.
Advanced configuration
----------------------
Thomas Kluyver
Docs on adding keyboard shortcuts to terminal IPython...
r22688 .. versionchanged:: 5.0
krassowski
Improve documentation for shortcuts
r28076 Creating custom commands requires adding custom code to a
:ref:`startup file <startup_files>`::
Thomas Kluyver
Docs on adding keyboard shortcuts to terminal IPython...
r22688
from IPython import get_ipython
from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.keys import Keys
from prompt_toolkit.filters import HasFocus, HasSelection, ViInsertMode, EmacsInsertMode
ip = get_ipython()
insert_mode = ViInsertMode() | EmacsInsertMode()
def insert_unexpected(event):
buf = event.current_buffer
buf.insert_text('The Spanish Inquisition')
# Register the shortcut if IPython is using prompt_toolkit
Matthias Bussonnier
Fix custom keyboard shortcut examples....
r24742 if getattr(ip, 'pt_app', None):
registry = ip.pt_app.key_bindings
Thomas Kluyver
Docs on adding keyboard shortcuts to terminal IPython...
r22688 registry.add_binding(Keys.ControlN,
filter=(HasFocus(DEFAULT_BUFFER)
& ~HasSelection()
& insert_mode))(insert_unexpected)
Matthias Bussonnier
Fix custom keyboard shortcut examples....
r24742
Here is a second example that bind the key sequence ``j``, ``k`` to switch to
VI input mode to ``Normal`` when in insert mode::
from IPython import get_ipython
from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.filters import HasFocus, ViInsertMode
from prompt_toolkit.key_binding.vi_state import InputMode
ip = get_ipython()
def switch_to_navigation_mode(event):
vi_state = event.cli.vi_state
vi_state.input_mode = InputMode.NAVIGATION
if getattr(ip, 'pt_app', None):
registry = ip.pt_app.key_bindings
registry.add_binding(u'j',u'k',
filter=(HasFocus(DEFAULT_BUFFER)
& ViInsertMode()))(switch_to_navigation_mode)
Thomas Kluyver
Docs on adding keyboard shortcuts to terminal IPython...
r22688 For more information on filters and what you can do with the ``event`` object,
`see the prompt_toolkit docs
Marcio Mazza
Fix broken link in docs...
r25739 <https://python-prompt-toolkit.readthedocs.io/en/latest/pages/asking_for_input.html#adding-custom-key-bindings>`__.
Matthias Bussonnier
Add narrative docs for customizing enter behavior....
r23731
Enter to execute
----------------
Matthias Bussonnier
Take Brian comments into account
r23732 In the Terminal IPython shell – which by default uses the ``prompt_toolkit``
interface, the semantic meaning of pressing the :kbd:`Enter` key can be
ambiguous. In some case :kbd:`Enter` should execute code, and in others it
should add a new line. IPython uses heuristics to decide whether to execute or
insert a new line at cursor position. For example, if we detect that the current
code is not valid Python, then the user is likely editing code and the right
behavior is to likely to insert a new line. If the current code is a simple
statement like `ord('*')`, then the right behavior is likely to execute. Though
the exact desired semantics often varies from users to users.
luz.paz
Misc. typos fixes...
r24132 As the exact behavior of :kbd:`Enter` is ambiguous, it has been special cased
Matthias Bussonnier
Take Brian comments into account
r23732 to allow users to completely configure the behavior they like. Hence you can
have enter always execute code. If you prefer fancier behavior, you need to get
your hands dirty and read the ``prompt_toolkit`` and IPython documentation
though. See :ghpull:`10500`, set the
``c.TerminalInteractiveShell.handle_return`` option and get inspiration from the
Carl Smith
Improved example code in handle_return docs.
r23775 following example that only auto-executes the input if it begins with a bang or
a modulo character (``!`` or ``%``). To use the following code, add it to your
IPython configuration::
def custom_return(shell):
"""This function is required by the API. It takes a reference to
the shell, which is the same thing `get_ipython()` evaluates to.
This function must return a function that handles each keypress
event. That function, named `handle` here, references `shell`
by closure."""
def handle(event):
"""This function is called each time `Enter` is pressed,
and takes a reference to a Prompt Toolkit event object.
If the current input starts with a bang or modulo, then
the input is executed, otherwise a newline is entered,
followed by any spaces needed to auto-indent."""
# set up a few handy references to nested items...
buffer = event.current_buffer
document = buffer.document
text = document.text
if text.startswith('!') or text.startswith('%'): # execute the input...
buffer.accept_action.validate_and_handle(event.cli, buffer)
else: # insert a newline with auto-indentation...
if document.line_count > 1: text = text[:document.cursor_position]
Thomas Kluyver
Add shell.check_complete() method...
r24180 indent = shell.check_complete(text)[1]
buffer.insert_text('\n' + indent)
Carl Smith
Improved example code in handle_return docs.
r23775
# if you just wanted a plain newline without any indentation, you
# could use `buffer.insert_text('\n')` instead of the lines above
return handle
c.TerminalInteractiveShell.handle_return = custom_return