.. _example_csv_table: CSV Table Upload/Download ========================= This example uploads a CSV file as a LabArchives rich-text HTML table and downloads table entries back to CSV. When to Use It -------------- Use it to publish CSV results in notebook pages and retrieve table entries for downstream analysis. Requirements ------------ This example assumes the recommended local interactive profile, ``labapi[dotenv,builtin-auth]``. See :ref:`installation`. It also requires ``beautifulsoup4`` for HTML table parsing during download. Configuration ------------- For local interactive use, create a ``.env`` file in the repository root: .. code-block:: toml API_URL="https://api.labarchives.com" ACCESS_KEYID="your_access_key_id" ACCESS_PWD="your_password" You can also provide the same values through shell environment variables. See :ref:`first_calls` for both options. How It Works ------------ ``examples/csv_table/csv_table.py`` defines typed upload/download options, converts CSV rows to HTML table markup, and parses table entries back to CSV. Upload Flow ~~~~~~~~~~~ - Read CSV data from disk. - Convert it to HTML table markup. - Upload the HTML as a rich-text entry. Download Flow ~~~~~~~~~~~~~ - Find a text entry containing an HTML table. - Parse the table back into structured data. - Write the result to a CSV file. Common Commands --------------- Set up the example project: .. code-block:: bash cd examples/csv_table uv sync Upload the checked-in sample CSV file: .. code-block:: bash uv run python csv_table.py upload sample_data.csv "Experiments/Results" --notebook "My Notebook" Upload a CSV file without treating the first row as a header: .. code-block:: bash uv run python csv_table.py upload sample_data.csv "Experiments/Results" --notebook "My Notebook" --no-header Download the newest text entry on a page that contains an HTML table: .. code-block:: bash uv run python csv_table.py download "Experiments/Results" results.csv --notebook "My Notebook" Download a table from a specific zero-based page entry index: .. code-block:: bash uv run python csv_table.py download "Experiments/Results" results.csv --notebook "My Notebook" --entry-index 2 Example CSV Input ----------------- Given this CSV file (``examples/csv_table/sample_data.csv``): .. code-block:: text Experiment,Temperature,Pressure,Result Trial 1,25.0,101.3,Success Trial 2,30.0,101.3,Success Trial 3,35.0,102.1,Failure The script will generate this HTML table: .. code-block:: html
| Experiment | Temperature | Pressure | Result |
|---|---|---|---|
| Trial 1 | 25.0 | 101.3 | Success |
| Trial 2 | 30.0 | 101.3 | Success |
| Trial 3 | 35.0 | 102.1 | Failure |