[Qgis-developer] New Python console idea: Expanding the Python console to handle DSLs for plugins eg CADTools

Charlie Sharpsteen chuck at sharpsteen.net
Thu Jun 16 15:24:21 EDT 2011


On Thu, Jun 16, 2011 at 5:27 AM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> On Thu, Jun 16, 2011 at 12:33 PM, Nathan Woodrow <madmanwoo at gmail.com> wrote:
>
>> I would appreciated any feedback anyone has.  I'm not a python expert so the
>> code could be done better if done for real but at the moment it's just rough
>> to get the idea out there.
>
>  I like the idea of supplying console functionality for plugins.
>
>  I don't like the idea of piggybacking via "PREFIX: foo" syntax onto Python.
>
>  People might think they can mix python variables with variables in
> other DSLs. Which would be tricky.
>
>  What might be nice would be if the Python console could exist in a
> tabbed window, and plugins could have an 'open console' option, which
> would add a tab to the python console. This would provide standard
> console functionality and then feed the lines to a handler registered
> by the plugin.
>
>  Maybe you could even have seventeen python console tabs open at once
> next to four SQL command tabs and one interface to the QGIS API
> written in perl?
>
> Barry

You may want to look at the IPython console---they have a very nice
syntax for adding new domain-specific commands which they call "magic
commands":

    http://ipython.org/ipython-doc/dev/interactive/reference.html#magic-command-system

For example, say you want to run a Python function as a command:

    def say_hello(self, arg):
      print("Hello, world!")

    # Grab a hook to the currently running IPython instance:
    import IPython.ipapi
    ip = IPython.ipapi.get()

    # Register a new magic function:
    ip.expose_magic("hello", say_hello)


Users can they run magic commands directly at the console level using
a "%command" syntax:

In [1]: %hello
Hello, world!


IPython 0.11 is scheduled to be released at the end of this month and
has a slightly different api for registering new magic functions:

    # Grab a hook to the currently running IPython instance
    ip = get_ipython()

    # Register a new magic function
    ip.define_magic("hello", say_hello)


I have always thought IPython would be a good replacement for the
current QGIS console as it has a lot of features such as
tab-completion, inline help and interactive object inspection that
saves users a lot of running back and forth between manual pages and
their workspace. The 0.11 version even has a frontend that is built in
PyQt which would make it easy to integrate into QGIS as a plugin.
IPython 0.11 also provides a parallel and distributed computing
framework that could have very interesting applications for GIS
algorithms. I was even working on such a plugin a couple of months
ago.

Unfortunately I had to shelve the project as IPython decided to
migrate to Version 2 of the SIP API in order to be compatible with
PySide as well as PyQt and to be compatible with the rest of the
Enthought tools for scientific computing in Python. QGIS uses Version
1 of the SIP API which is incompatible with Version 2. QGIS could
migrate to Version 2, but all plugins that use QString or QVariant in
their Python code would need to replace these with plain Python
strings and objects as the QString and QVariant classes are no longer
needed or defined with the Version 2 API.

Migrating QGIS to the latest version of the SIP API might be a good
idea in the long run as I think it may have better support for Python
3.  Some of the issues were discussed when PySide decided which API to
support:

    http://www.pyside.org/docs/pseps/psep-0101.html


-Charlie


More information about the Qgis-developer mailing list