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 Python community

The Python Package Index (PyPI) is the central location where open source libraries are collected and distributed. Try searching for a topic you’re interested in or a problem you’d like to solve and marvel at both the number and quality of solutions that are already written, tested, and waiting for you. When you’re writing python code from the command line, you’ll typically use a combination of virtual environments and the pip installer tool to add libraries to your project.

Installing packages for use with the PlotDevice app is a little tricker since it uses a version of Python embedded in the app bundle itself. To simplify this, the plotdevice command line tool has an --install option that will call pip for you and installs into a folder the application knows about: ~/Library/Application Support/PlotDevice. Be sure you have access to the plotdevice command by opening the application’s Preferences window and clicking the Install button in the Command Line Tool section.

To install a package, first find it on the Python Package Index and look for the text pip install <package-name> to find its official module name. For instance, on the NumPy page we can see that the package name is numpy (all lowercase). Now, from the terminal, you can install it by typing:

plotdevice --install numpy

After the installation is complete, go back to the app and try importing it in your own script using the import statement used in the previous section. You can even ask the module to describe itself via the help command:

import numpy
print(help(numpy))
>>> Help on package numpy:
>>>
>>> NAME
>>>     numpy
>>>
>>> DESCRIPTION
>>>     NumPy
>>>     =====
>>>
>>>     Provides
>>>       1. An array object of arbitrary homogeneous items
>>>       2. Fast mathematical operations over arrays
>>>       3. Linear Algebra, Fourier Transforms, Random Number Generation
>>>
>>>     How to use the documentation
>>>     ----------------------------
>>>     Documentation is available in two forms: docstrings provided
>>>     with the code, and a loose standing reference guide, available from
>>>     `the NumPy homepage `_.
>>>
>>>     We recommend exploring the docstrings using
>>>     `IPython `_, an advanced Python shell with
>>>     TAB-completion and introspection capabilities.  See below for further
>>>     instructions.
>>>
>>> …and much much more…