|
| 1 | +extensionName: py |
| 2 | +icon: logos:python |
| 3 | +filesToIncludeInManual: |
| 4 | +- USING.md |
| 5 | +- primitives |
| 6 | +markdownTemplate: |2 |
| 7 | +
|
| 8 | + # NetLogo Python extension |
| 9 | +
|
| 10 | + This NetLogo extension allows you to run Python code from NetLogo. It works with both Python 2 and 3, and should work with almost all Python libraries. |
| 11 | +
|
| 12 | + {{> BUILDING.md }} |
| 13 | +
|
| 14 | + {{> USING.md }} |
| 15 | +
|
| 16 | +
|
| 17 | + ## Primitives |
| 18 | +
|
| 19 | + {{#primitives}} |
| 20 | + {{> primTemplate}} |
| 21 | + {{/primitives}} |
| 22 | +primTemplate: |2 |
| 23 | +
|
| 24 | + ### `{{name}}` |
| 25 | +
|
| 26 | + ```NetLogo |
| 27 | + {{#examples}} |
| 28 | + {{primitive.fullName}}{{#args}} {{name}}{{/args}} |
| 29 | + {{/examples}} |
| 30 | + ``` |
| 31 | +
|
| 32 | + {{{description}}} |
| 33 | +primitives: |
| 34 | +- arguments: |
| 35 | + - name: python-executable |
| 36 | + type: string |
| 37 | + description: |2 |
| 38 | +
|
| 39 | + Create the Python session that this extension will use to execute code. The session will be started with the given Python executable. This command *must* be run before running any other Python extension primitive. Running this command again will shutdown the current Python environment and start a new one. |
| 40 | +
|
| 41 | + The executable may be specified as a relative path, absolute path, or just the executable name if it is on your PATH. |
| 42 | + Furthermore, this extension offers a few helper primitives for getting particular versions of Python in system |
| 43 | + independent ways. |
| 44 | +
|
| 45 | + In general, unless working with a virtual environment or a specific system setup, you should do: |
| 46 | +
|
| 47 | + ```NetLogo |
| 48 | + py:setup py:python ; if your code works with either Python 2 or 3 |
| 49 | + py:setup py:python3 ; for Python 3 |
| 50 | + py:setup py:python2 ; for Python 2 |
| 51 | + ``` |
| 52 | +
|
| 53 | + `py:setup` may be invoked by directly referring to different Pythons as well. For instance: |
| 54 | +
|
| 55 | + ```NetLogo |
| 56 | + py:setup "python3" ; if `python3` is on your PATH |
| 57 | + py:setup "python" ; if `python` is on your PATH |
| 58 | + ``` |
| 59 | +
|
| 60 | + If you use virtualenv or Conda, simply specify the path of the Python executable in the environment you wish to use: |
| 61 | +
|
| 62 | + ```NetLogo |
| 63 | + py:setup "/path/to/myenv/bin/python" |
| 64 | + ``` |
| 65 | +
|
| 66 | + The path may be relative or absolute. So, if you have a virtual environment in the same folder as your model, you can do: |
| 67 | +
|
| 68 | + ```NetLogo |
| 69 | + py:setup "myenv/bin/python" |
| 70 | + ``` |
| 71 | + name: setup |
| 72 | + type: command |
| 73 | +- description: |2 |
| 74 | +
|
| 75 | + Reports either the path to the latest version of Python configured in the `python.properties` file or, if that is blank, looks for a Python executable on your system's PATH. |
| 76 | + For Windows, there is an installation option for including Python on your PATH. |
| 77 | + For MacOS and Linux, it will likely already be on your PATH. |
| 78 | + The output of this reporter is meant to be used with `py:setup`, but you may also use it to see which Python installation this extension will use by default. |
| 79 | +
|
| 80 | + For example, on MacOS with Homebrew installed Python 3: |
| 81 | + ```NetLogo |
| 82 | + observer> show py:python |
| 83 | + observer: "/usr/local/bin/python3" |
| 84 | + ``` |
| 85 | + name: python |
| 86 | + returns: string |
| 87 | + type: reporter |
| 88 | +- description: |2 |
| 89 | +
|
| 90 | + Reports either the path to Python 2 configured in the `python.properties` file or, if that is blank, looks for a Python 2 executable on your system's PATH. |
| 91 | + For Windows, there is an installation option for including Python on your PATH. |
| 92 | + For MacOS and Linux, it will likely already be on your PATH. |
| 93 | + The output of this reporter is meant to be used with `py:setup`, but you may also use it to see which Python 2 installation this extension will use by default. |
| 94 | +
|
| 95 | + For example, on MacOS with Homebrew installed Python 2: |
| 96 | + ```NetLogo |
| 97 | + observer> show py:python2 |
| 98 | + observer: "/usr/local/bin/python2" |
| 99 | + ``` |
| 100 | + name: python2 |
| 101 | + returns: string |
| 102 | + type: reporter |
| 103 | +- description: |2 |
| 104 | +
|
| 105 | + Reports either the path to Python 3 configured in the `python.properties` file or, if that is blank, looks for a Python 3 executable on your system's PATH. |
| 106 | + For Windows, there is an installation option for including Python on your PATH. |
| 107 | + For MacOS and Linux, it will likely already be on your PATH. |
| 108 | + The output of this reporter is meant to be used with `py:setup`, but you may also use it to see which Python 3 installation this extension will use by default. |
| 109 | +
|
| 110 | + For example, on MacOS with Homebrew installed Python 3: |
| 111 | + ```NetLogo |
| 112 | + observer> show py:python3 |
| 113 | + observer: "/usr/local/bin/python3" |
| 114 | + ``` |
| 115 | + name: python3 |
| 116 | + returns: string |
| 117 | + type: reporter |
| 118 | +- arguments: |
| 119 | + - name: python-statement |
| 120 | + type: repeatable string |
| 121 | + description: |2 |
| 122 | +
|
| 123 | + Runs the given Python statements in the current Python session. To make multi-line Python code easier to run, this command will take multiple strings, each of which will be interpreted as a separate line of Python code. For instance: |
| 124 | +
|
| 125 | + ```NetLogo |
| 126 | + (py:run |
| 127 | + "import matplotlib" |
| 128 | + "matplotlib.use('TkAgg')" |
| 129 | + "import numpy as np" |
| 130 | + "import matplotlib.pyplot as plt" |
| 131 | + "for i in range(10):" |
| 132 | + " plt.plot([ x ** i for x in np.arange(-1, 1, 0.1) ])" |
| 133 | + "plt.show()" |
| 134 | + ) |
| 135 | + ``` |
| 136 | +
|
| 137 | + `py:run` will wait for the statements to finish running before continuing. Thus, if you have long running Python code, NetLogo will pause while it runs. |
| 138 | + name: run |
| 139 | + type: command |
| 140 | +- arguments: |
| 141 | + - name: python-expression |
| 142 | + type: repeatable string |
| 143 | + description: |2 |
| 144 | +
|
| 145 | + Evaluates the given Python expression and reports the result. |
| 146 | + `py:runresult` attempts to convert from Python data types to NetLogo data types. |
| 147 | + Numbers, strings, and booleans convert as you would expect. |
| 148 | + Any list-like object in Python (that is, anything with a length that you can iterate through) will be converted to a NetLogo list. |
| 149 | + For instance, Python lists and NumPy arrays will convert to NetLogo lists. |
| 150 | + Python dicts (and dict-like objects) will convert to a NetLogo list of key-value pairs (where each pair is represented as a list). |
| 151 | + `None` will be converted to `nobody`. |
| 152 | + Other objects will simply be converted to a string representation. |
| 153 | +
|
| 154 | + Note that due a [current issue](https://github.com/qiemem/PythonExtension/issues/6), dict keys will always be reported as strings. |
| 155 | + If you need to report non-string keys, report the `.items()` of the dict instead of the dict itself. |
| 156 | + name: runresult |
| 157 | + returns: anything |
| 158 | + type: reporter |
| 159 | +- arguments: |
| 160 | + - name: variable-name |
| 161 | + type: string |
| 162 | + - name: value |
| 163 | + type: anything |
| 164 | + description: |2+ |
| 165 | +
|
| 166 | + Sets a variable in the Python session with the given name to the given NetLogo value. NetLogo objects will be converted to Python objects as expected. |
| 167 | +
|
| 168 | + All vanilla NetLogo objects are supported, but objects from other extensions, even other bundled extensions, are not supported. |
| 169 | +
|
| 170 | + ```NetLogo |
| 171 | + py:set "x" [1 2 3] |
| 172 | + show py:runresult "x" ;; Shows [1 2 3] |
| 173 | + ``` |
| 174 | +
|
| 175 | + Agents are converted into dictionaries with elements for each agent variable. Agentsets are converted into lists of agent dictionaries. |
| 176 | +
|
| 177 | + ```NetLogo |
| 178 | + breed [goats goat] |
| 179 | + goats-own [energy ] |
| 180 | + create-goats 1 [ set heading 0 set color 75 ] |
| 181 | + ask goat 0 [ set energy 42 ] |
| 182 | + py:set "goat" goat 0 |
| 183 | + py:runresult "str(goat)" ;; Should output: "{'WHO': 0, 'COLOR': 75, 'HEADING': 0, 'XCOR': 0, 'YCOR': 0, 'SHAPE': 'default', 'LABEL': '', 'LABEL-COLOR': 9.9, 'BREED': 'GOATS', 'HIDDEN?': False, 'SIZE': 1, 'PEN-SIZE': 1, 'PEN-MODE': 'up', 'ENERGY': 42}" |
| 184 | + ``` |
| 185 | +
|
| 186 | + Agents with variables containing references to agentsets will have those variables converted into the string representation of that agentset. |
| 187 | +
|
| 188 | + name: set |
| 189 | + type: command |
0 commit comments