-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vdk-ipython: add %%vdkingest magic function
This commit introduces a new IPython magic function, `vdkingest`, which enables data ingestion through TOML configuration within a Jupyter notebook. This enhancement aims to simplify and streamline the data ingestion process by allowing users to declare their data flows in a structured format (TOML), thereby making it more accessible and maintainable. This is an optional feature of the plugin. If `vdk-data-sources` is not installed, an ImportError is raised with an informative message.
- Loading branch information
1 parent
745b14d
commit 1ba3ad8
Showing
8 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ ipyaggrid | |
# testing dependecies | ||
pytest | ||
vdk-core | ||
vdk-data-sources | ||
vdk-sqlite | ||
vdk-test-utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
projects/vdk-plugins/vdk-ipython/src/vdk/plugin/ipython/ingest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2021-2023 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import logging | ||
import os | ||
import warnings | ||
|
||
from IPython import get_ipython | ||
from vdk.api.job_input import IJobInput | ||
from vdk.plugin.ipython import job | ||
from vdk.plugin.ipython.common import show_ipython_error | ||
from vdk.plugin.ipython.job import JobControl | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def vdkingest(line, cell): | ||
""" | ||
TOML based ingestion configuration | ||
""" | ||
|
||
vdk: JobControl = get_ipython().user_global_ns.get("VDK", None) | ||
if not vdk: | ||
log.warning( | ||
"VDK is not initialized with '%reload_VDK'. " | ||
"Will auto-initialize now wth default parameters." | ||
) | ||
job.load_job() | ||
vdk = get_ipython().user_global_ns.get("VDK", None) | ||
if not vdk: | ||
message = "VDK cannot initialized. Please execute: %reload_VDK" | ||
show_ipython_error(message) | ||
return None | ||
|
||
job_input: IJobInput = vdk.get_initialized_job_input() | ||
|
||
try: | ||
from vdk.plugin.data_sources.mapping.data_flow import DataFlowInput | ||
except ImportError: | ||
raise ImportError( | ||
"vdk-data-sources is not installed. %%vdkingest is not available without it" | ||
) | ||
|
||
from vdk.plugin.data_sources.mapping import toml_parser | ||
import toml | ||
|
||
parsed_toml = toml.loads(cell) | ||
definitions = toml_parser.definitions_from_dict(parsed_toml) | ||
|
||
with DataFlowInput(job_input) as flow_input: | ||
flow_input.start_flows(definitions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2021-2023 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import logging | ||
import os | ||
from unittest.mock import patch | ||
|
||
import ipyaggrid | ||
|
||
_log = logging.getLogger(__name__) | ||
|
||
|
||
@patch.dict( | ||
os.environ, | ||
{"USE_DEFAULT_CELL_TABLE_OUTPUT": "true"}, | ||
) | ||
def test_ingest_cell(sqlite_ip, capsys): | ||
query = """%%vdkingest | ||
[sources] | ||
s1 = {name = "auto-generated-data"} | ||
[destinations] | ||
d1 = {method = "sqlite"} | ||
[[flows]] | ||
from="s1" | ||
to="d1" | ||
""" | ||
sqlite_ip.get_ipython().run_cell(query) | ||
assert capsys.readouterr().out == "" | ||
|
||
select_query = """ | ||
%%vdksql | ||
SELECT * from stream_0 | ||
""" | ||
capsys.readouterr() # reset buffer | ||
select_output = sqlite_ip.get_ipython().run_cell(select_query).result | ||
assert select_output.values.tolist() == [ | ||
[1, "Stream_0_Name_0", 0], | ||
[2, "Stream_0_Name_1", 0], | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters