# Examples

> Runnable Python and TypeScript scripts that write, read, and stream data, run control sequences, and set up hardware tasks.

Each example is a script in the Synnax repository that you can run against your own Core.

## Run an example

[Enterprise](https://docs.synnaxlabs.com/reference/get-started/editions "Needs Synnax Enterprise")

**Python**

Most Python examples connect with the login that `sy login` saves. Log in once, see [Log in once with sy login](https://docs.synnaxlabs.com/reference/client/connect#log-in-once-with-sy-login), then run a script:

```bash
python basic_read_write.py
```

The Arduino examples set their connection values in the script.

The control examples and the simulators import other example files. Run them as modules from the `client/py` folder, with a simulator in a second terminal:

```bash
python -m examples.simulators.press
python -m examples.control.press.control_sequence
```

The README in each folder gives the steps.

**TypeScript**

The TypeScript examples connect to `localhost:9090` with the username `synnax` and the password `seldon`. Change these values in the script to match your Core. From the `client/ts/examples/node` folder, install the client and run a script:

```bash
npm install
node basicReadWrite.js
```

`streamRead.js` streams the samples that `streamWrite.js` writes. Run `streamWrite.js` first, in a second terminal.

## Examples by topic

[Enterprise](https://docs.synnaxlabs.com/reference/get-started/editions "Needs Synnax Enterprise")

**Python**

Write, read, and work with data. See [Write data](https://docs.synnaxlabs.com/reference/client/write-data) and [Read data](https://docs.synnaxlabs.com/reference/client/read-data).

| Example                                                                                                        | Shows how to                                                   |
| -------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
| [basic_read_write.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/basic_read_write.py)   | Create channels, write samples, read them back, and plot them. |
| [read_latest.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/read_latest.py)             | Read the latest samples of a channel.                          |
| [series_and_frames.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/series_and_frames.py) | Work with series and frames.                                   |
| [export_to_csv.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/export_to_csv.py)         | Export samples to a CSV file.                                  |
| [plot.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/plot.py)                           | Plot samples with Matplotlib.                                  |

Stream data. See [Stream data](https://docs.synnaxlabs.com/reference/client/stream-data). The scripts that read live samples need `stream_write.py` to run in a second terminal.

| Example                                                                                                        | Shows how to                                            |
| -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- |
| [stream_write.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/stream_write.py)           | Write live samples with a writer.                       |
| [stream_read.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/stream_read.py)             | Stream live samples.                                    |
| [stream_read_async.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/stream_read_async.py) | Stream live samples with `asyncio`.                     |
| [simulation.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/simulation.py)               | Write many channels and answer commands to many valves. |

Channels and ranges. See [Channels](https://docs.synnaxlabs.com/reference/channels) and [Ranges](https://docs.synnaxlabs.com/reference/ranges).

| Example                                                                                                            | Shows how to                                                        |
| ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------- |
| [create_channels.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/create_channels.py)         | Create many channels at once.                                       |
| [create_range.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/create_range.py)               | Create a range and read its data.                                   |
| [calculated_channels.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/calculated_channels.py) | Calculate values from live channels and write them to new channels. |
| [multi-rate-calculated/](https://github.com/synnaxlabs/synnax/tree/main/client/py/examples/multi-rate-calculated)  | Calculate values from channels that have different sample rates.    |

Control sequences. See [Control](https://docs.synnaxlabs.com/reference/control).

| Example                                                                                                    | Shows how to                                                  |
| ---------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
| [control/press/](https://github.com/synnaxlabs/synnax/tree/main/client/py/examples/control/press)          | Pressurize a system in steps with a valve.                    |
| [control/tpc/](https://github.com/synnaxlabs/synnax/tree/main/client/py/examples/control/tpc)              | Run a tank pressure control sequence, in Python and in Arc.   |
| [control/abort/](https://github.com/synnaxlabs/synnax/tree/main/client/py/examples/control/abort)          | Watch for overpressure, take control, and vent the system.    |
| [control/virtual.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/control/virtual.py) | Send values between scripts through a virtual channel.        |
| [simulators/](https://github.com/synnaxlabs/synnax/tree/main/client/py/examples/simulators)                | Simulate hardware, so you can test sequences with no devices. |

Hardware tasks. See [Hardware](https://docs.synnaxlabs.com/reference/hardware). These examples need the Driver and the device. The Modbus, OPC UA, and HTTP folders include a simulated device.

| Example                                                                                                                      | Shows how to                                                 |
| ---------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------ |
| [ni/](https://github.com/synnaxlabs/synnax/tree/main/client/py/examples/ni)                                                  | Create National Instruments read, write, and counter tasks.  |
| [labjack/](https://github.com/synnaxlabs/synnax/tree/main/client/py/examples/labjack)                                        | Connect a LabJack and create read and write tasks.           |
| [modbus/](https://github.com/synnaxlabs/synnax/tree/main/client/py/examples/modbus)                                          | Connect a Modbus TCP server and create read and write tasks. |
| [opcua/](https://github.com/synnaxlabs/synnax/tree/main/client/py/examples/opcua)                                            | Connect an OPC UA server and create read and write tasks.    |
| [http_sim/](https://github.com/synnaxlabs/synnax/tree/main/client/py/examples/http_sim)                                      | Connect an HTTP server and create read and write tasks.      |
| [arduino/](https://github.com/synnaxlabs/synnax/tree/main/client/py/examples/arduino)                                        | Write your own driver for an Arduino.                        |
| [hardware/task_management.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/hardware/task_management.py) | Find, configure, start, and stop tasks.                      |
| [pagerduty/alert_task.py](https://github.com/synnaxlabs/synnax/blob/main/client/py/examples/pagerduty/alert_task.py)         | Open a PagerDuty incident when a status changes.             |

**TypeScript**

Write, read, and work with data. See [Write data](https://docs.synnaxlabs.com/reference/client/write-data) and [Read data](https://docs.synnaxlabs.com/reference/client/read-data).

| Example                                                                                                         | Shows how to                                        |
| --------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
| [basicReadWrite.js](https://github.com/synnaxlabs/synnax/blob/main/client/ts/examples/node/basicReadWrite.js)   | Create channels, write samples, and read them back. |
| [readLatest.js](https://github.com/synnaxlabs/synnax/blob/main/client/ts/examples/node/readLatest.js)           | Read the latest samples of a channel.               |
| [seriesAndFrames.js](https://github.com/synnaxlabs/synnax/blob/main/client/ts/examples/node/seriesAndFrames.js) | Work with series and frames.                        |

Stream data. See [Stream data](https://docs.synnaxlabs.com/reference/client/stream-data).

| Example                                                                                                 | Shows how to                      |
| ------------------------------------------------------------------------------------------------------- | --------------------------------- |
| [streamWrite.js](https://github.com/synnaxlabs/synnax/blob/main/client/ts/examples/node/streamWrite.js) | Write live samples with a writer. |
| [streamRead.js](https://github.com/synnaxlabs/synnax/blob/main/client/ts/examples/node/streamRead.js)   | Stream live samples.              |

Channels and ranges. See [Channels](https://docs.synnaxlabs.com/reference/channels) and [Ranges](https://docs.synnaxlabs.com/reference/ranges).

| Example                                                                                                       | Shows how to                      |
| ------------------------------------------------------------------------------------------------------------- | --------------------------------- |
| [createChannels.js](https://github.com/synnaxlabs/synnax/blob/main/client/ts/examples/node/createChannels.js) | Create many channels at once.     |
| [createRange.js](https://github.com/synnaxlabs/synnax/blob/main/client/ts/examples/node/createRange.js)       | Create a range and read its data. |
