Skip to content

Great Expectations Core integration guide#

Open in Colab

Great Expectations (GX) Core is an open-source tool for validating, documenting, and monitoring your data. With Neptune, you can:

  • Log GX Core's configurations
  • Log validation results and display them in the Neptune app
  • Upload GX Core's rich HTML reports and interact with them in the Neptune app

GX Core metadata, validation results, and HTML reports visualized in the Neptune app.

See example in Neptune  Code examples 

Before you start#

To see how the integration works without setting up your environment, run the example in Colab .

Quickstart#

To log metadata to Neptune, create a run object in your script. The run objects contain the metadata that you want to track, plus the automatically logged system metrics.

To log metadata to Neptune:

  1. Save your Neptune API token and full project name as environment variables.

    How do I save my credentials as environment variables?

    Set your Neptune API token and full project name to the NEPTUNE_API_TOKEN and NEPTUNE_PROJECT environment variables, respectively.

    export NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM.4kl0jvYh3Kb8...ifQ=="
    
    export NEPTUNE_PROJECT="ml-team/classification"
    
    export NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM.4kl0jvYh3Kb8...ifQ=="
    
    export NEPTUNE_PROJECT="ml-team/classification"
    
    setx NEPTUNE_API_TOKEN "h0dHBzOi8aHR0cHM.4kl0jvYh3Kb8...ifQ=="
    
    setx NEPTUNE_PROJECT "ml-team/classification"
    

    You can also navigate to SettingsEdit the system environment variables and add the variables there.

    %env NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM.4kl0jvYh3Kb8...ifQ=="
    
    %env NEPTUNE_PROJECT="ml-team/classification"
    

    To find your credentials:

    • API token: In the bottom-left corner of the Neptune app, expand your user menu and select Get your API token. If you need the token of a service account, go to the workspace or project settings and enter the Service accounts settings.
    • Project name: Your full project name has the form workspace-name/project-name. You can copy it from the project menu ( Details & privacy).

    If you're working in Google Colab, you can set your credentials with the os and getpass libraries:

    import os
    from getpass import getpass
    os.environ["NEPTUNE_API_TOKEN"] = getpass("Enter your Neptune API token: ")
    os.environ["NEPTUNE_PROJECT"] = "workspace-name/project-name"
    
  2. In your script, import Neptune:

    import neptune
    
  3. Initialize a Neptune run:

    run = neptune.init_run()
    

    You can specify additional run parameters, such as tags or a description. For a full list of options, see the API reference.

  4. Log the GX Core metadata. For example, to log a Data Context configuration under the gx/context/config namespace of a run, use:

    run["gx/context/config"] = context.get_config().to_json_dict()
    

    For details, see Logging examples.

  5. To stop the connection to Neptune and sync all data, call the stop() method:

    run.stop()
    
  6. Run your script.

    To open the run and watch the logging live, click the Neptune link that appears in the console output.

    Example link: https://app.neptune.ai/o/showcase/org/great-expectations/e/GX-5

Use the Neptune app to visualize, compare, and organize your logged metadata. For details, see Experiments.

Logging examples#

You can organize the logged metadata into a folder-like structure with the namespaces and fields of a run object. For details, see Namespaces and fields.

Tip

To view the logging examples in the Neptune app, check the GX metadata dashboard.

Log a Data Context configuration#

To log a Data Context configuration under the gx/context/config namespace of a run, use:

run["gx/context/config"] = context.get_config().to_json_dict()

Log Expectations#

To log and organize your Expectations, use:

# Log the Expectation Suite name to the `gx/expectations/expectations_suite_name` 
# field of a run:
run["gx/expectations/expectations_suite_name"] = suite.name

# Create a numbered folder for each Expectation in the `gx/expectations` namespace:
for idx, expectation in enumerate(suite.to_json_dict()["expectations"]):
    run["gx/expectations"][idx] = expectation

Log validation results#

By saving validation results as a dictionary, you can access them programmatically and use in your CI/CD pipelines:

# Log validation status to the `gx/validations/success` field of a run:
run["gx/validations/success"] = results.describe_dict()["success"]

# Log validation statistics to the `gx/validations/statistics` field of a run: 
run["gx/validations/json"] = results.describe_dict()["validation_results"][0]

# Create a numbered folder for each validation result in the 
# `gx/validations/json/results` namespace:
for idx, result in enumerate(
    results.describe_dict()["validation_results"][0]["expectations"]
):
    run["gx/validations/json/results"][idx] = stringify_unsupported(result)

The validation results contain unsupported values such as lists. To convert all the lists to strings, use the stringify_unsupported() method.

Upload HTML reports#

You can also upload the rich HTML reports to Neptune and then interact with them in the app:

  1. HTML reports are available only for FileDataContext. Start by converting the context to FileDataContext:

    from great_expectations.data_context import EphemeralDataContext
    import os
    
    if isinstance(context, EphemeralDataContext):
        context = context.convert_to_file_context()
    
  2. Fetch the local_site_path of a Data Context:

    local_site_path = os.path.dirname(context.build_data_docs()["local_site"])[7:]
    
  3. To log the HTML Expectations and Validation reports, use:

    run["gx/expectations/reports"].upload_files(
        os.path.join(local_site_path, "expectations")
    )   
    
    run["gx/validations/reports"].upload_files(
        os.path.join(local_site_path, "validations")
    )
    
  4. To view the uploaded reports in the Neptune app, navigate to All metadata, or create a custom dashboard with a File preview widget.