Script Runner: A Plugin to Run Python Scripts in QGIS

January 29, 2012
By

Following up on my last post, Running Scripts in the Python Console, I created a plugin to simplify running scripts:

The Script Runner plugin allows you to add your scripts to a list so they are readily available. You can then run them to automate QGIS tasks and have full access to the PyQGIS API. In addition, you can view information about the classes, methods, and functions in your module as well as browse the source:

In order for your script to work with ScriptRunner it has to implement a single function as an entry point. Here is some additional information from the Help tab of the plugin:

Requirements

In order for Script Runner to execute your script you must define a run_script function that accepts a single argument. This is the standard entry point used by Script Runner. A reference to the qgis.utils.iface object will be passed to your run_script function. You don’t have to use the iface object in your script but your run_script function must accept it as an argument.

Here is an example of a simple run_script function:

    def run_script(iface):
        ldr = Loader(iface)
        ldr.load_shapefiles('/vmap0_shapefiles')

In this example, the run_script creates an instance (ldr) of a class named Loader that is defined in the same source file. It then calls a method in the Loader class named load_shapefiles to do something useful—in this case, load all the shapefiles in a specified directory.

Alternatively, you could choose not to use classes and just do everything within the run_script function, including having it call functions in the same script or others you might import. The important thing is to be sure you have defined a run_script function. If not, Script Runner won’t load your script.

Working with Scripts

To run a script, you must add it to Script Runner using the Add Script tool on the toolbar. This will add it to a list in the left panel. This list of scripts is persisted between uses of QGIS. You can remove a script using the Remove Script tool. This just removes it from the list; it does nothing to the script file on disk.

Once you have a script loaded, you can click the Script Info tool to populate the Info and Source tabs in the panel on the right. The Info tab contains the docstring from your module and then a list of the classes, methods, and functions found in the script. Having a proper docstring at the head of your script will help you determine the puprose of script.

You can view the source of the script on the Source tab. This allows you to quickly confirm that you are using the right script and it does what you think it will.

Installing the Plugin

To install the plugin:

  1. Open the Python plugin installer: Plugins->Fetch Python Plugins
  2. Check to see if you have the new Official repository in your list of plugins by clicking on Repositories tab. The URL is http://plugins.qgis.org/plugins/plugins.xml.
  3. If you have it, skip to step 5. If the new repository isn’t in the list, add it by clicking the Add button. Give it a name and insert the URL http://plugins.qgis.org/plugins/plugins.xml
  4. Click on the Plugins tab
  5. Enter scriptrunner in the Filter box
  6. Select the ScriptRunner plugin and click Install

ScriptRunner adds an entry to the Plugins menu as well as a tool on the Plugins toolbar: . Click it and you are off and running.

Tags: , ,

8 Responses to Script Runner: A Plugin to Run Python Scripts in QGIS

  1. [...] Go check out Gray’s post about the new plugin at http://spatialgalaxy.net/2012/01/29/script-runner-a-plugin-to-run-python-scripts-in-qgis/ [...]

  2. Bo Victor Thomsen on January 30, 2012 at 12:24 am

    Very smart. As a long-time MapInfo user – now trying QGis – I’ve missed a “MapInfo Workspace” tool in QGis. This tool fits the bill perfectly.

  3. Liam Veitch on February 4, 2012 at 4:44 am

    Hi Gary,

    I created a similar plugin which I have been using for qGIS, but I have not had time to polish and publish it. On my plugin, I have not stored the scripts as you have – although I had the intention of using Qt settings files to store per project and per user favourite scripts in the list.

    I also have the output of the python script redirected to the qGIS python console, and have options to run the scripts from specified directory – project, script location, or any other folder on the filesystem.

    I’m also curious why it was decided that you should have a function entry point, after all were supposed to be running python scripts!

    Would you consider collaborating on this project?

    Best Regards,
    Liam

  4. gene on February 7, 2012 at 11:28 am

    Hi,
    I illustrated the use of Script Runner in French in http://www.portailsig.org/content/qgis-lancer-des-scripts-python-ou-des-commandes-shell-depuis-la-console-python-ou-avec-scrip
    with all the solutions to run a Python script in the Python Console (execfiles(), subprocess and importing a module )

  5. gene on February 7, 2012 at 11:31 am

    I was able to transfer all my scripts using Shapely or Fiona without problem, for example

  6. Carlos on February 10, 2012 at 5:53 am

    Hi,
    I’ve tried to use it but I get an error when I use QgsVectorLayer…What it is supposed to import in order to avoid the error?

  7. Gary Sherman on February 10, 2012 at 6:05 am

    @Carlos:
    You should be importing
    from qgis.core import *

    If you want access to the full API, also import qgis.gui.

  8. Carlos on February 13, 2012 at 1:45 am

    @Gary Sherman

    Perfect, it works!!Thank you very much!

Leave a Reply

Your email address will not be published. Required fields are marked *

*