Create a Data Context
A Data Context defines the storage location for metadata, such as your configurations for Data Sources, Expectation Suites, Checkpoints, and Data Docs. It also contains your Validation Results and the metrics associated with them, and it provides access to those objects in Python, along with other helper functions.
All scripts that utilize GX Core should start with the creation of a Data Context.
The following are the available Data Context types:
-
File Data Context: A persistent Data Context that stores metadata and configuration information as YAML files within a file system. File Data Contexts allow you to re-use previously configured Expectation Suites, Data Sources, and Checkpoints.
-
Ephemeral Data Context: A temporary Data Context that stores metadata and configuration information in memory. This Data Context will not persist beyond the current Python session. Ephemeral Data Contexts are useful when you don’t have write permissions to a file system or if you are going to engage in data exploration without needing to save your results.
- Quick Start
- File
- Ephemeral
Prerequisites
Request an available Data Context
- Instructions
- Sample code
-
Run the following code to request a Data Context:
Python inputimport great_expectations as gx
context = gx.get_context()If you don't specify parameters with the
get_context()
method, GX checks your project environment and returns the first Data Context using the following criteria:- If a File Data Context is found in the folder hierarchy of your current working directory,
get_context()
will instantiate and return it. - Otherwise,
get_context()
instantiates and returns an Ephemeral Data Context.
- If a File Data Context is found in the folder hierarchy of your current working directory,
-
Optional. Run the following code to verify the type of Data Context you received:
Python inputprint(type(context).__name__)
The name of the Data Context class is displayed.
# Import great_expectations and request a Data Context.
import great_expectations as gx
context = gx.get_context()
# Optional. Check the type of Data Context that was returned.
print(type(context).__name__)
Prerequisites
Create a File Data Context
- Instructions
- Sample code
-
Run the following code to request a File Data Context:
Python inputimport great_expectations as gx
context = gx.get_context(mode="file")When you specify
mode="file"
, theget_context()
method instantiates and returns the first File Data Context it finds in the folder hierarchy of your current working directory.If a File Data Context configuration is not found,
get_context(mode="file")
creates a new File Data Context in your current working directory and then instantiates and returns the newly created File Data Context.Alternatively, you can request a specific File Data Context by providing a folder path with the
project_root_dir
parameter. If a File Data Context exists in the specified folder it will be instantiated and returned. If a File Data Context is not found in the specified folder, a new File Data Context will be created.Python inputcontext = gx.get_context(mode="file", project_root_dir="./new_context_folder")
-
Optional. Run the following code to review the File Data Context configuration:
Python inputprint(context)
The Data Context configuration, formatted as a Python dictionary, is displayed.
# Import great_expectations and request a Data Context.
import great_expectations as gx
context = gx.get_context(mode="file")
# Optional. Request a File Data Context from a specific folder.
context = gx.get_context(mode="file", project_root_dir="./new_context_folder")
# Optional. Review the configuration of the returned File Data Context.
print(context)
Prerequisites
Create an Ephemeral Data Context
- Instructions
- Sample code
-
Run the following code to request an Ephemeral Data Context:
Python inputimport great_expectations as gx
context = gx.get_context(mode="ephemeral")Ephemeral Data Contexts are temporary and
get_context(mode="ephemeral")
always instantiates and returns a new Ephemeral Data Context. -
Optional. Run the following code to review the Ephemeral Data Context configuration:
Python inputprint(context)
The Data Context configuration, formatted as a Python dictionary, is displayed.
# Import great_expectations and request a Data Context.
import great_expectations as gx
context = gx.get_context(mode="ephemeral")
# Optional. Review the configuration of the returned Ephemeral Data Context.
print(context)