-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from stackql/feature/refactor
v3.2.0 updates to magic ext
- Loading branch information
Showing
10 changed files
with
168 additions
and
125 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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .stackql import StackQL | ||
from .stackql_magic import load_ipython_extension | ||
from .stackql_magic import StackqlMagic, load_non_server_magic | ||
from .stackql_server_magic import StackqlServerMagic, load_server_magic |
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,73 @@ | ||
from __future__ import print_function | ||
import pandas as pd | ||
import json, argparse | ||
from IPython.core.magic import (Magics, line_cell_magic) | ||
from string import Template | ||
|
||
class BaseStackqlMagic(Magics): | ||
"""Base Jupyter magic extension enabling running StackQL queries. | ||
This extension allows users to conveniently run StackQL queries against cloud | ||
or SaaS reources directly from Jupyter notebooks, and visualize the results in a tabular | ||
format using Pandas DataFrames. | ||
""" | ||
def __init__(self, shell, server_mode): | ||
"""Initialize the StackqlMagic class. | ||
:param shell: The IPython shell instance. | ||
""" | ||
from . import StackQL | ||
super(BaseStackqlMagic, self).__init__(shell) | ||
self.stackql_instance = StackQL(server_mode=server_mode, output='pandas') | ||
|
||
def get_rendered_query(self, data): | ||
"""Substitute placeholders in a query template with variables from the current namespace. | ||
:param data: SQL query template containing placeholders. | ||
:type data: str | ||
:return: A SQL query with placeholders substituted. | ||
:rtype: str | ||
""" | ||
t = Template(data) | ||
return t.substitute(self.shell.user_ns) | ||
|
||
def run_query(self, query): | ||
"""Execute a StackQL query | ||
:param query: StackQL query to be executed. | ||
:type query: str | ||
:return: Query results, returned as a Pandas DataFrame. | ||
:rtype: pandas.DataFrame | ||
""" | ||
return self.stackql_instance.execute(query) | ||
|
||
@line_cell_magic | ||
def stackql(self, line, cell=None): | ||
"""A Jupyter magic command to run StackQL queries. | ||
Can be used as both line and cell magic: | ||
- As a line magic: `%stackql QUERY` | ||
- As a cell magic: `%%stackql [OPTIONS]` followed by the QUERY in the next line. | ||
:param line: The arguments and/or StackQL query when used as line magic. | ||
:param cell: The StackQL query when used as cell magic. | ||
:return: StackQL query results as a named Pandas DataFrame (`stackql_df`). | ||
""" | ||
is_cell_magic = cell is not None | ||
|
||
if is_cell_magic: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--no-display", action="store_true", help="Suppress result display.") | ||
args = parser.parse_args(line.split()) | ||
query_to_run = self.get_rendered_query(cell) | ||
else: | ||
args = None | ||
query_to_run = self.get_rendered_query(line) | ||
|
||
results = self.run_query(query_to_run) | ||
self.shell.user_ns['stackql_df'] = results | ||
|
||
if is_cell_magic and args and not args.no_display: | ||
return results | ||
elif not is_cell_magic: | ||
return results |
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 |
---|---|---|
@@ -1,89 +1,12 @@ | ||
from __future__ import print_function | ||
import pandas as pd | ||
import json, argparse | ||
from IPython.core.magic import (Magics, magics_class, line_cell_magic) | ||
from string import Template | ||
# stackql_magic.py | ||
from IPython.core.magic import magics_class | ||
from .base_stackql_magic import BaseStackqlMagic | ||
|
||
@magics_class | ||
class StackqlMagic(Magics): | ||
""" | ||
A Jupyter magic extension enabling SQL querying against a StackQL database. | ||
This extension allows users to conveniently run SQL queries against the StackQL | ||
database directly from Jupyter notebooks, and visualize the results in a tabular | ||
format using Pandas DataFrames. | ||
""" | ||
|
||
class StackqlMagic(BaseStackqlMagic): | ||
def __init__(self, shell): | ||
""" | ||
Initialize the StackqlMagic class. | ||
:param shell: The IPython shell instance. | ||
""" | ||
from . import StackQL | ||
super(StackqlMagic, self).__init__(shell) | ||
self.stackql_instance = StackQL(server_mode=True, output='pandas') | ||
|
||
def get_rendered_query(self, data): | ||
""" | ||
Substitute placeholders in a query template with variables from the current namespace. | ||
:param data: SQL query template containing placeholders. | ||
:type data: str | ||
:return: A SQL query with placeholders substituted. | ||
:rtype: str | ||
""" | ||
t = Template(data) | ||
return t.substitute(self.shell.user_ns) | ||
|
||
def run_query(self, query): | ||
""" | ||
Execute a StackQL query | ||
:param query: StackQL query to be executed. | ||
:type query: str | ||
:return: Query results, returned as a Pandas DataFrame. | ||
:rtype: pandas.DataFrame | ||
""" | ||
return self.stackql_instance.execute(query) | ||
|
||
@line_cell_magic | ||
def stackql(self, line, cell=None): | ||
""" | ||
A Jupyter magic command to run SQL queries against the StackQL database. | ||
Can be used as both line and cell magic: | ||
- As a line magic: `%stackql QUERY` | ||
- As a cell magic: `%%stackql [OPTIONS]` followed by the QUERY in the next line. | ||
:param line: The arguments and/or StackQL query when used as line magic. | ||
:param cell: The StackQL query when used as cell magic. | ||
:return: StackQL query results as a named Pandas DataFrame (`stackql_df`). | ||
""" | ||
is_cell_magic = cell is not None | ||
|
||
if is_cell_magic: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--no-display", action="store_true", help="Suppress result display.") | ||
args = parser.parse_args(line.split()) | ||
query_to_run = self.get_rendered_query(cell) | ||
else: | ||
args = None | ||
query_to_run = self.get_rendered_query(line) | ||
|
||
results = self.run_query(query_to_run) | ||
self.shell.user_ns['stackql_df'] = results | ||
|
||
if is_cell_magic and args and not args.no_display: | ||
return results | ||
elif not is_cell_magic: | ||
return results | ||
super().__init__(shell, server_mode=False) | ||
|
||
def load_ipython_extension(ipython): | ||
""" | ||
Enable the StackqlMagic extension in IPython. | ||
This function allows the extension to be loaded via the `%load_ext` command or | ||
be automatically loaded by IPython at startup. | ||
""" | ||
def load_non_server_magic(ipython): | ||
"""Load the non-server magic in IPython.""" | ||
ipython.register_magics(StackqlMagic) |
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,13 @@ | ||
# stackql_server_magic.py | ||
from IPython.core.magic import magics_class | ||
from .base_stackql_magic import BaseStackqlMagic | ||
|
||
@magics_class | ||
class StackqlServerMagic(BaseStackqlMagic): | ||
|
||
def __init__(self, shell): | ||
super().__init__(shell, server_mode=True) | ||
|
||
def load_server_magic(ipython): | ||
"""Load the extension in IPython.""" | ||
ipython.register_magics(StackqlServerMagic) |
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
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