Skip to content

Commit 731ace4

Browse files
committed
added notebook
1 parent f81a90c commit 731ace4

File tree

5 files changed

+175
-287
lines changed

5 files changed

+175
-287
lines changed

.devcontainer/devcontainer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"image": "mcr.microsoft.com/devcontainers/universal:2",
3+
"containerEnv": {
4+
},
5+
"hostRequirements": {
6+
"cpus": 2
7+
},
8+
"waitFor": "onCreateCommand",
9+
"updateContentCommand": "pip install -e ",
10+
"postCreateCommand": "",
11+
"postStartCommand": "git reset --hard && git clean -fd",
12+
"customizations": {
13+
"codespaces": {
14+
"openFiles": [
15+
"notebooks/demo.ipynb"
16+
]
17+
},
18+
"vscode": {
19+
"extensions": [
20+
"ms-toolsai.jupyter",
21+
"ms-python.python"
22+
]
23+
}
24+
}
25+
}

notebooks/demo.ipynb

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# PyStackQL Development Demo\n",
8+
"\n",
9+
"This notebook demonstrates how to use the development version of PyStackQL directly from the source code. Any changes you make to the PyStackQL code will be immediately reflected here."
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"# First, let's check what version of PyStackQL we're using\n",
19+
"import pystackql\n",
20+
"print(f\"PyStackQL Version: {pystackql.__version__}\")\n",
21+
"\n",
22+
"# Check the location of the package to confirm we're using the development version\n",
23+
"print(f\"Package Location: {pystackql.__file__}\")"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"# Load the magic extension\n",
33+
"%load_ext pystackql.magic"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"## Basic Query Test\n",
41+
"\n",
42+
"Let's run a simple query to test that everything is working:"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"metadata": {},
49+
"outputs": [],
50+
"source": [
51+
"%stackql SELECT 42 as answer"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"## CSV Download Test\n",
59+
"\n",
60+
"Let's test the CSV download functionality:"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"%%stackql --csv-download\n",
70+
"SELECT \n",
71+
" 'Python' as language,\n",
72+
" 'Development' as mode,\n",
73+
" 'PyStackQL' as package"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"## Test Cloud Provider Functionality\n",
81+
"\n",
82+
"If you have credentials configured, you can test actual cloud provider queries:"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": null,
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"# Uncomment and run the appropriate provider query based on your available credentials\n",
92+
"\n",
93+
"# AWS Example\n",
94+
"# %stackql DESCRIBE aws.ec2.instances\n",
95+
"\n",
96+
"# GitHub Example\n",
97+
"# %stackql registry pull github\n",
98+
"# %stackql SELECT login FROM github.users.followers WHERE username = 'stackql'"
99+
]
100+
},
101+
{
102+
"cell_type": "markdown",
103+
"metadata": {},
104+
"source": [
105+
"## Development Tips\n",
106+
"\n",
107+
"1. After modifying PyStackQL code, you don't need to reinstall the package - changes are reflected immediately\n",
108+
"2. You can run tests from the terminal with `pytest tests/`\n",
109+
"3. If you modify deep core functionality, you may need to restart the kernel\n",
110+
"4. To debug issues, you can use Python's built-in debugging tools:"
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": null,
116+
"metadata": {},
117+
"outputs": [],
118+
"source": [
119+
"# Example debugging a PyStackQL function\n",
120+
"from pystackql.core import StackQL\n",
121+
"\n",
122+
"# Get instance properties\n",
123+
"stackql = StackQL()\n",
124+
"props = stackql.properties()\n",
125+
"print(props)"
126+
]
127+
}
128+
],
129+
"metadata": {
130+
"kernelspec": {
131+
"display_name": "Python (PyStackQL Dev)",
132+
"language": "python",
133+
"name": "pystackql-dev"
134+
},
135+
"language_info": {
136+
"codemirror_mode": {
137+
"name": "ipython",
138+
"version": 3
139+
},
140+
"file_extension": ".py",
141+
"mimetype": "text/x-python",
142+
"name": "python",
143+
"nbconvert_exporter": "python",
144+
"pygments_lexer": "ipython3",
145+
"version": "3.10.4"
146+
}
147+
},
148+
"nbformat": 4,
149+
"nbformat_minor": 4
150+
}

pystackql/magic_ext/base.py

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,3 @@
1-
# # pystackql/magic_ext/base.py
2-
3-
# """
4-
# Base Jupyter magic extension for PyStackQL.
5-
6-
# This module provides the base class for PyStackQL Jupyter magic extensions.
7-
# """
8-
9-
# from __future__ import print_function
10-
# from IPython.core.magic import Magics
11-
# from string import Template
12-
13-
# class BaseStackqlMagic(Magics):
14-
# """Base Jupyter magic extension enabling running StackQL queries.
15-
16-
# This extension allows users to conveniently run StackQL queries against cloud
17-
# or SaaS resources directly from Jupyter notebooks, and visualize the results in a tabular
18-
# format using Pandas DataFrames.
19-
# """
20-
# def __init__(self, shell, server_mode):
21-
# """Initialize the BaseStackqlMagic class.
22-
23-
# :param shell: The IPython shell instance.
24-
# :param server_mode: Whether to use server mode.
25-
# """
26-
# from ..core import StackQL
27-
# super(BaseStackqlMagic, self).__init__(shell)
28-
# self.stackql_instance = StackQL(server_mode=server_mode, output='pandas')
29-
30-
# def get_rendered_query(self, data):
31-
# """Substitute placeholders in a query template with variables from the current namespace.
32-
33-
# :param data: SQL query template containing placeholders.
34-
# :type data: str
35-
# :return: A SQL query with placeholders substituted.
36-
# :rtype: str
37-
# """
38-
# t = Template(data)
39-
# return t.substitute(self.shell.user_ns)
40-
41-
# def run_query(self, query):
42-
# """Execute a StackQL query
43-
44-
# :param query: StackQL query to be executed.
45-
# :type query: str
46-
# :return: Query results, returned as a Pandas DataFrame.
47-
# :rtype: pandas.DataFrame
48-
# """
49-
# # Check if the query starts with "registry pull" (case insensitive)
50-
# if query.strip().lower().startswith("registry pull"):
51-
# return self.stackql_instance.executeStmt(query)
52-
53-
# return self.stackql_instance.execute(query)
54-
551
# pystackql/magic_ext/base.py
562

