{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Customizing the completer in IPython\n", "\n", "> It took a little bit to understand the event object.\n", "\n", "* For the demostration, we will modify our existing `module_completer` to predict imports from indented lines. `importnb` will need this." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ " from IPython.core.completerlib import module_completer \n", " Ø = __name__ == '__main__'\n", " if Ø: ip = get_ipython()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* The [`h5py.ipy_completer`](https://github.com/h5py/h5py/blob/master/h5py/ipy_completer.py) helped me understand the events a little better." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ " def event(self, event):\n", " event.line = event.line.lstrip()\n", " return module_completer(self, event)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* As expected, `requests` should show up for unindented lines." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ " if Ø: 'requests' in ip.complete('re', 'import re')[1], \"\"\"requests is not installed\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* but, an indent causes problems." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ " if Ø: 'requests' not in ip.complete('re', '\\timport re')[1], \"\"\"the new complete_command hook was assigned already.\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Set the complete command hook." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ " def load_ipython_extension(ip):\n", " ip.set_hook('complete_command', event, str_key=\"import\")\n", " \n", " if Ø: load_ipython_extension(ip)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Now an indented import query will find requests." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ " if Ø: 'requests' in ip.complete('re', '\\timport re')[1], \"\"\"requests still doesn't exist.\"\"\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ " from . import disqus" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "p6", "language": "python", "name": "other-env" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }