PyHasura
A library for conveniently working with Hasura, GraphQL, File Formats, and some basic Machine Learning.
Getting Started
HasuraClient
# Create Hasura Client
import os
from dotenv import load_dotenv
from pyhasura import gql_client, HasuraClient, ExportFormat
from pprint import pprint
load_dotenv() # Load environment variables from .env
hasura_client = HasuraClient(uri=os.environ.get("HASURA_URI"), admin_secret=os.environ.get("HASURA_ADMIN_SECRET"))
Query for a Result
result = hasura_client.execute("""
query findCarts {
carts {
is_complete
cart_items {
quantity
product {
price
}
}
}
cart_items {
id
}
}
""")
pprint(result)
Convert Results to a Dictionary of Alternate Formats
result = hasura_client.convert_output_format(ExportFormat.ARROW)
pprint(result)
result = hasura_client.convert_output_format(ExportFormat.CSV)
pprint(result)
result = hasura_client.convert_output_format(ExportFormat.PARQUET)
pprint(result)
result = hasura_client.convert_output_format(ExportFormat.DATAFRAME)
pprint(result)
result = hasura_client.convert_output_format(ExportFormat.FLAT)
pprint(result)
Write Results, one file for each root entry in the query
result = hasura_client.write_to_file(output_format=ExportFormat.ARROW)
pprint(result)
result = hasura_client.write_to_file(output_format=ExportFormat.CSV)
pprint(result)
result = hasura_client.write_to_file(output_format=ExportFormat.PARQUET)
pprint(result)
result = hasura_client.write_to_file(output_format=ExportFormat.FLAT)
pprint(result)
result = hasura_client.write_to_file(output_format=ExportFormat.NATURAL)
pprint(result)
Detect Anomalies
Uses Doc2Vec to facilitate deeper semantic analysis, but also works fine with categorical string fields.
result = hasura_client.anomalies()
pprint(result)
result = hasura_client.anomalies(threshold=.03)
pprint(result)
Train and Serialize then Re-Use for Anomaly Detection
Typically, do this to train on some historical dataset and then search for anomalies in an alternate (maybe current) dataset.
result = hasura_client.anomalies_training()
pprint(result)
result = hasura_client.anomalies(training_files=result, threshold=0)
pprint(result)
Clustering
Uses KMedoids clustering. You are always working on a dictionary of datasets. You need to define the number of clusters for each dataset in a corresponding input dictionary. You can auto-generate the optimal number of clusters and use that as the input.
result = hasura_client.optimal_number_of_clusters(1,8)
pprint(result)
result = hasura_client.clusters(result)
pprint(result)
Model First Design using DBML
Build models using DB Diagram then generate Hasura metadata.
metadata = hasura_client.add_dbml_model_as_source(
'global-retail-sales.dbml',
kind='postgres',
configuration=configuration,
output_file='new-metadata.json'
)
Auto-Generated/Discovery of Relationships
Wire up as many data sources as you want to analyze to a Hasura instance and automatically generate relationships (across data sources).
old_metadata = hasura_client.get_metadata()
# generate relationships
new_metadata = hasura_client.relationship_analysis('new-metadata.json', entity_synonyms={"Stores": ["warehouse"]})
# update hasura with new relationships
hasura_client.replace_metadata(metadata=new_metadata)
Upload a folder of CSVs to PostgreSQL
Create a datasource from a schema from PostgreSQL. Point a folder of CSVs to same PostgreSQL instance and schema. Then automatically track them in Hasura.
# upload data to database
tables = hasura_client.upload_csv_folder('retailer', uri=_uri, casing=Casing.camel)
# track all the tables we uploaded
result = hasura_client.track_pg_tables(tables, schema="public")
Convert SDL into nodes and relationships
Take a Hasura graphql endpoint and converts the metadata it into nodes and edges for graph analysis (e.g. finding the optimal path between 2 types).
nodes, relationships = hasura_client.get_schema_relationships()
pp(nodes)
pp(relationships)
hasura_client.metadata_to_neo4j(
os.environ.get("NEO4J_URI"),
os.environ.get("NEO4J_USERNAME"),
os.environ.get("NEO4J_PASSWORD"))
Metadata
Release files for pyhasura 1.0.36
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyhasura-1.0.36.tar.gz | 27.3 kB | Without provenance | Details for pyhasura-1.0.36.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyhasura-1.0.36-py3-none-any.whl | Python 3 | none | any | Details for pyhasura-1.0.36-py3-none-any.whl |
Total release size: 57.9 kB
Release files / pyhasura-1.0.36.tar.gz
| Download URL | pyhasura-1.0.36.tar.gz |
|---|---|
| Size | 27.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7eb11598e3578578f26c0a770dbcd73f3b7c9d24c0f2986152ae21974eb05023
|
|
BLAKE2b-256 checksum How to use checksums |
5cc2531df13234e92a2e48f277af28227ed18cc99bb08af13d8d0ad015bf1d4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.6
|
Release files / pyhasura-1.0.36-py3-none-any.whl
| Download URL | pyhasura-1.0.36-py3-none-any.whl |
|---|---|
| Size | 30.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
465aa2cf17915c29ddf53d0b18001dd288bfabcc92db6eb3e81a5a3c862ae5db
|
|
BLAKE2b-256 checksum How to use checksums |
b88b649658b4691858efc2d30cdfc7cefa5dddb2481ed2fdfc27a1ffe33d2cf2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.6
|
Security for pyhasura 1.0.36
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.