Skip to main content

Data hub for pandapower and pandapipes networks based on MongoDB

Project description

pandapower pandapipes

pandahub pandahub pandahub

pandahub is a data hub for pandapower and pandapipes networks based on MongoDB. It allows you to store pandapower and pandapipes networks as well as timeseries in a MongoDB. pandahub allows you to access the database directly through the PandaHub class, but also provides a REST-API based on FastAPI including authentication (implemented with FastAPI Users).

PandaHub example

Run uv sync and bring up a local mongodb instance with docker compose up db -d.

Save pandapower's mv_oberrhein network to the database:

>>> net = pp.networks.mv_oberrhein()
>>> ph = PandaHub()
>>> project_data = ph.create_project("MV Oberrhein Example")
>>> net_data = ph.write_network_to_db(net, name="mv_oberrhein", net_id=0)

Close a switch in the database:

>>> open_switch_idx = net.switch.query("closed==False").index.to_list()[0]
>>> ph.set_net_value_in_db(net_id=0, element_type="switch", element_index=open_switch_idx,
                           parameter="closed", value=True)

Retrieve the network from the database and confirm the switch is now closed:

>>> net_from_db = ph.get_network(net_id=0)
>>> net.switch.loc[open_switch_idx, "closed"]
np.False_
>>> net_from_db.switch.loc[open_switch_idx, "closed"]
np.True_

REST-api

Run uv sync --extra rest-api to set up your venv with the required dependencies, then build the docker image and bring up a mongodb database + the rest-api:

docker compose build
docker compose up -d

This will run the example app in pandahub/api/main.py with live reload at http://localhost:8002. To connect to an existing database instead, set MONGODB_URL to the connection string through an environment variable or in you .env file.

Swagger UI is available at http://localhost:8002/docs.

MongoClient handling

You can supply a mongodb connection string (or the host as connection string and user/password separately) either as arguments to PandaHub or by setting the following environment variables:

  • MONGODB_URL
  • MONGODB_USER (optional)
  • MONGODB_PASSWORD (optional)

These values are then used as default arguments when creating a PandaHub object.

By default, each instantiation of a PandaHub instance will create a new MongoClient instance. This is fine for interactive use or small scripts. To clean up the mongo client, call ph.close() on the instance once you are done with it.

When running the pandahub API as a web application, provide database credentials through the environment variables and additionally set PANDAHUB_GLOBAL_DB_CLIENT to true. A single, global mongodb client instance per process will then be used for all PandaHub instantiations, as recommended by pymongo.

You can use the following properties to access database resources from a PandaHub object:

  • ph.project_db: database of the activated project (e.g. ph.project_db.net_bus.find().to_list())
  • ph.mgmt_db: the user_management database
  • ph.users_collection: the users collection from the management database
  • ph.projects_collection: the projects collection from the management database

In case you want to access the database but do not need a PandaHub object, there are two convenience functions available:

from pandahub.lib import get_mongo_client

my_collection = get_mongo_client("db", "collection")
docs = collection.find().to_list()

PANDAHUB_GLOBAL_DB_CLIENT is respected, you either get the global client or a fresh instance, in which case you are responsible for cleaning up the connection.

from pandahub.lib import mongo_client

with mongo_client("db", "my_collection") as collection:
    docs = collection.find().to_list()

With the context manager, a new MongoClient is created (even if PANDAHUB_GLOBAL_DB_CLIENT is true) and closed on completion of the with block.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pandahub-0.5.15.tar.gz (260.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pandahub-0.5.15-py3-none-any.whl (48.9 kB view details)

Uploaded Python 3

File details

Details for the file pandahub-0.5.15.tar.gz.

File metadata

  • Download URL: pandahub-0.5.15.tar.gz
  • Upload date:
  • Size: 260.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.14 {"installer":{"name":"uv","version":"0.9.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pandahub-0.5.15.tar.gz
Algorithm Hash digest
SHA256 e86bbcc52c25298b73121780467de8e6e8a12dc1c99ec6e89cb8f11994ff67c3
MD5 5830ff4ab475449f6d208ea39483ee64
BLAKE2b-256 f168b02eb4f7b8af1c42ee6e8a31d173a5d636d7392b2da1cc6b6c88f55f2c53

See more details on using hashes here.

File details

Details for the file pandahub-0.5.15-py3-none-any.whl.

File metadata

  • Download URL: pandahub-0.5.15-py3-none-any.whl
  • Upload date:
  • Size: 48.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.14 {"installer":{"name":"uv","version":"0.9.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pandahub-0.5.15-py3-none-any.whl
Algorithm Hash digest
SHA256 ae8c1f1a786695e3bd2f76f0149ab60f6f54ed4e89ac355871d299777f4fcc3f
MD5 f4b1b1a012af958ccc4d43b292920ce7
BLAKE2b-256 2a513b632ec9e92453c3c8b480c6d88be4a31ed24a20f1ab09bd3e6f81b83529

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page