def test_launch_jupyter_notebook_env_set_in_env(mock_subprocess): with mock.patch.dict( os.environ, {"GE_JUPYTER_CMD": "jupyter notebook --test-args"}, clear=True ): toolkit.launch_jupyter_notebook("test_path") mock_subprocess.assert_called_once_with( "jupyter notebook --test-args test_path", shell=True )
def test_launch_jupyter_notebook_env_none(mock_subprocess): try: if os.environ.get("GE_JUPYTER_CMD"): temp = os.environ.great_expectations("GE_JUPYTER_CMD") del os.environ["GE_JUPYTER_CMD"] toolkit.launch_jupyter_notebook("test_path") mock_subprocess.assert_called_once_with(["jupyter", "notebook", "test_path"]) except Exception: if temp: os.environ["GE_JUPYTER_CMD"] = temp raise
def _suite_scaffold(suite: str, directory: str, jupyter: bool) -> None: usage_event = "cli.suite.scaffold" suite_name = suite context = toolkit.load_data_context_with_error_handling(directory) notebook_filename = f"scaffold_{suite_name}.ipynb" notebook_path = _get_notebook_path(context, notebook_filename) if suite_name in context.list_expectation_suite_names(): toolkit.tell_user_suite_exists(suite_name) if os.path.isfile(notebook_path): cli_message( f" - If you wish to adjust your scaffolding, you can open this notebook with jupyter: `{notebook_path}` <red>(Please note that if you run that notebook, you will overwrite your existing suite.)</red>" ) send_usage_message( data_context=context, event=usage_event, api_version="v2", success=False, ) sys.exit(1) datasource = toolkit.select_datasource(context) if datasource is None: send_usage_message( data_context=context, event=usage_event, api_version="v2", success=False, ) sys.exit(1) _suite = context.create_expectation_suite(suite_name) _, _, _, batch_kwargs = get_batch_kwargs(context, datasource_name=datasource.name) renderer = SuiteScaffoldNotebookRenderer(context, _suite, batch_kwargs) renderer.render_to_disk(notebook_path) send_usage_message( data_context=context, event=usage_event, api_version="v2", success=True, ) if jupyter: toolkit.launch_jupyter_notebook(notebook_path) else: cli_message( f"To continue scaffolding this suite, run `jupyter notebook {notebook_path}`" )
def _suite_edit( suite, datasource, directory, jupyter, batch_kwargs, usage_event, suppress_usage_message=False, ): # suppress_usage_message flag is for the situation where _suite_edit is called by _suite_new(). # when called by _suite_new(), the flag will be set to False, otherwise it will default to True batch_kwargs_json = batch_kwargs batch_kwargs = None context = toolkit.load_data_context_with_error_handling(directory) try: suite = toolkit.load_expectation_suite(context, suite, usage_event) citations = suite.get_citations(require_batch_kwargs=True) if batch_kwargs_json: try: batch_kwargs = json.loads(batch_kwargs_json) if datasource: batch_kwargs["datasource"] = datasource _batch = toolkit.load_batch(context, suite, batch_kwargs) except json_parse_exception as je: cli_message( "<red>Please check that your batch_kwargs are valid JSON.\n{}</red>".format( je ) ) if not suppress_usage_message: send_usage_message( data_context=context, event=usage_event, api_version="v2", success=True, ) sys.exit(1) except ge_exceptions.DataContextError: cli_message( "<red>Please check that your batch_kwargs are able to load a batch.</red>" ) if not suppress_usage_message: send_usage_message( data_context=context, event=usage_event, api_version="v2", success=False, ) sys.exit(1) except ValueError as ve: cli_message( "<red>Please check that your batch_kwargs are able to load a batch.\n{}</red>".format( ve ) ) if not suppress_usage_message: send_usage_message( data_context=context, event=usage_event, api_version="v2", success=False, ) sys.exit(1) elif citations: citation = citations[-1] batch_kwargs = citation.get("batch_kwargs") if not batch_kwargs: cli_message( """ A batch of data is required to edit the suite - let's help you to specify it.""" ) additional_batch_kwargs = None try: data_source = toolkit.select_datasource( context, datasource_name=datasource ) except ValueError as ve: cli_message(f"<red>{ve}</red>") send_usage_message( data_context=context, event=usage_event, api_version="v2", success=False, ) sys.exit(1) if not data_source: cli_message("<red>No datasources found in the context.</red>") if not suppress_usage_message: send_usage_message( data_context=context, event=usage_event, api_version="v2", success=False, ) sys.exit(1) if batch_kwargs is None: ( datasource_name, batch_kwargs_generator, data_asset, batch_kwargs, ) = get_batch_kwargs( context, datasource_name=data_source.name, batch_kwargs_generator_name=None, data_asset_name=None, additional_batch_kwargs=additional_batch_kwargs, ) notebook_name = f"edit_{suite.expectation_suite_name}.ipynb" notebook_path = _get_notebook_path(context, notebook_name) SuiteEditNotebookRenderer.from_data_context(context).render_to_disk( suite, notebook_path, batch_kwargs ) if not jupyter: cli_message( f"To continue editing this suite, run <green>jupyter notebook {notebook_path}</green>" ) payload = edit_expectation_suite_usage_statistics( data_context=context, expectation_suite_name=suite.expectation_suite_name ) if not suppress_usage_message: send_usage_message( data_context=context, event=usage_event, event_payload=payload, api_version="v2", success=True, ) if jupyter: toolkit.launch_jupyter_notebook(notebook_path) except Exception as e: send_usage_message( data_context=context, event=usage_event, api_version="v2", success=False, ) raise e