PlotDevice can be extended with a wealth of functionality by importing the right external libraries. You can install Python libraries for making HTTP requests, running databases, processing images, and a million other problem-domains you’d never even considered.

These specialized tools aren’t included by default for the simple reason that it would make PlotDevice too unwieldy and complex. Instead, PlotDevice’s commands apply to the general case and libraries handle the specifics.

Python modules

Since the scripts you write in PlotDevice use Python, you can access and import any of the modules from the Standard Library (a.k.a. the ‘stdlib’) in your projects. The stdlib is almost preposterously extensive, reflecting Python’s motto of ‘batteries included’. A terrific set of tutorials for some of the most useful modules can be found at Python Module of the Week.

For instance, you can import the json module to serialize your data to a string:

import json
data = dict(fingers=10, handedness="left", feathers=None)
print json.dumps(data)
>>> {"handedness": "left", "feathers": null, "fingers": 10}

If you only need certain pieces of functionality from a module, you can import specific functions using the ‘from … import …’ syntax. For example, both of the following snippets make use of the os module’s basename function. Use whichever approach makes your code easier to understand:

import os
print os.path.basename("/etc/passwd")
from os.path import basename
print basename("/etc/passwd")
>>> passwd

You don’t need to install any files or configure anything; PlotDevice knows where the standard libraries are.

Libraries from the PlotDevice site

The PlotDevice site has a lot of interesting libraries originally developed for NodeBox. Libraries allow you to access natural language processing, or Core Image filters, or even Color math. A list of all libraries is here.

To use one of these libraries, you first need to download it to your computer. In most cases, the zip file you download will expand to a folder with the same name as the library. Then, you need to place the whole folder (don’t mess with its contents) in a known-location where your PlotDevice script can find it:

Once the library is installed, you can import it like any other Python module. Here’s an example using the WordNet library. Notice how the wordnet folder is in the same directory as the dictscript.pv file (which we just created):

Now our script can access the library by using the import statement along with the library’s folder name (note the lack of quotes around the name). When you import a module, a variable with the same name is created and you access its functionality using dot-syntax of the form: module.funcname(). Let’s try using the WordNet lib’s ‘glossary’ method: