|
| 1 | +.. _python_api_quickstart: |
| 2 | + |
| 3 | +===================== |
| 4 | +Python API Quickstart |
| 5 | +===================== |
| 6 | + |
| 7 | +tmuxp allows for developers and system administrators to control live tmux |
| 8 | +sessions using python code. |
| 9 | + |
| 10 | +In this example, we will launch a tmux session and control the windows |
| 11 | +from inside a live tmux session. |
| 12 | + |
| 13 | + |
| 14 | +Setting up tab-completion |
| 15 | +------------------------- |
| 16 | + |
| 17 | +To begin, it's preferable to install a python CLI with tab-completion. |
| 18 | + |
| 19 | +You can install a custom python shell like `bpython`_ or `iPython`_, which |
| 20 | +has some awesome CLI features, or setup vanilla :py:mod:`readline` support. |
| 21 | + |
| 22 | +``readline`` tab-completion |
| 23 | +""""""""""""""""""""""""""" |
| 24 | + |
| 25 | +.. seealso:: |
| 26 | + Source: `How do I add tab-completion to the python shell`_ on |
| 27 | + `StackOverflow`_. |
| 28 | + |
| 29 | +Create ``~.pythonrc`` in ``$HOME`` folder: |
| 30 | + |
| 31 | +.. code-block:: python |
| 32 | +
|
| 33 | + # ~/.pythonrc |
| 34 | + # enable syntax completion |
| 35 | + try: |
| 36 | + import readline |
| 37 | + except ImportError: |
| 38 | + print "Module readline not available." |
| 39 | + else: |
| 40 | + import rlcompleter |
| 41 | + readline.parse_and_bind("tab: complete") |
| 42 | +
|
| 43 | +Then to your ``.bashrc`` or ``.zshrc`` file, add: |
| 44 | + |
| 45 | +.. code-block:: bash |
| 46 | +
|
| 47 | + export PYTHONSTARTUP=~/.pythonrc |
| 48 | +
|
| 49 | +.. _How do I add tab-completion to the python shell: http://stackoverflow.com/a/246779 |
| 50 | +.. _StackOverflow: http://www.stackoverflow.com |
| 51 | + |
| 52 | +bpython or ipython cli |
| 53 | +"""""""""""""""""""""" |
| 54 | + |
| 55 | +`bpython`_ can be installed with ``$ [sudo] pip install bpython`` and |
| 56 | +`ipython`_ can be installed with ``$ [sudo] pip install ipython``. |
| 57 | + |
| 58 | +bpython allows using ``<F2>`` to see the source of CLI methods in colors. |
| 59 | + |
| 60 | +.. todo:: |
| 61 | + If you know any extra benefits of ipython or bpython for CLI and could |
| 62 | + list them here please edit this page. |
| 63 | + |
| 64 | + |
| 65 | +.. _bpython: https://bitbucket.org/bobf/bpython |
| 66 | +.. _ipython: http://ipython.org |
| 67 | + |
| 68 | +Control tmux via python |
| 69 | +----------------------- |
| 70 | + |
| 71 | +.. seealso:: :ref:`api` |
| 72 | + |
| 73 | +.. todo:: Do a version of this with `sliderepl`_ |
| 74 | + |
| 75 | +To begin, ensure the ``tmux`` program is installed. |
| 76 | + |
| 77 | +Next, ensure ``tmuxp`` (note the p!) is installed: |
| 78 | + |
| 79 | +.. code-block:: bash |
| 80 | +
|
| 81 | + $ [sudo] pip install tmuxp |
| 82 | +
|
| 83 | +Now, let's open a tmux session. |
| 84 | + |
| 85 | +.. code-block:: bash |
| 86 | +
|
| 87 | + $ tmux |
| 88 | +
|
| 89 | +We are inside of a tmux session, let's launch our python interpretter |
| 90 | +(``$ python``, ``$ bpython`` or ``$ ipython``) and begin issuing commands |
| 91 | +to tmuxp CLI style. For this I'll use ``python``. |
| 92 | + |
| 93 | +.. code-block:: bash |
| 94 | +
|
| 95 | + $ python |
| 96 | +
|
| 97 | +.. module:: tmuxp |
| 98 | + |
| 99 | +First, we can grab a :class:`Server`. |
| 100 | + |
| 101 | + |
| 102 | +.. code-block:: python |
| 103 | +
|
| 104 | + server = tmuxp.Server() |
| 105 | +
|
| 106 | +.. note:: |
| 107 | + |
| 108 | + You can specify a ``socket_name``, ``socket_path`` and ``config_file`` |
| 109 | + in your server object. ``tmuxp.Server(socket_name='mysocket')`` is |
| 110 | + equivalent to ``$ tmux -L mysocket``. |
| 111 | + |
| 112 | +``server`` is now a living object bound to the tmux server's Sessions, |
| 113 | +Windows and Panes. |
| 114 | + |
| 115 | + |
| 116 | +.. _sliderepl: http://discorporate.us/projects/sliderepl/ |
0 commit comments