
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/16_trame_threshold_viewer.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_16_trame_threshold_viewer.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_16_trame_threshold_viewer.py:

Trame viewer example with two startup patterns.

This example shows both ways to start the same Trame app:

1. **File pattern**: point to a single ``.pbm`` file.
2. **Hive pattern**: point to a hive-style directory and use the in-app
   selector to drill down by levels and ``pbm.name``.

Use ``DEMO_SOURCE_KIND`` to switch between these patterns in one place.

This example is gallery-safe: it stays in Sphinx-Gallery, but skips runtime
app setup/launch while docs are being built.

.. GENERATED FROM PYTHON SOURCE LINES 14-79







.. code-block:: Python


    import logging
    import tempfile
    from pathlib import Path

    import pyvista as pv

    from parq_blockmodel import ParquetBlockModel
    from parq_blockmodel.visualization import BlockModelTrameApp

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.WARNING)

    # sphinx_gallery_thumbnail_path = "../docs/_static/branding/parq-blockmodel-gallery-thumbnail.svg"
    DEMO_SOURCE_KIND = "file"  # "file" or "hive"


    def _seed_temporary_hive_demo() -> Path:
        hive_root = Path(tempfile.gettempdir()) / "parq_blockmodel_trame_hive_demo"
        asset_specs = [
            ("site=alpha", "scenario=base", "alpha_base_orebody.parquet", (4, 4, 4)),
            ("site=beta", "scenario=optimised", "beta_optimised_orebody.parquet", (4, 4, 4)),
        ]
        for site_level, scenario_level, filename, shape in asset_specs:
            parquet_path = hive_root / site_level / scenario_level / filename
            parquet_path.parent.mkdir(parents=True, exist_ok=True)
            ParquetBlockModel.create_toy_blockmodel(filename=parquet_path, shape=shape)
        return hive_root


    def _resolve_source_path() -> Path:
        example_dir = Path(__file__).resolve().parent
        local_file = example_dir / "example_blocks_constructor.pbm"
        if DEMO_SOURCE_KIND == "hive":
            return _seed_temporary_hive_demo()

        return local_file

    def main() -> None:
        source_path = _resolve_source_path()
        logger.warning("Launching Trame demo from %s", source_path)
        app_kwargs = {
            "app_name": "Demo App",
        }
        if source_path.is_dir():
            app_kwargs["data_filter_1_attribute"] = "depth"
            app_kwargs["data_filter_1_min"] = 1.25
            app_kwargs["scalar"] = "depth"
            app_kwargs["threshold_value"] = 2.1
            app = BlockModelTrameApp.from_hive_directory(source_path, **app_kwargs)
        else:
            app_kwargs["data_filter_1_attribute"] = "density"
            app_kwargs["data_filter_1_min"] = 2.4
            app_kwargs["scalar"] = "density"
            app_kwargs["threshold_value"] = 2.6
            app = BlockModelTrameApp.from_pbm_file(source_path, **app_kwargs)
        if getattr(pv, "BUILDING_GALLERY", False):
            logger.debug("Skipping live Trame launch while building the gallery.")
            return

        app.launch(port=8080, host="0.0.0.0")


    if __name__ == "__main__" and not getattr(pv, "BUILDING_GALLERY", False):
        main()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.002 seconds)


.. _sphx_glr_download_auto_examples_16_trame_threshold_viewer.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 16_trame_threshold_viewer.ipynb <16_trame_threshold_viewer.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 16_trame_threshold_viewer.py <16_trame_threshold_viewer.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 16_trame_threshold_viewer.zip <16_trame_threshold_viewer.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
