|
| 1 | +Quick start |
| 2 | +=========== |
| 3 | + |
| 4 | +You can install `labthings-fastapi` using `pip`. We recommend you create a virtual environment, for example: |
| 5 | + |
| 6 | + |
| 7 | +.. literalinclude:: quickstart_example.sh |
| 8 | + :language: bash |
| 9 | + :start-after: BEGIN venv |
| 10 | + :end-before: END venv |
| 11 | + |
| 12 | +then install labthings with: |
| 13 | + |
| 14 | +.. literalinclude:: quickstart_example.sh |
| 15 | + :language: bash |
| 16 | + :start-after: BEGIN install |
| 17 | + :end-before: END install |
| 18 | + |
| 19 | +To define a simple example ``Thing``, paste the following into a python file, ``counter.py``: |
| 20 | + |
| 21 | +.. literalinclude:: counter.py |
| 22 | + :language: python |
| 23 | + |
| 24 | +``counter.py`` defines the ``TestThing`` class, and then runs a LabThings server in its ``__name__ == "__main__"`` block. This means we should be able to run the server with: |
| 25 | + |
| 26 | + |
| 27 | +.. literalinclude:: quickstart_example.sh |
| 28 | + :language: bash |
| 29 | + :start-after: BEGIN serve |
| 30 | + :end-before: END serve |
| 31 | + |
| 32 | +Visiting http://localhost:5000/counter/ will show the thing description, and you can interact with the actions and properties using the Swagger UI at http://localhost:5000/docs/. |
| 33 | + |
| 34 | +You can also interact with it from another Python instance, for example by running: |
| 35 | + |
| 36 | +.. literalinclude:: counter_client.py |
| 37 | + :language: python |
| 38 | + |
| 39 | +It's best to write ``Thing`` subclasses in Python packages that can be imported. This makes them easier to re-use and distribute, and also allows us to run a LabThings server from the command line, configured by a configuration file. An example config file is below: |
| 40 | + |
| 41 | +.. literalinclude:: example_config.json |
| 42 | + :language: JSON |
| 43 | + |
| 44 | +Paste this into ``example_config.json`` and then run a server using: |
| 45 | + |
| 46 | +.. code-block:: bash |
| 47 | +
|
| 48 | + labthings-server -c example_config.json |
| 49 | +
|
| 50 | +Bear in mind that this won't work if `counter.py` above is still running - both will try to use port 5000. |
| 51 | + |
| 52 | +As before, you can visit http://localhost:5000/docs or http://localhost:5000/example/ to see the OpenAPI docs or Thing Description, and you can use the Python client module with the second of those URLs. |
0 commit comments