Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/docs/pyrobird.md is written in an unsupported language. File is not indexed.

0001 # pyrobird
0002 
0003 [![PyPI - Version](https://img.shields.io/pypi/v/pyrobird.svg)](https://pypi.org/project/pyrobird)
0004 [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pyrobird.svg)](https://pypi.org/project/pyrobird)
0005 
0006 -----
0007 
0008 ## Installation
0009 
0010 ```bash
0011 pip install pyrobird
0012 ```
0013 
0014 Optional dependencies:
0015 
0016 - `batch` - install playwright, that allows to make screenshots in batch mode
0017 - `xrootd` - install libraries to read xrootd located files and URLs starting with `root://`
0018 - `test` - install pytest, mainly to run tests in development build
0019 
0020 > If using `batch` for screenshots, after installing playwright, you need to install browser binaries:
0021 > ```bash
0022 > python -m playwright install chromium
0023 > ```
0024 
0025 > If installed via pip, `xrootd` library requires compilation, so the system should have cmake,
0026 > compiler and some xrootd dependencies installed.
0027 
0028 Development installation
0029 
0030 ```bash
0031 python -m pip install --editable .[test,batch]
0032 ```
0033 
0034 Running with Gunicorn (development mode)
0035 
0036 ```bash
0037 gunicorn --bind 0.0.0.0:5454 pyrobird.server:flask_app --log-level debug --capture-output
0038 ```
0039 
0040 
0041 ## Contributing
0042 
0043 - [PEP8](https://peps.python.org/pep-0008/) is required
0044 - [Use Numpy style dockstring comments](https://numpydoc.readthedocs.io/en/latest/format.html)
0045 - [pytest](https://docs.pytest.org/en/latest/) is used for unit tests. Aim for comprehensive coverage of the new code.
0046 - Utilize [type hints](https://docs.python.org/3/library/typing.html) wherever is possible to enhance readability and reduce errors.
0047 - Use of specific exceptions for error handling is better. As described in the [Python documentation](https://docs.python.org/3/tutorial/errors.html) rather than general exceptions.
0048 - Contributions are subject to code review. Please submit pull requests (PRs) against the `main` branch for any contributions.
0049 - Manage dependencies appropriately. Add new dependencies to `pyproject.toml`. Provide a justification for new dependencies
0050 
0051 ## Testing
0052 
0053 To install dependencies with testing libraries included
0054 
0055 ```bash
0056 pip install .[test]
0057 ```
0058 
0059 Navigate to the pyrobird/tests and execute:
0060 
0061 ```bash
0062 pytest
0063 
0064 # To stop immediately on error and enter debugging mode
0065 pytest -x --pdb 
0066 ```
0067 
0068 ## Development install
0069 
0070 ```
0071 python -m pip install --upgrade --editable  .[test]
0072 ```
0073 
0074 # Pyrobird Server
0075 
0076 This server allows Firebird to work with local files and the local file system as
0077 well as to complement frontend features such as opening XRootD files, etc.
0078 
0079 Serve Firobird locally and have access to files in current directory:
0080 
0081 ```bash
0082 pyrobird serve
0083 ```
0084 
0085 **pyrobird** (backend) allows **Firebird** (frontend) to access certain files on your system.
0086 For this reason pyrobird server has multiple endpoints such as `/api/v1/download`
0087 which allows to download files. There are library files served and by default Firebird
0088 has access to files in your current directory or a directory provided via `--work-path` flag
0089 
0090 
0091 #### **Available Options**
0092 
0093 | Option                     | Short | Type    | Default | Description                                                                                                                                                                                                                                   |
0094 |----------------------------|-------|---------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
0095 | `--allow-any-file`         |       | Flag    | `False` | Allow unrestricted access to download files in the system. When enabled, the server allows downloads of all files which the running user has access to. **Use with caution**: It is considered dangerous in production environments.            |
0096 | `--allow-cors`             |       | Flag    | `False` | Enable CORS for downloaded files. This option should be used if you need to support web applications from different domains accessing the files, such as serving your server from a central Firebird server.                                       |
0097 | `--disable-files`          |       | Flag    | `False` | Disable all file downloads from the server. This option will prevent any file from being downloaded, enhancing security by restricting file access.                                                                                              |
0098 | `--work-path TEXT`         |       | String  | `CWD`   | Set the base directory path for file downloads. Defaults to the current working directory. Use this option to specify where the server should look for files when handling download requests.                                                       |
0099 
0100 
0101 > `--allow-any-file` - allows unrestricted access to download files in a system.
0102 > When enabled, the server allows downloads of all files that user has access to.
0103 > When disabled - only files in `--work-path` and its subdirectories are allowed to be downloaded.
0104 > This option could be considered safe on personal machines with a single user,
0105 > who runs localhost server in a terminal
0106 >
0107 > (!) It is considered dangerous in all other cases: farms, interactive nodes, production environments, servers, etc.
0108 > Just think `/etc/passwd` will be accessible through  `localhost:port/api/v1/download?f=/etc/passwd`
0109 >
0110 > So security wise, it is better to use `--work-path` than `--allow-any-file`
0111 
0112 
0113 - Start server with default settings, Firebird works with files in current directory:
0114    ```bash
0115   fbd serve
0116    ```
0117 
0118 - Set where Firebird will take files from:
0119 
0120    ```bash
0121    fbd serve --work-path=/home/username/datafiles
0122    ```
0123 
0124   Now if you set file `local://filename.root` in Firebird UI,
0125   the file `/home/username/datafiles/filename.root` will be opened
0126 
0127 
0128 ## Batch Screenshots
0129 
0130 The `pyrobird screenshot` command allows you to capture screenshots of Firebird visualizations in batch mode. This is particularly useful for:
0131 
0132 - Generating visualizations for publications and presentations
0133 - Creating automated documentation of event displays
0134 - Batch processing multiple events for comparison
0135 - Quality assurance and validation workflows
0136 
0137 ### Installation
0138 
0139 To use the screenshot functionality, you need to install pyrobird with the `batch` optional dependency:
0140 
0141 ```bash
0142 pip install pyrobird[batch]
0143 ```
0144 
0145 After installing, you need to install the Chromium browser binaries used by Playwright:
0146 
0147 ```bash
0148 python -m playwright install chromium
0149 ```
0150 
0151 > **Note**: On the first run, Playwright will download Chromium (~100MB) if it's not already installed on your system.
0152 
0153 ### Basic Usage
0154 
0155 The simplest way to capture a screenshot:
0156 
0157 ```bash
0158 pyrobird screenshot
0159 ```
0160 
0161 This command will:
0162 1. Start a local Firebird server on `http://localhost:5454`
0163 2. Wait for the page to load
0164 3. Capture a full-page screenshot
0165 4. Save it to `screenshots/screenshot.png`
0166 5. Automatically shut down the server
0167 
0168 Screenshots are saved in a `screenshots/` directory with automatic numbering to prevent overwrites.
0169 
0170 ### Command Options
0171 
0172 The screenshot command accepts several options to customize its behavior:
0173 
0174 | Option                     | Type     | Default                 | Description                                                                                                   |
0175 |----------------------------|----------|-------------------------|---------------------------------------------------------------------------------------------------------------|
0176 | `--url TEXT`               | String   | `http://localhost:5454` | URL to take the screenshot of. Use this if you want to screenshot a specific page or event.                    |
0177 | `--output-path TEXT`       | String   | `screenshot.png`        | Base filename for the screenshot. Will be saved in `screenshots/` directory with auto-numbering if file exists. |
0178 | `--work-path TEXT`         | String   | Current directory       | Set the base directory path for file downloads. Files in Firebird will be loaded relative to this path.        |
0179 | `--unsecure-files`         | Boolean  | `False`                 | Allow unrestricted file downloads. Use with caution - see security notes in the serve section.                 |
0180 | `--allow-cors`             | Boolean  | `False`                 | Enable CORS for downloaded files.                                                                              |
0181 | `--disable-download`       | Boolean  | `False`                 | Disable all file downloads from the server.                                                                    |
0182 
0183 ### Examples
0184 
0185 #### 1. Basic Screenshot with Custom Output Name
0186 
0187 ```bash
0188 pyrobird screenshot --output-path event_001.png
0189 ```
0190 
0191 This saves the screenshot as `screenshots/event_001.png` (or `screenshots/event_001_001.png` if the file already exists).
0192 
0193 #### 2. Screenshot with Custom Data Directory
0194 
0195 ```bash
0196 pyrobird screenshot --work-path=/path/to/data --output-path detector_view.png
0197 ```
0198 
0199 This allows Firebird to access files in `/path/to/data` when loading event data.
0200 
0201 #### 3. Screenshot a Specific Event
0202 
0203 First, ensure your data file is accessible, then use a URL that opens a specific event:
0204 
0205 ```bash
0206 pyrobird screenshot --work-path=/path/to/data \
0207                     --url "http://localhost:5454/#/event?file=local://mydata.root&event=5" \
0208                     --output-path event_5.png
0209 ```
0210 
0211 #### 4. Batch Processing Multiple Events
0212 
0213 You can create a simple shell script to process multiple events:
0214 
0215 ```bash
0216 #!/bin/bash
0217 # batch_screenshots.sh
0218 
0219 DATA_PATH="/path/to/data"
0220 DATA_FILE="myevents.root"
0221 
0222 for event in {0..9}; do
0223     pyrobird screenshot \
0224         --work-path="$DATA_PATH" \
0225         --url "http://localhost:5454/#/event?file=local://$DATA_FILE&event=$event" \
0226         --output-path "event_${event}.png"
0227 
0228     echo "Captured screenshot for event $event"
0229 done
0230 ```
0231 
0232 Make the script executable and run it:
0233 
0234 ```bash
0235 chmod +x batch_screenshots.sh
0236 ./batch_screenshots.sh
0237 ```
0238 
0239 #### 5. Screenshot with Different Server Configurations
0240 
0241 If you need to allow access to files outside the working directory:
0242 
0243 ```bash
0244 pyrobird screenshot --unsecure-files --output-path system_files.png
0245 ```
0246 
0247 > **Warning**: Use `--unsecure-files` only on trusted personal machines. Never use in production or shared environments.
0248 
0249 ### Screenshot Configuration
0250 
0251 The screenshot functionality uses the following default settings:
0252 
0253 - **Viewport Size**: 1920x1080 pixels (Full HD)
0254 - **Page Mode**: Full page screenshot (captures entire scrollable content)
0255 - **Wait Strategy**: Waits for DOM content loaded (up to 10 seconds)
0256 - **Additional Wait**: 2 seconds after page load to ensure rendering completes (in normal cases). If both page load and selector detection fail, a fallback mechanism applies: the code waits for 3 seconds, then an additional 2 seconds, totaling up to 5 seconds before capturing the screenshot.
0257 - **Browser**: Headless Chromium
0258 
0259 These settings are optimized for high-quality event display captures but are currently hardcoded in the implementation.
0260 
0261 ### Output Directory Structure
0262 
0263 Screenshots are automatically organized in a `screenshots/` directory with smart numbering:
0264 
0265 ```
0266 screenshots/
0267 ├── screenshot.png          # First screenshot
0268 ├── event_001.png           # First screenshot with custom name
0269 ├── event_001_001.png       # Second screenshot with same name (auto-numbered)
0270 ├── event_001_002.png       # Third screenshot with same name
0271 └── detector_view.png       # Another screenshot
0272 ```
0273 
0274 The auto-numbering system:
0275 - If `filename.png` doesn't exist, it's created as-is
0276 - If it exists, the next available number is found (e.g., `filename_001.png`)
0277 - Numbering continues sequentially even if there are gaps
0278 
0279 ### Troubleshooting
0280 
0281 #### Playwright Not Installed
0282 
0283 If you see an error about Playwright not being installed:
0284 
0285 ```bash
0286 pip install playwright
0287 python -m playwright install chromium
0288 ```
0289 
0290 #### Server Won't Start
0291 
0292 If the Flask server fails to start, check if port 5454 is already in use:
0293 
0294 ```bash
0295 # On Linux/Mac
0296 lsof -i :5454
0297 
0298 # Kill the process if needed
0299 kill -9 <PID>
0300 ```
0301 
0302 #### Screenshots Are Black or Incomplete
0303 
0304 If screenshots appear black or don't show the expected content:
0305 
0306 1. The page might need more time to render. The command waits 2 seconds after page load, but complex visualizations might need longer. If the page fails to load, the command waits an additional 3 seconds (totaling 5 seconds) before taking the screenshot.
0307 2. Try taking a screenshot manually first to verify the URL works correctly.
0308 3. Check that your data files are accessible from the `--work-path` directory.
0309 
0310 #### Permission Errors
0311 
0312 If you get permission errors when accessing files:
0313 
0314 1. Make sure the `--work-path` includes all necessary data files
0315 2. Check file permissions on your data files
0316 3. Consider using `--unsecure-files` for testing (on personal machines only)
0317 
0318 ### Advanced: Programmatic Usage
0319 
0320 You can also use the screenshot functionality programmatically in Python:
0321 
0322 ```python
0323 from pyrobird.cli.screenshot import capture_screenshot, get_screenshot_path
0324 
0325 # Generate unique output path
0326 output_path = get_screenshot_path("my_event.png")
0327 
0328 # Capture screenshot (requires server to be running)
0329 capture_screenshot("http://localhost:5454/#/event?file=local://data.root&event=5", output_path)
0330 
0331 print(f"Screenshot saved to {output_path}")
0332 ```
0333 
0334 > **Note**: When using programmatically, you need to manage the Flask server lifecycle yourself.
0335 
0336 ### Tips for Best Results
0337 
0338 1. **Consistent Naming**: Use descriptive names that include event numbers or identifiers
0339 2. **Batch Processing**: Write scripts to automate screenshot capture for multiple events
0340 3. **Resolution**: The default 1920x1080 viewport provides good quality for most purposes
0341 4. **Data Organization**: Keep your data files organized and use `--work-path` to point to the correct directory
0342 5. **Preview First**: Manually check one or two events in the browser before running batch operations
0343 
0344 
0345 ## API Documentation
0346 
0347 This is technical explanation of what is under the hood of the server part
0348 
0349 ## Features
0350 
0351 - **Secure File Downloading**: Download files with access control to prevent unauthorized access.
0352 - **EDM4eic Event Processing**: Extract and convert specific events from EDM4eic files to JSON.
0353 - **Static File Serving**: Serve frontend assets seamlessly alongside API endpoints.
0354 - **Dynamic Configuration**: Serve configuration files with real-time server information.
0355 - **CORS Support**: Enable Cross-Origin Resource Sharing for specified routes.
0356 
0357 ### Configuration Options
0358 - **DOWNLOAD_PATH**: `str[getcwd()]`, Specifies the directory from which files can be downloaded when using relative paths.
0359 - **PYROBIRD_DOWNLOAD_IS_DISABLED**: `bool[False]` If set to `True`, all download functionalities are disabled.
0360 - **PYROBIRD_DOWNLOAD_IS_UNRESTRICTED**: `bool[False]`, allows unrestricted access to download any file, including sensitive ones.
0361 - **CORS_IS_ALLOWED**: `bool[False]`, If set to `True`, enables Cross-Origin Resource Sharing (CORS) for download routes.
0362 
0363 
0364 
0365 
0366 The API provides endpoints for downloading files, processing EDM4eic events, and serving configuration files. It also includes static file serving for frontend assets.
0367 
0368 ---
0369 
0370 ### Download File
0371 
0372 #### **Endpoint**
0373 
0374 ```
0375 GET /api/v1/download
0376 GET /api/v1/download/<path:filename>
0377 ```
0378 
0379 #### **Description**
0380 
0381 Allows users to download specified files. The download can be restricted based on configuration settings to prevent unauthorized access to sensitive files.
0382 
0383 #### **Parameters**
0384 
0385 - **Query Parameters**:
0386   - `filename` (optional): The name or path of the file to download.
0387   - `f` (optional): An alternative parameter for the filename.
0388 
0389 - **Path Parameters**:
0390   - `filename` (optional): The path of the file to download.
0391 
0392 **Note**: You can provide the filename either as a query parameter or as part of the URL path.
0393 
0394 #### **Usage**
0395 
0396 1. **Download via Query Parameter**
0397 
0398    ```bash
0399    curl -O "http://localhost:5454/api/v1/download?filename=example.txt"
0400    ```
0401 
0402 2. **Download via URL Path**
0403 
0404    ```bash
0405    curl -O "http://localhost:5454/api/v1/download/example.txt"
0406    ```
0407 
0408 #### **Security Considerations**
0409 
0410 - **Access Control**: Ensure that `DOWNLOAD_ALLOW_UNRESTRICTED` is set appropriately to prevent unauthorized access.
0411 - **Path Traversal**: The server sanitizes file paths to prevent directory traversal attacks.
0412 
0413 ---
0414 
0415 ### Open EDM4eic Event
0416 
0417 #### **Endpoint**
0418 
0419 ```
0420 GET /api/v1/convert/edm4eic/<int:event_number>
0421 GET /api/v1/convert/edm4eic/<int:event_number>/<path:filename>
0422 ```
0423 
0424 #### **Description**
0425 
0426 Processes an EDM4eic file to extract a specific event and returns the event data in JSON format. Supports both local and remote files.
0427 
0428 #### **Parameters**
0429 
0430 - **Path Parameters**:
0431   - `event_number` (required): The number of the event to extract.
0432   - `filename` (optional): The path or URL of the EDM4eic file.
0433 
0434 - **Query Parameters**:
0435   - `filename` (optional): The name or path of the file to process.
0436   - `f` (optional): An alternative parameter for the filename.
0437 
0438 **Note**: You can provide the filename either as a query parameter or as part of the URL path.
0439 
0440 #### **Usage**
0441 
0442 1. **Process Local File via Query Parameter**
0443 
0444    ```bash
0445    curl "http://localhost:5454/api/v1/convert/edm4eic/5?filename=path/to/file.edm4eic.root"
0446    ```
0447 
0448 2. **Process Remote File via URL Path**
0449 
0450    ```bash
0451    curl "http://localhost:5454/api/v1/convert/edm4eic/5/http://example.com/data/file.edm4eic.root"
0452    ```
0453 
0454 ### Asset Configuration
0455 
0456 #### **Endpoint**
0457 
0458 ```
0459 GET /assets/config.jsonc
0460 ```
0461 
0462 #### **Description**
0463 
0464 Serves the asset configuration file (`config.jsonc`) with additional server information injected dynamically.
0465 
0466 #### **Usage**
0467 
0468 ```bash
0469 curl "http://localhost:5454/assets/config.jsonc"
0470 ```
0471 
0472 ### Publishing
0473 
0474 ```bash
0475 hatch build
0476 hatch publish
0477 
0478 # You will have to setup your pip authentication key
0479 ```