pyhasura


Namepyhasura JSON
Version 1.0.36 PyPI version JSON
download
home_pageNone
SummaryA Python library to simplify Hasura, GraphQL and Machine Learning
upload_time2024-06-06 17:19:28
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords graphql hasura ml ai machine learning arrow
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PyHasura

A library for conveniently working with Hasura, GraphQL, File Formats, and some basic Machine Learning.

## Getting Started

### HasuraClient

```python

# 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

```python
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

```python
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
```python
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.

```python
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.
```python
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.
```python
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](https://dbdiagram.io/) then generate Hasura metadata.

[Click here for an example](https://dbdiagram.io/e/663bac189e85a46d55569b7f/6648f38ef84ecd1d2288c5ed)

```python
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).
```python
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.

```python
# 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).

```python
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"))
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pyhasura",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "graphql, hasura, ml, ai, machine learning, arrow",
    "author": null,
    "author_email": "Kenneth Stott <ken@kenstott.com>",
    "download_url": "https://files.pythonhosted.org/packages/5c/c2/531df13234e92a2e48f277af28227ed18cc99bb08af13d8d0ad015bf1d4a/pyhasura-1.0.36.tar.gz",
    "platform": null,
    "description": "# PyHasura\n\nA library for conveniently working with Hasura, GraphQL, File Formats, and some basic Machine Learning.\n\n## Getting Started\n\n### HasuraClient\n\n```python\n\n# Create Hasura Client\nimport os\nfrom dotenv import load_dotenv\nfrom pyhasura import gql_client, HasuraClient, ExportFormat\nfrom pprint import pprint\n\nload_dotenv()  # Load environment variables from .env\n\nhasura_client = HasuraClient(uri=os.environ.get(\"HASURA_URI\"), admin_secret=os.environ.get(\"HASURA_ADMIN_SECRET\"))\n```\n\n### Query for a Result\n\n```python\nresult = hasura_client.execute(\"\"\"\n        query findCarts {\n            carts {\n                is_complete\n                cart_items {\n                    quantity\n                    product {\n                        price\n                    }\n                }\n            }\n            cart_items {\n                id\n            }\n        }\n    \"\"\")\n\npprint(result)\n```\n\n### Convert Results to a Dictionary of Alternate Formats\n\n```python\nresult = hasura_client.convert_output_format(ExportFormat.ARROW)\npprint(result)\nresult = hasura_client.convert_output_format(ExportFormat.CSV)\npprint(result)\nresult = hasura_client.convert_output_format(ExportFormat.PARQUET)\npprint(result)\nresult = hasura_client.convert_output_format(ExportFormat.DATAFRAME)\npprint(result)\nresult = hasura_client.convert_output_format(ExportFormat.FLAT)\npprint(result)\n```\n\n### Write Results, one file for each root entry in the query\n```python\nresult = hasura_client.write_to_file(output_format=ExportFormat.ARROW)\npprint(result)\nresult = hasura_client.write_to_file(output_format=ExportFormat.CSV)\npprint(result)\nresult = hasura_client.write_to_file(output_format=ExportFormat.PARQUET)\npprint(result)\nresult = hasura_client.write_to_file(output_format=ExportFormat.FLAT)\npprint(result)\nresult = hasura_client.write_to_file(output_format=ExportFormat.NATURAL)\npprint(result)\n```\n\n### Detect Anomalies\n\nUses Doc2Vec to facilitate deeper semantic analysis, but also works fine with categorical string fields.\n\n```python\nresult = hasura_client.anomalies()\npprint(result)\nresult = hasura_client.anomalies(threshold=.03)\npprint(result)\n```\n\n### Train and Serialize then Re-Use for Anomaly Detection\n\nTypically, do this to train on some historical dataset and then\nsearch for anomalies in an alternate (maybe current) dataset.\n```python\nresult = hasura_client.anomalies_training()\npprint(result)\nresult = hasura_client.anomalies(training_files=result, threshold=0)\npprint(result)\n```\n\n### Clustering\n\nUses KMedoids clustering. You are always working on a dictionary of datasets.\nYou need to define the number of clusters for each dataset in a corresponding input dictionary.\nYou can auto-generate the optimal number of clusters and use that as the input.\n```python\nresult = hasura_client.optimal_number_of_clusters(1,8)\npprint(result)\nresult = hasura_client.clusters(result)\npprint(result)\n```\n\n### Model First Design using DBML\n\nBuild models using [DB Diagram](https://dbdiagram.io/) then generate Hasura metadata.\n\n[Click here for an example](https://dbdiagram.io/e/663bac189e85a46d55569b7f/6648f38ef84ecd1d2288c5ed)\n\n```python\nmetadata = hasura_client.add_dbml_model_as_source(\n    'global-retail-sales.dbml',\n    kind='postgres',\n    configuration=configuration,\n    output_file='new-metadata.json'\n)\n```\n\n### Auto-Generated/Discovery of Relationships\n\nWire up as many data sources as you want to analyze to a Hasura instance\nand automatically generate relationships (across data sources).\n```python\nold_metadata = hasura_client.get_metadata()\n\n# generate relationships\nnew_metadata = hasura_client.relationship_analysis('new-metadata.json', entity_synonyms={\"Stores\": [\"warehouse\"]})\n\n# update hasura with new relationships\nhasura_client.replace_metadata(metadata=new_metadata)\n\n```\n\n### Upload a folder of CSVs to PostgreSQL\n\nCreate a datasource from a schema from PostgreSQL.\nPoint a folder of CSVs to same PostgreSQL instance and schema.\nThen automatically track them in Hasura.\n\n```python\n# upload data to database\ntables = hasura_client.upload_csv_folder('retailer', uri=_uri, casing=Casing.camel)\n\n# track all the tables we uploaded\nresult = hasura_client.track_pg_tables(tables, schema=\"public\")\n```\n### Convert SDL into nodes and relationships\n\nTake a Hasura graphql endpoint and converts the metadata it into nodes \nand edges for graph analysis (e.g. finding the optimal path between 2 types).\n\n```python\nnodes, relationships = hasura_client.get_schema_relationships()\npp(nodes)\npp(relationships)\n\nhasura_client.metadata_to_neo4j(\n    os.environ.get(\"NEO4J_URI\"),\n    os.environ.get(\"NEO4J_USERNAME\"),\n    os.environ.get(\"NEO4J_PASSWORD\"))\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A Python library to simplify Hasura, GraphQL and Machine Learning",
    "version": "1.0.36",
    "project_urls": {
        "repository": "https://github.com/kenstott/pyhasura"
    },
    "split_keywords": [
        "graphql",
        " hasura",
        " ml",
        " ai",
        " machine learning",
        " arrow"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b88b649658b4691858efc2d30cdfc7cefa5dddb2481ed2fdfc27a1ffe33d2cf2",
                "md5": "1b324cdff6a0a6a0885bdc2cb0b8422e",
                "sha256": "465aa2cf17915c29ddf53d0b18001dd288bfabcc92db6eb3e81a5a3c862ae5db"
            },
            "downloads": -1,
            "filename": "pyhasura-1.0.36-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1b324cdff6a0a6a0885bdc2cb0b8422e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 30608,
            "upload_time": "2024-06-06T17:19:27",
            "upload_time_iso_8601": "2024-06-06T17:19:27.124176Z",
            "url": "https://files.pythonhosted.org/packages/b8/8b/649658b4691858efc2d30cdfc7cefa5dddb2481ed2fdfc27a1ffe33d2cf2/pyhasura-1.0.36-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5cc2531df13234e92a2e48f277af28227ed18cc99bb08af13d8d0ad015bf1d4a",
                "md5": "d13d3ad296e37005790311cd193b2deb",
                "sha256": "7eb11598e3578578f26c0a770dbcd73f3b7c9d24c0f2986152ae21974eb05023"
            },
            "downloads": -1,
            "filename": "pyhasura-1.0.36.tar.gz",
            "has_sig": false,
            "md5_digest": "d13d3ad296e37005790311cd193b2deb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 27323,
            "upload_time": "2024-06-06T17:19:28",
            "upload_time_iso_8601": "2024-06-06T17:19:28.666121Z",
            "url": "https://files.pythonhosted.org/packages/5c/c2/531df13234e92a2e48f277af28227ed18cc99bb08af13d8d0ad015bf1d4a/pyhasura-1.0.36.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-06 17:19:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "kenstott",
    "github_project": "pyhasura",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "pyhasura"
}
        
Elapsed time: 1.06402s