Skip to content

More examples

Saving logs generated by Phable's logger to a file

Running the below Python code and observing the generated log file may help with troubleshooting HTTP related issues. Also, the log configuration shown can be modified to address other use cases.

Note: In this example the logs are written to a file called app.log in the same directory as the executed Python script.

import logging

from phable import open_haystack_client

logging.basicConfig(
    filename="app.log",
    encoding="utf-8",
    filemode="a",
    format="{asctime} - {name} - {levelname} - {message}",
    level=logging.DEBUG,
    style="{",
    datefmt="%Y-%m-%d %H:%M:%S",
)

# define these settings specific to your use case
uri = "http://localhost:8080/api/demo"
username = "<username>"
password = "<password>"

with open_haystack_client(uri, username, password) as client:
    client.about()

Async Usage without Context Manager

import asyncio

from phable import HaystackClient


async def main() -> None:
    # define these settings specific to your use case
    uri = "http://localhost:8080/api/demo"
    username = "<username>"
    password = "<password>"

    client = HaystackClient.open(uri=uri, username=username, password=password)

    power_pt_grid = asyncio.to_thread(client.read, "power and point")
    energy_pt_grid = asyncio.to_thread(client.read, "energy and point")
    power_pt_grid, energy_pt_grid = await asyncio.gather(power_pt_grid, energy_pt_grid)

    # remember to close the session with the server
    client.close()

    power_pt_df_meta, power_pt_df = power_pt_grid.to_polars_all()
    energy_pt_df_meta, energy_pt_df = energy_pt_grid.to_polars_all()


if __name__ == "__main__":
    asyncio.run(main())