Move more development information to the dev docs to minimise repitition and getting out of sync
|
|
@ -6,96 +6,4 @@ Any contributions to any of our projects should follow [our code of conduct](htt
|
|||
|
||||
If you are interested in contributing to the OpenFlexure project, and do not know where to start, we recommend you visit [our forum](https://openflexure.discourse.group/).
|
||||
|
||||
**The rest of this page details contribution guidelines that are specific to this repository.**
|
||||
|
||||
This page generally focusses on the back-end of the server, not the front-end webapp. For more details on the webapp development see the [webapp's README](./webapp/README.md).
|
||||
|
||||
## Python Development
|
||||
|
||||
For installation, see the [project README](./README.md).
|
||||
|
||||
### Core packages and concepts
|
||||
|
||||
The OpenFlexure Microscope server is built in the [LabThings-FastAPI framework](https://labthings-fastapi.readthedocs.io/en/latest/). This provides a convenient way to expose laboratory hardware over HTTP via a [FastAPI server](https://fastapi.tiangolo.com/).
|
||||
|
||||
The core elements of the server code are `Things`. A `Thing` creates a [W3C Web of Things](https://www.w3.org/TR/wot-architecture) compliant HTTP interface for hardware interactions and other server functionality.
|
||||
|
||||
The project also makes use of the following libraries:
|
||||
|
||||
* [Pydantic](https://docs.pydantic.dev/latest/) - For serialising/deserialising data being saved to disk or sent over HTTP.
|
||||
* [OpenFlexure Stitching](https://gitlab.com/openflexure/openflexure-stitching) - Our own library for creating composite microscope images from a scan.
|
||||
* [OpenCV](https://opencv.org/) and [Picamera2](https://github.com/raspberrypi/picamera2/) - For controlling cameras, and simulating cameras for tests.
|
||||
* [Pillow](https://pillow.readthedocs.io) - For image manipulation and saving.
|
||||
* [NumPy](https://numpy.org/) and [SciPy](https://scipy.org/) - For mathematical calculations.
|
||||
|
||||
*The list above is not exhaustive!*
|
||||
|
||||
## Programming style
|
||||
|
||||
Ideally, python code should be:
|
||||
|
||||
* Broken up into small, clear, testable functions.
|
||||
* Documented with docstrings (For conventions see below).
|
||||
* Type hinted
|
||||
* Covered by unit tests (using [pytest](https://docs.pytest.org))
|
||||
|
||||
The server code is going through significant flux during the transition from v2 to v3, so currently our test coverage is being rebuilt and type checking does not yet pass.
|
||||
|
||||
[Ruff](https://docs.astral.sh/ruff/) is used both for linting and formatting the codebase. Our custom linter rules are in our `pyproject.toml`.
|
||||
|
||||
Run `ruff format` to format the code in a way that will pass on our CI.
|
||||
|
||||
Run `ruff check` to check for linter errors. Any contributions that don't pass both of these checks will be automatically rejected by our pipeline.
|
||||
|
||||
To check your code before each commit we recommend putting the following script in tour `.git/hooks` directory with the filename `pre-commit`.
|
||||
|
||||
|
||||
```python
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
function angrymessage()
|
||||
{
|
||||
RED='\033[1;31m'
|
||||
NC='\033[0m' # No Color
|
||||
printf "\n${RED}Not committing as ruff failed${NC}\n\n"
|
||||
}
|
||||
|
||||
trap angrymessage ERR
|
||||
|
||||
ruff format --check
|
||||
ruff check
|
||||
```
|
||||
|
||||
|
||||
## Docstrings conventions.
|
||||
|
||||
We write docstrings in [reStructuredText](https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html), and process them with [pydoctor](https://pydoctor.readthedocs.io/).
|
||||
|
||||
For function arguments, return values, and exceptions we use the following style recognised by reStructuredText:
|
||||
|
||||
```python
|
||||
def a_function(arg1: str, arg2: int) -> tuple[float, int]:
|
||||
"""A one sentence summary, in imperative mood, ending with punctuation.
|
||||
|
||||
More detail after a blank line. This could be a whole paragraph.
|
||||
|
||||
When referring to ``code`` in reStructuredText used double backticks
|
||||
|
||||
:param arg1: This is the first argument, we can say everything we need to about it.
|
||||
If the text wraps onto a new line, this line is indented.
|
||||
:param arg2: This is the second argument.
|
||||
|
||||
:returns: Say what it returns without anything within the colons. If returning
|
||||
multiple values, a bulleted list is a good idea - just make sure:
|
||||
|
||||
* It is indented to 1 level in
|
||||
* There is a blank line before the bullets
|
||||
* The bullet points are ``*`` not ``-``
|
||||
"""
|
||||
```
|
||||
|
||||
+The linter rules we use for docstrings are defined by [pydocstyle](https://docs.astral.sh/ruff/rules/#pydocstyle-d). We do not use rules D203, D213, D400 for reasons explained in our `pyproject.toml` file.
|
||||
|
||||
The documentation generated by pydoctor from docstrings can be viewed in any merge request (if the CI pipeline has passed) using the "View App" button.
|
||||
Detailed guidelines specific to this project can be found in our [developer documentation](https://openflexure.gitlab.io/openflexure-microscope-server/dev/index.html).
|
||||
|
|
|
|||
133
README.md
|
|
@ -29,7 +29,7 @@ The Python package provides a command `ofm-microscope-server` that runs the serv
|
|||
|
||||
It is also possible to run the server in simulation mode. See Developer guidelines below.
|
||||
|
||||
## Settings
|
||||
## Configuration and Settings
|
||||
|
||||
The microscope is initially configured by a LabThings config file. This specifies two important things:
|
||||
|
||||
|
|
@ -38,133 +38,8 @@ The microscope is initially configured by a LabThings config file. This specifie
|
|||
|
||||
By default, this configuration file should be read from `/var/openflexure/settings/ofm_config.json` when the microscope is run as a service. If it is run at the command line you should specify the configuration using the `-c` command line flag. This configuration file does not change often, and usually only needs to be updated when you change the physical hardware. It may be that this file should be made read-only, particularly in microscopes deployed for e.g. medical applications.
|
||||
|
||||
The settings folder is, by default, `/var/openflexure/settings/` on the SD card, or `./settings/` if run elsewhere. It can be changed in the configuration file. This holds every other persistent setting. Camera settings, calibration data, default capture settings, stream resolution and so on. There is one folder per `Thing`, each with their own file. The settings files can change very regularly, and if you need to reset your settings, it's usually these files that you should remove or reset. If you delete one settings file this should reset the corresponding `Thing`, you don't have to delete the whole folder.
|
||||
The settings folder is, by default, `/var/openflexure/settings/` if running on a Raspberry Pi, or `./settings/` if run elsewhere. It can be changed in the configuration file. This holds every other persistent setting. Camera settings, calibration data, default capture settings, stream resolution and so on. There is one folder per `Thing`, each with their own file. The settings files can change very regularly, and if you need to reset your settings, it's usually these files that you should remove or reset. If you delete one settings file this should reset the corresponding `Thing`, you don't have to delete the whole folder.
|
||||
|
||||
# Developer guidelines
|
||||
## Development
|
||||
|
||||
## Installation and set up
|
||||
|
||||
To set up a development environment you will need Python 3.11 and the static files for the web application.
|
||||
|
||||
### Installation and set up on a Raspberry Pi
|
||||
|
||||
The OpenFlexure Raspberry Pi OS image comes with Python 3.11 already installed. We do not recommend trying to develop on the standard Raspberry Pi OS image.
|
||||
|
||||
|
||||
* To activate the microscope virtual environment run `ofm activate`
|
||||
* Navigate to the Openflexure repository with `cd /var/openflexure/application/openflexure-microscope-server`
|
||||
|
||||
The Pi should already come with the static files for the web application.
|
||||
|
||||
### Installation and set up on other platforms
|
||||
|
||||
You should install Python 3.11. If you have a different version of python we recommend using [UV](https://docs.astral.sh/uv/) to set up a Python 3.11 virtual environment.
|
||||
|
||||
You can check your Python version by running `py --version` or `python --version` in any terminal. Your Python version should be >= 3.11.0.
|
||||
|
||||
* Clone the source code: `git clone https://gitlab.com/openflexure/openflexure-microscope-server.git`
|
||||
* Enter the directory `cd openflexure-microscope-server`
|
||||
* Create the virtual environment, for example with `uv venv --python 3.11` (or `py -m venv .venv` if not using uv)
|
||||
* Activate your virtual environment with `source .venv/bin/activate` (on Linux) or `.venv\Scripts\activate.bat` (on Windows CMD) or `.venv\Scripts\activate` (on Windows Powershell)
|
||||
|
||||
_Note: If you activate the venv in a CMD terminal in VSCode on Windows, the venv will not visually show as activated until you open a new powershell window._
|
||||
|
||||
* Install the application `uv pip install -e .[dev]` (or `pip install -e .[dev]` if you are not using UV)
|
||||
* Run `python pull_webapp.py` to get the latest static files for the web application.
|
||||
|
||||
Full video walkthroughs of the software installation process are available below:
|
||||
|
||||
* [Windows Walkthrough](https://www.youtube.com/watch?v=LhnOLlIk-b8)
|
||||
* [Linux Walkthrough](https://youtu.be/81jkMKOBeHQ)
|
||||
|
||||
> If you are using Windows and you cannot activate your virtual environment from powershell, you probably need to change the execution policy. The official Microsoft docs have the right command in example #1: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.5#example-1-set-an-execution-policy.
|
||||
|
||||
## Running the server
|
||||
|
||||
There are two types of server available to run:
|
||||
|
||||
* A live OpenFlexure Microscope Server, that runs on a Raspberry Pi within your OpenFlexure Microscope. This server processes real images from the microscope's camera.
|
||||
|
||||
|  | |
|
||||
|:---:|:---:|
|
||||
|
||||
* A Simulation Server, which replicates the software functionality of the live server, but instead runs on your computer. Instead of receiving a live feed from a camera, the server 'scans' simulated data from a simulated camera.
|
||||
|
||||
|  | |
|
||||
|:---:|:---:|
|
||||
|
||||
|
||||
### Running the server on a Raspberry Pi
|
||||
|
||||
You can manage the server with the `ofm` command, using `ofm start`, `ofm stop`, and `ofm restart` to do the respective actions. Often, stopping the server and running it manually will make errors easier to spot. To do this, run:
|
||||
```
|
||||
ofm stop
|
||||
ofm activate
|
||||
cd /var/openflexure
|
||||
sudo -u openflexure-ws openflexure-microscope-server --fallback -c microscope_configuration.json --host 0.0.0.0 --port 5000
|
||||
```
|
||||
|
||||
### Running the simulation server on other platforms
|
||||
|
||||
To start the simulation server:
|
||||
|
||||
* Ensure all steps of the installation for your platform of choice are complete.
|
||||
* Activate the virtual environment that was set up during installation
|
||||
* Run the following command:
|
||||
|
||||
`openflexure-microscope-server --fallback -c ./ofm_config_simulation.json`
|
||||
|
||||
* Open the address displayed in the terminal in any browser.
|
||||
* To close the simulation server, return to the terminal and press `ctrl + c`.
|
||||
|
||||
> Note: If you are using Windows, we advise running this command in PowerShell. If it doesn't work this, might be a permission issue on managed machines, in which case try executing the command in a CMD terminal.
|
||||
|
||||
> Note: If you receive an error saying that port 5000 is already in use, you can specify a different port to run the simulation on using `--port`:
|
||||
>
|
||||
> `openflexure-microscope-server --fallback --port 8081 -c ./ofm_config_simulation.json`
|
||||
|
||||
A [full video walkthrough of running the simulation server](https://youtu.be/Z8verCqfj9s) is available on our YouTube Channel.
|
||||
|
||||
You can find more information on using the simulation server in the [Simulation Guide](./simulation_guide.md).
|
||||
|
||||
|
||||
## General development overview
|
||||
|
||||
Please first read our **[project contributions guide](./CONTRIBUTING.md)**
|
||||
|
||||
* We recommend all development is done on a branch.
|
||||
* If you are editing the web app, please see our see the README in the [webapp](./webapp) directory.
|
||||
* If you want to try a branch with webapp changes (that are already pushed to GitLab), but don't want to compile the javascript, then run `python pull_webapp.py -b <branch_name>` to get the artifacts from the GitLab CI.
|
||||
|
||||
**For development on a Raspberry Pi** we recommend VSCode Remote session from another computer. This allows you to use your usual developer environment to write code, but everything runs on the Raspberry Pi with real hardware. Note that the repository is cloned by default over `https`, so you will probably need to change the "remote" URL to your fork of the repository, or to SSH, before you're able to push changes.
|
||||
|
||||
### Adding functionality
|
||||
|
||||
The microscope comprises a number of `Thing` instances, which provide actions and properties to implement hardware control and software features. These may be imported from any Python module, using `ofm_config.json`. See the LabThings-FastAPI documentation for how to create a `Thing`.
|
||||
|
||||
Currently, the camera and stage classes are provided by external libraries, `labthings-picamera2` and `labthings-sangaboard`. Replacing these is the easiest way to use alternative hardware: interfaces are defined in `openflexure_microscope_server.things.camera` and `openflexure_microscope_server.things.stage` respectively.
|
||||
|
||||
|
||||
### Python: Formatting, linting, and tests
|
||||
|
||||
All of the commands below assume that you are running in the OFM virtual environment, i.e. you have run `ofm activate` on an OpenFlexure SD card, or `source .venv/bin/activate` on Linux, or `.venv/Scripts/activate`on Windows Powershell, or `.venv\Scripts\activate.bat` on Windows CMD.
|
||||
|
||||
**Before committing** you should lint and auto-format your code:
|
||||
|
||||
* To lint run `ruff check`
|
||||
* To auto-format the Python code run `ruff format`
|
||||
* To auto-format the Javascript code, run
|
||||
* `cd webapp`
|
||||
* `npm run lint`
|
||||
|
||||
**Before merging** please auto-format your code and also run the quality checks (linting, static analysis, and unit tests)
|
||||
|
||||
* All commands above
|
||||
* `pytest`
|
||||
|
||||
|
||||
We use several code analysis and formatting libraries in this project. Our CI will check each of these automatically, so ensuring they pass locally will save you time. Currently `ruff` is used for linting/formatting and `pytest` for unit tests. `mypy` will be enabled once the codebase is ready.
|
||||
|
||||
### Python environment, build, and dependencies
|
||||
|
||||
As of `v3` we specify dependencies in `pyproject.toml`. These are not currently frozen due to difficulties matching versions on different platforms. On Raspberry Pi, it's best to specify `--only-binary=:all:` to ensure libraries like `numpy` and `scipy` are not compiled from source (which takes many hours, and/or fails). Pinning dependencies with `requirements.txt` and/or `requirements.in` may happen in the future.
|
||||
For development please see our [developer documentation](https://openflexure.gitlab.io/openflexure-microscope-server/dev/index.html).
|
||||
|
|
@ -2,8 +2,112 @@
|
|||
|
||||
This site provides information that is useful for developing the OpenFlexure Microscope.
|
||||
|
||||
### Viewing and Modifying Locally
|
||||
## Viewing and Modifying These Docs Locally
|
||||
|
||||
If you want to view these documents in your machine (very useful when editing them). You will need to serve the `apidocs` directory. Navigate to the `apidocs` directory and run `python -m http.server`.
|
||||
|
||||
All developer guidance documentation is written in markdown and then displayed with Docsify. More information on Docsify can be found at https://docsify.js.org.
|
||||
All developer guidance documentation is written in markdown and then displayed with Docsify. More information on Docsify can be found at https://docsify.js.org.
|
||||
|
||||
|
||||
## Installation and set up
|
||||
|
||||
To set up a development environment you will need Python 3.11 and the static files for the web application.
|
||||
|
||||
### Installation and set up on a Raspberry Pi
|
||||
|
||||
The OpenFlexure Raspberry Pi OS image comes with Python 3.11 already installed. We do not recommend trying to develop on the standard Raspberry Pi OS image.
|
||||
|
||||
|
||||
* To activate the microscope virtual environment run `ofm activate`
|
||||
* Navigate to the Openflexure repository with `cd /var/openflexure/application/openflexure-microscope-server`
|
||||
|
||||
The Pi should already come with the static files for the web application.
|
||||
|
||||
### Installation and set up on other platforms
|
||||
|
||||
You should install Python 3.11. If you have a different version of python we recommend using [UV](https://docs.astral.sh/uv/) to set up a Python 3.11 virtual environment.
|
||||
|
||||
You can check your Python version by running `py --version` or `python --version` in any terminal. Your Python version should be >= 3.11.0.
|
||||
|
||||
* Clone the source code: `git clone https://gitlab.com/openflexure/openflexure-microscope-server.git`
|
||||
* Enter the directory `cd openflexure-microscope-server`
|
||||
* Create the virtual environment, for example with `python -m venv .venv` (or with `uv venv --python 3.11` if using uv)
|
||||
* Activate your virtual environment with `source .venv/bin/activate` (on Linux) or `.venv\Scripts\activate.bat` (on Windows CMD) or `.venv\Scripts\activate` (on Windows Powershell)
|
||||
|
||||
_Note: If you activate the venv in a CMD terminal in VSCode on Windows, the venv will not visually show as activated until you open a new powershell window._
|
||||
|
||||
* Install the application `pip install -e .[dev]` (or `uv pip install -e .[dev]` if you are using UV)
|
||||
* Run `python pull_webapp.py` to get the latest static files for the web application.
|
||||
|
||||
Full video walkthroughs of the software installation process are available below:
|
||||
|
||||
* [Windows Walkthrough](https://www.youtube.com/watch?v=LhnOLlIk-b8)
|
||||
* [Linux Walkthrough](https://youtu.be/81jkMKOBeHQ)
|
||||
|
||||
> If you are using Windows and you cannot activate your virtual environment from powershell, you probably need to change the execution policy. The official Microsoft docs have the right command in example #1: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.5#example-1-set-an-execution-policy.
|
||||
|
||||
## Running the server
|
||||
|
||||
There are two types of server available to run:
|
||||
|
||||
* A live OpenFlexure Microscope Server, that runs on a Raspberry Pi within your OpenFlexure Microscope. This server processes real images from the microscope's camera.
|
||||
|
||||
|  | |
|
||||
|:---:|:---:|
|
||||
|
||||
* A Simulation Server, which replicates the software functionality of the live server, but instead runs on your computer. Instead of receiving a live feed from a camera, the server 'scans' simulated data from a simulated camera.
|
||||
|
||||
|  | |
|
||||
|:---:|:---:|
|
||||
|
||||
|
||||
### Running the server on a Raspberry Pi
|
||||
|
||||
You can manage the server with the `ofm` command, using `ofm start`, `ofm stop`, and `ofm restart` to do the respective actions. Often, stopping the server and running it manually will make errors easier to spot. To do this, run:
|
||||
```
|
||||
ofm stop
|
||||
ofm activate
|
||||
cd /var/openflexure
|
||||
sudo -u openflexure-ws openflexure-microscope-server --fallback -c microscope_configuration.json --host 0.0.0.0 --port 5000
|
||||
```
|
||||
|
||||
### Running the simulation server on other platforms
|
||||
|
||||
To start the simulation server:
|
||||
|
||||
* Ensure all steps of the installation for your platform of choice are complete.
|
||||
* Activate the virtual environment that was set up during installation
|
||||
* Run the following command:
|
||||
|
||||
`openflexure-microscope-server --fallback -c ./ofm_config_simulation.json`
|
||||
|
||||
* Open the address displayed in the terminal in any browser.
|
||||
* To close the simulation server, return to the terminal and press `ctrl + c`.
|
||||
|
||||
> Note: If you are using Windows, we advise running this command in PowerShell. If it doesn't work this, might be a permission issue on managed machines, in which case try executing the command in a CMD terminal.
|
||||
|
||||
> Note: If you receive an error saying that port 5000 is already in use, you can specify a different port to run the simulation on using `--port`:
|
||||
>
|
||||
> `openflexure-microscope-server --fallback --port 8081 -c ./ofm_config_simulation.json`
|
||||
|
||||
A [full video walkthrough of running the simulation server](https://youtu.be/Z8verCqfj9s) is available on our YouTube Channel.
|
||||
|
||||
You can find more information on using the simulation server in the [Simulation Guide](simulation_guide).
|
||||
|
||||
|
||||
## General development overview
|
||||
|
||||
Please first read our **[project contributions guide](contributing)**
|
||||
|
||||
* We recommend all development is done on a branch.
|
||||
* If you are editing the web app, please see our see the README in the [webapp](webapp) directory.
|
||||
* If you want to try a branch with webapp changes (that are already pushed to GitLab), but don't want to compile the javascript, then run `python pull_webapp.py -b <branch_name>` to get the artifacts from the GitLab CI.
|
||||
|
||||
**For development on a Raspberry Pi** we recommend VSCode Remote session from another computer. This allows you to use your usual developer environment to write code, but everything runs on the Raspberry Pi with real hardware. Note that the repository is cloned by default over `https`, so you will probably need to change the "remote" URL to your fork of the repository, or to SSH, before you're able to push changes.
|
||||
|
||||
### Adding functionality
|
||||
|
||||
The microscope comprises a number of `Thing` instances, which provide actions and properties to implement hardware control and software features. These may be imported from any Python module, using `ofm_config.json`. See the LabThings-FastAPI documentation for how to create a `Thing`.
|
||||
|
||||
Currently, the camera and stage classes are provided by external libraries, `labthings-picamera2` and `labthings-sangaboard`. Replacing these is the easiest way to use alternative hardware: interfaces are defined in `openflexure_microscope_server.things.camera` and `openflexure_microscope_server.things.stage` respectively.
|
||||
|
||||
|
|
|
|||
|
|
@ -5,5 +5,9 @@
|
|||
<h2>Developer Guidance<h2>
|
||||
|
||||
- [Home](README)
|
||||
- [Software Architecture](architecture.md)
|
||||
- [PiCamera Tuning Files](picamera-tuning-files.md)
|
||||
- [Contributing](contributing)
|
||||
- [Python Development](python)
|
||||
- [Webapp Development](webapp)
|
||||
- [Running the Simulator](simulation_guide)
|
||||
- [Software Architecture](architecture)
|
||||
- [PiCamera Tuning Files](picamera-tuning-files)
|
||||
|
Before Width: | Height: | Size: 122 KiB After Width: | Height: | Size: 122 KiB |
|
Before Width: | Height: | Size: 106 KiB After Width: | Height: | Size: 106 KiB |
|
Before Width: | Height: | Size: 117 KiB After Width: | Height: | Size: 117 KiB |
|
Before Width: | Height: | Size: 111 KiB After Width: | Height: | Size: 111 KiB |
36
apidocs/dev/contributing.md
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
# Contributing to the OpenFlexure Microscope Server
|
||||
|
||||
All contributions to the OpenFlexure project should follow our [project contributions guidelines](https://openflexure.org/contribute).
|
||||
|
||||
Any contributions to any of our projects should follow [our code of conduct](https://openflexure.org/conduct).
|
||||
|
||||
If you are interested in contributing to the OpenFlexure project, and do not know where to start, we recommend you visit [our forum](https://openflexure.discourse.group/).
|
||||
|
||||
**The rest of this page details contribution guidelines that are specific to this repository.**
|
||||
|
||||
This page generally focusses on the back-end of the server, not the front-end webapp. For more details on the webapp development see the [webapp's README](webapp).
|
||||
|
||||
## Installation
|
||||
|
||||
For installation, see the [home page](README).
|
||||
|
||||
### Core packages and concepts
|
||||
|
||||
The OpenFlexure Microscope server is built in the [LabThings-FastAPI framework](https://labthings-fastapi.readthedocs.io/en/latest/). This provides a convenient way to expose laboratory hardware over HTTP via a [FastAPI server](https://fastapi.tiangolo.com/).
|
||||
|
||||
The core elements of the server code are `Things`. A `Thing` creates a [W3C Web of Things](https://www.w3.org/TR/wot-architecture) compliant HTTP interface for hardware interactions and other server functionality.
|
||||
|
||||
The project also makes use of the following libraries:
|
||||
|
||||
* [Pydantic](https://docs.pydantic.dev/latest/) - For serialising/deserialising data being saved to disk or sent over HTTP.
|
||||
* [OpenFlexure Stitching](https://gitlab.com/openflexure/openflexure-stitching) - Our own library for creating composite microscope images from a scan.
|
||||
* [OpenCV](https://opencv.org/) and [Picamera2](https://github.com/raspberrypi/picamera2/) - For controlling cameras, and simulating cameras for tests.
|
||||
* [Pillow](https://pillow.readthedocs.io) - For image manipulation and saving.
|
||||
* [NumPy](https://numpy.org/) and [SciPy](https://scipy.org/) - For mathematical calculations.
|
||||
|
||||
*The list above is not exhaustive!*
|
||||
|
||||
## Coding guidelines
|
||||
|
||||
We have separate coding guidelines for [the Python server](python) and for the [Vue webapp](webapp).
|
||||
|
||||
92
apidocs/dev/python.md
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
# Python Server Development
|
||||
|
||||
## Overview
|
||||
|
||||
|
||||
Ideally, Python code should be:
|
||||
|
||||
* Broken up into small, clear, testable functions.
|
||||
* Documented with docstrings (For conventions see below).
|
||||
* Type hinted
|
||||
* Covered by unit tests (using [pytest](https://docs.pytest.org))
|
||||
|
||||
The server code is going through significant flux during the transition from v2 to v3, so currently our test coverage is being rebuilt and type checking does not yet pass.
|
||||
|
||||
## Python environment, build, and dependencies
|
||||
|
||||
As of `v3` we specify dependencies in `pyproject.toml`. These are not currently frozen due to difficulties matching versions on different platforms. On Raspberry Pi, it's best to specify `--only-binary=:all:` to ensure libraries like `numpy` and `scipy` are not compiled from source (which takes many hours, and/or fails). Pinning dependencies with `requirements.txt` and/or `requirements.in` may happen in the future.
|
||||
|
||||
|
||||
|
||||
## Python: Formatting, linting, and tests
|
||||
|
||||
[Ruff](https://docs.astral.sh/ruff/) is used both for linting and formatting the codebase. Our custom linter rules are in our `pyproject.toml`.
|
||||
|
||||
> All of the commands below assume that you are running in the OFM virtual environment, i.e. you have run `ofm activate` on an OpenFlexure SD card, or `source .venv/bin/activate` on Linux, or `.venv/Scripts/activate`on Windows Powershell, or `.venv\Scripts\activate.bat` on Windows CMD.
|
||||
|
||||
**Before committing** you should lint and auto-format your code:
|
||||
|
||||
* To lint run `ruff check`
|
||||
* To auto-format the Python code run `ruff format`
|
||||
|
||||
**Before merging** please auto-format your code and also run the quality checks (linting, static analysis, and unit tests)
|
||||
|
||||
* All commands above
|
||||
* `pytest`
|
||||
|
||||
|
||||
We use several code analysis and formatting libraries in this project. Our CI will check each of these automatically, so ensuring they pass locally will save you time. Currently `ruff` is used for linting/formatting and `pytest` for unit tests. `mypy` will be enabled once the codebase is ready.
|
||||
|
||||
|
||||
To check your code before each commit we recommend putting the following script in tour `.git/hooks` directory with the filename `pre-commit`.
|
||||
|
||||
|
||||
```python
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
function angrymessage()
|
||||
{
|
||||
RED='\033[1;31m'
|
||||
NC='\033[0m' # No Color
|
||||
printf "\n${RED}Not committing as ruff failed${NC}\n\n"
|
||||
}
|
||||
|
||||
trap angrymessage ERR
|
||||
|
||||
ruff format --check
|
||||
ruff check
|
||||
```
|
||||
|
||||
|
||||
## Docstrings conventions.
|
||||
|
||||
We write docstrings in [reStructuredText](https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html), and process them with [pydoctor](https://pydoctor.readthedocs.io/).
|
||||
|
||||
For function arguments, return values, and exceptions we use the following style recognised by reStructuredText:
|
||||
|
||||
```python
|
||||
def a_function(arg1: str, arg2: int) -> tuple[float, int]:
|
||||
"""A one sentence summary, in imperative mood, ending with punctuation.
|
||||
|
||||
More detail after a blank line. This could be a whole paragraph.
|
||||
|
||||
When referring to ``code`` in reStructuredText used double backticks
|
||||
|
||||
:param arg1: This is the first argument, we can say everything we need to about it.
|
||||
If the text wraps onto a new line, this line is indented.
|
||||
:param arg2: This is the second argument.
|
||||
|
||||
:returns: Say what it returns without anything within the colons. If returning
|
||||
multiple values, a bulleted list is a good idea - just make sure:
|
||||
|
||||
* It is indented to 1 level in
|
||||
* There is a blank line before the bullets
|
||||
* The bullet points are ``*`` not ``-``
|
||||
"""
|
||||
```
|
||||
|
||||
+The linter rules we use for docstrings are defined by [pydocstyle](https://docs.astral.sh/ruff/rules/#pydocstyle-d). We do not use rules D203, D213, D400 for reasons explained in our `pyproject.toml` file.
|
||||
|
||||
The documentation generated by pydoctor from docstrings can be viewed in any merge request (if the CI pipeline has passed) using the "View App" button.
|
||||
|
|
@ -16,7 +16,7 @@ The simulation server GUI is a copy of the GUI you would find on the hardware wi
|
|||
|
||||
## Starting the Simulation Server
|
||||
|
||||
1. **Activate your virtual environment** (the same one created during installation, as described in the [README](./README.md)).
|
||||
1. **Activate your virtual environment** (the same one created during installation, as described in the [Python documentation](python)).
|
||||
2. **Run the simulation server** using the following command:
|
||||
|
||||
```bash
|
||||
|
|
@ -1,18 +1,11 @@
|
|||
# OpenFlexure Microscope JS Client
|
||||
# WebApp Development
|
||||
|
||||
## Key info
|
||||
|
||||
* Vue.js web application providing a graphical interface for the OFM
|
||||
* Vue.js web application providing a graphical interface for the Openflexure Microscope.
|
||||
* Once built, will be served by the API server from the host root on port 5000
|
||||
|
||||
* See [openflexure-microscope-server/README.md](https://gitlab.com/openflexure/openflexure-microscope-server/-/blob/master/README.md) for details on local installation and building
|
||||
|
||||
# Developer guidelines
|
||||
|
||||
## Creating releases
|
||||
|
||||
* JS client is coupled to the API, and is built and distributed with the server.
|
||||
* See [openflexure-microscope-server/README.md](https://gitlab.com/openflexure/openflexure-microscope-server/-/blob/master/README.md) for details on creating new releases
|
||||
|
||||
|
||||
## Installing
|
||||
|
||||
|
|
@ -23,7 +16,7 @@ _Note: If you are using a Windows OS, the following commands must be executed in
|
|||
* Install dependencies with `npm install`
|
||||
* Build the static web app with `npm run build`
|
||||
|
||||
# Developing
|
||||
## Live Server
|
||||
|
||||
When developing it is useful to use the development server so Vue changes happen instantly without the need to rebuild. To start the development server run:
|
||||
|
||||
|
|
@ -33,19 +26,10 @@ The development server is accessed on a different port from the microscope API.
|
|||
|
||||
When the development webapp starts it cannot locate the microscope API as this is served by the microscope on port 5000. Instead it will present you with a page giving you the option to "override API origin". If you are running a development server on your computer, you should enter `http://localhost:5000/`.
|
||||
|
||||
## VS Code and ESLint
|
||||
# Javascript: Formatting and linting
|
||||
|
||||
To prevent the editor from interfering with ESLint, add to your project `settings.json`:
|
||||
To enforce code style we and quality we use ESLint and Prettier. Both can be run together with the same command.
|
||||
|
||||
- To check the for errors and warnings run `npm run lint`
|
||||
- To automatically fix errors an warnings run `npm run lint:fix`
|
||||
|
||||
```json
|
||||
{
|
||||
"editor.tabSize": 2,
|
||||
"cSpell.enabled": false,
|
||||
"eslint.validate": ["vue","javascript", "javascriptreact"],
|
||||
"editor.formatOnSave": false,
|
||||
"vetur.validation.template": false,
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.fixAll.eslint": true
|
||||
}
|
||||
}
|
||||
```
|
||||