Skip to content

Commit c1d7b0a

Browse files
committed
Basic tutorial on how to install and run
1 parent 719cf93 commit c1d7b0a

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
lines changed

docs/source/index.rst

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
.. labthings-fastapi documentation master file, created by
2-
sphinx-quickstart on Wed May 15 16:34:51 2024.
3-
You can adapt this file completely to your liking, but it should at least
4-
contain the root `toctree` directive.
5-
6-
Welcome to labthings-fastapi's documentation!
1+
Documentation for LabThings-FastAPI
72
=============================================
83

94
.. toctree::
105
:maxdepth: 2
116
:caption: Contents:
127

13-
wot_core_concepts.rst
148
quickstart/quickstart.rst
9+
wot_core_concepts.rst
10+
tutorial/index.rst
1511
dependencies/dependencies.rst
1612
concurrency.rst
1713
client_code.rst

docs/source/quickstart/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/settings/

docs/source/tutorial/index.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
LabThings-FastAPI tutorial
2+
==========================
3+
4+
.. toctree:
5+
6+
installing_labthings.rst
7+
running_labthings.rst
8+
writing_a_thing.rst
9+
client_code.rst
10+
blobs.rst
11+
thing_dependencies.rst
12+
13+
In this tutorial, we'll cover how to start up and interact with a LabThings-FastAPI server, how to write a Thing, and a few more advanced topics. It is intended as an introduction for someone using LabThings-FastAPI and/or writing Thing code to implement a new instrument. It doesn't detail all the internal workings of the library, but we hope this isn't needed for most people.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Installing LabThings-FastAPI
2+
============================
3+
4+
LabThings-FastAPI is a Python package, which is published to PyPI. You can install `labthings-fastapi` using `pip`. To see compatible versions of Python, please check PyPI_.
5+
6+
It is common practice to use virtual environments in Python: this isolates projects from each other, and makes sure that installing packages for one project doesn't break other work you are doing. There are many ways of managing virtual environments in Python: if you are using a distribution like Anaconda, you may prefer to manage environments using the `conda` command or Anaconda interface. This tutorial uses the built-in `venv` module to create a virtual environment, but you can use whatever tool you are happy with.
7+
8+
The commands below are all intended to be run in a terminal. We tend to use PowerShell on Windows, Terminal on a mac or your preferred terminal utility if you are on Linux. Note that most of our automated testing runs on Linux, and one or two commands are different on Windows. This is indicated with a comment (some text after a ``#`` character).
9+
10+
It's always a good idea to check your Python version before you start, by running ``python --version``. This should print out something like ``Python 3.12.3``, although the exact version is not particularly important so long as it's up to date enough for the package to install. If this doesn't work, you likely need to install Python, which this tutorial doesn't cover. The Python website has instructions for most common operating systems.
11+
12+
To create a virtual environment, run the following command:
13+
14+
.. literalinclude:: ../quickstart/quickstart_example.sh
15+
:language: bash
16+
:start-after: BEGIN venv
17+
:end-before: END venv
18+
19+
then install labthings with:
20+
21+
.. literalinclude:: ../quickstart/quickstart_example.sh
22+
:language: bash
23+
:start-after: BEGIN install
24+
:end-before: END install
25+
26+
It is also possible to install LabThings from source, by cloning the GitHub repository and running ``pip install -e .[dev]``, but this is only recommended if you intend to alter the LabThings-FastAPI library; it is best to use the published package unless you have a good reason not to.
27+
28+
.. _PyPI: https://pypi.org/project/labthings-fastapi/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Running LabThings-FastAPI
2+
=========================
3+
4+
Each time you want to use LabThings-FastAPI, you will need to open a terminal and activate your virtual environment. If you created a virtual environment using the command on the :doc:`installing_labthings` page, you will need to change directory to the folder where you created your virtual environment (using `cd`) and then activate the virtual environment with `source .venv/bin/activate` or `.venv/Scripts/activate` (on Windows).
5+
6+
Once you have activated the virtual environment, you should be able to run an example server with the command:
7+
8+
.. code-block:: bash
9+
10+
labthings-server --json '{"things":{"/mything":"labthings_fastapi.example_things:MyThing"}}'
11+
12+
This command will start a LabThings server, and will print the root URL for your server (by default, ``http://127.0.0.1:5000``). The ``127.0.0.1`` part means the server is only accessible from your computer, so you don't need to worry about other computers on your network accessing it.
13+
14+
Now that your server is running, you should be able to view the interactive documentation in your web browser. There is an OpenAPI documentation page at ``http://127.0.0.1:5000/docs/``. This shows all the requests that the server supports, and even allows you to try them out in the web browser.
15+
16+
Another important document is the Thing Description: this is a higher-level description of all the capabilities of each Thing in the server. For our example server, we have just one Thing, which is at ``http://127.0.0.1:5000/mything/``. This is a JSON document, but if you view it in Firefox there is a convenient tree view that makes it easier to navigate. Currently the Thing Description is not as interactive as the OpenAPI documentation, but it is rather neater as it's a higher-level description: rather than describing every possible request, it describes the capabilities of your Thing in a way that should correspond nicely to the code you might write using a Python client object, or a client in some other language.
17+
18+
It is worth unpicking the command you ran to start the server: it has one argument, which is a JSON string. This is fine if you are starting up a test server for one Thing, but it gets unwieldy very quickly. Most of the time, you will want to start the server with a configuration file. This is a JSON file that contains the same information as the JSON string you passed to the command above, but in a more convenient format. To do this, create a file called `example_things.json` in the same directory as your virtual environment, and put the following content in it:
19+
20+
.. code-block:: json
21+
22+
{
23+
"things": {
24+
"/mything": "labthings_fastapi.example_things:MyThing"
25+
}
26+
}
27+
28+
You can then start the server using the command:
29+
30+
.. code-block:: bash
31+
32+
labthings-server --config example_things.json
33+

0 commit comments

Comments
 (0)