573
"""

pystackql/magic_ext/local.py

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,3 @@
1-
# # pystackql/magic_ext/local.py
2-
3-
# """
4-
# Local Jupyter magic extension for PyStackQL.
5-
6-
# This module provides a Jupyter magic command for running StackQL queries
7-
# using a local StackQL binary.
8-
# """
9-
10-
# from IPython.core.magic import (magics_class, line_cell_magic)
11-
# from IPython.display import display, HTML
12-
# from .base import BaseStackqlMagic
13-
# import argparse
14-
# import base64
15-
# import io
16-
17-
# @magics_class
18-
# class StackqlMagic(BaseStackqlMagic):
19-
# """Jupyter magic command for running StackQL queries in local mode."""
20-
21-
# def __init__(self, shell):
22-
# """Initialize the StackqlMagic class.
23-
24-
# :param shell: The IPython shell instance.
25-
# """
26-
# super().__init__(shell, server_mode=False)
27-
28-
# @line_cell_magic
29-
# def stackql(self, line, cell=None):
30-
# """A Jupyter magic command to run StackQL queries.
31-
32-
# Can be used as both line and cell magic:
33-
# - As a line magic: `%stackql QUERY`
34-
# - As a cell magic: `%%stackql [OPTIONS]` followed by the QUERY in the next line.
35-
36-
# :param line: The arguments and/or StackQL query when used as line magic.
37-
# :param cell: The StackQL query when used as cell magic.
38-
# :return: StackQL query results as a named Pandas DataFrame (`stackql_df`).
39-
# """
40-
# is_cell_magic = cell is not None
41-
42-
# if is_cell_magic:
43-
# parser = argparse.ArgumentParser()
44-
# parser.add_argument("--no-display", action="store_true", help="Suppress result display.")
45-
# parser.add_argument("--csv-download", action="store_true", help="Add CSV download link to output.")
46-
# args = parser.parse_args(line.split())
47-
# query_to_run = self.get_rendered_query(cell)
48-
# else:
49-
# args = None
50-
# query_to_run = self.get_rendered_query(line)
51-
52-
# results = self.run_query(query_to_run)
53-
# self.shell.user_ns['stackql_df'] = results
54-
55-
# if is_cell_magic and args and args.no_display:
56-
# return None
57-
# elif is_cell_magic and args and args.csv_download and not args.no_display:
58-
# self._display_with_csv_download(results)
59-
# return results
60-
# elif is_cell_magic and args and not args.no_display:
61-
# return results
62-
# elif not is_cell_magic:
63-
# return results
64-
65-
# def _display_with_csv_download(self, df):
66-
# """Display DataFrame with CSV download link.
67-
68-
# :param df: The DataFrame to display and make downloadable.
69-
# """
70-
# import IPython.display
71-
72-
# try:
73-
# # Generate CSV data
74-
# import io
75-
# import base64
76-
# csv_buffer = io.StringIO()
77-
# df.to_csv(csv_buffer, index=False)
78-
# csv_data = csv_buffer.getvalue()
79-
80-
# # Encode to base64 for data URI
81-
# csv_base64 = base64.b64encode(csv_data.encode()).decode()
82-
83-
# # Create download link
84-
# download_link = f'data:text/csv;base64,{csv_base64}'
85-
86-
# # Display the DataFrame first
87-
# IPython.display.display(df)
88-
89-
# # Create and display the download button
90-
# download_html = f'''
91-
# <div style="margin-top: 10px;">
92-
# <a href="{download_link}" download="stackql_results.csv"
93-
# style="display: inline-block; padding: 8px 16px; background-color: #007cba;
94-
# color: white; text-decoration: none; border-radius: 4px;
95-
# font-family: Arial, sans-serif; font-size: 14px; border: none; cursor: pointer;">
96-
# 📥 Download CSV
97-
# </a>
98-
# </div>
99-
# '''
100-
# IPython.display.display(IPython.display.HTML(download_html))
101-
102-
# except Exception as e:
103-
# # If CSV generation fails, just display the DataFrame normally
104-
# IPython.display.display(df)
105-
# print(f"Error generating CSV download: {e}")
106-
107-
# def load_ipython_extension(ipython):
108-
# """Load the non-server magic in IPython.
109-
110-
# This is called when running %load_ext pystackql.magic in a notebook.
111-
# It registers the %stackql and %%stackql magic commands.
112-
113-
# :param ipython: The IPython shell instance
114-
# """
115-
# # Create an instance of the magic class and register it
116-
# magic_instance = StackqlMagic(ipython)
117-
# ipython.register_magics(magic_instance)
118-
119-
1201
# pystackql/magic_ext/local.py
1212

1223
"""

0 commit comments

Comments
 (0)