You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.rst
+8-11Lines changed: 8 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ PyStackQL - Python Wrapper for StackQL
22
22
StackQL is an open source developer tool which allows you to query and interact with cloud and SaaS provider APIs using SQL grammar.
23
23
StackQL can be used for cloud inventory analysis, cloud cost optimization, cloud security and compliance, provisioning/IaC, assurance, XOps, and more.
24
24
25
-
PyStackQL is a Python wrapper for StackQL which allows you to use StackQL within Python applications and to use the power of Python to extend StackQL.
25
+
`PyStackQL <https://pypi.org/project/pystackql/>`_ is a Python wrapper for StackQL which allows you to use StackQL within Python applications and to use the power of Python to extend StackQL.
26
26
PyStackQL can be used with ``pandas``, ``matplotlib``, ``plotly``, ``jupyter`` and other Python libraries to create powerful data analysis and visualization applications.
27
27
28
28
For detailed documentation, including the API reference, see `Read the Docs <https://pystackql.readthedocs.io>`_.
@@ -52,19 +52,17 @@ The following example demonstrates how to run a query and return the results as
52
52
::
53
53
54
54
from pystackql import StackQL
55
-
import pandas as pd
56
55
region = "ap-southeast-2"
57
-
stackql = StackQL()
56
+
stackql = StackQL(output='pandas')
58
57
59
58
query = """
60
-
SELECT instanceType, COUNT(*) as num_instances
59
+
SELECT instance_type, COUNT(*) as num_instances
61
60
FROM aws.ec2.instances
62
61
WHERE region = '%s'
63
-
GROUP BY instanceType
62
+
GROUP BY instance_type
64
63
""" % (region)
65
64
66
-
res = stackql.execute(query)
67
-
df = pd.read_json(res)
65
+
df = stackql.execute(query)
68
66
print(df)
69
67
70
68
Using PyStackQL with Jupyter Notebook
@@ -76,7 +74,7 @@ To use the integrated Jupyter magic commands provided by PyStackQL:
stackql = StackQL()# output format defaults to 'dict'
163
157
164
158
query ="""
165
159
SELECT name,
@@ -172,8 +166,7 @@ Here is an example of using the ``json_extract`` function to extract a field fro
172
166
"""% (resourceGroupName, subscriptionId)
173
167
174
168
res = stackql.execute(query)
175
-
df = pd.read_json(res)
176
-
print(df)
169
+
print(res)
177
170
178
171
Using the Jupyter Magic Extension
179
172
=================================
@@ -184,7 +177,7 @@ To get started with the magic extension, first load it into your Jupyter environ
184
177
185
178
.. code-block:: ipython
186
179
187
-
%load_ext pystackql
180
+
%load_ext pystackql.magic
188
181
189
182
After loading the magic extension, you can use the `%%stackql` magic to execute StackQL commands in a dedicated Jupyter cell. The output will be displayed directly below the cell, just like any other Jupyter command output.
190
183
@@ -196,3 +189,9 @@ Example:
196
189
SHOW SERVICES in aws
197
190
198
191
This Jupyter magic extension provides a seamless integration of `pystackql` into your Jupyter workflows, allowing you to explore cloud and SaaS provider data interactively within your notebooks.
192
+
193
+
To use the magic extension to run queries against a StackQL server, you can use the following command:
Copy file name to clipboardExpand all lines: docs/source/magic_ext.rst
+51-5Lines changed: 51 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,25 +38,39 @@ The extension provides both line and cell magic functionalities:
38
38
.. code-block:: python
39
39
40
40
%%stackql
41
-
SELECTinstanceType, COUNT(*) as num_instances
41
+
SELECTinstance_type, COUNT(*) as num_instances
42
42
FROM aws.ec2.instances
43
-
WHERE region ='$region'GROUPBYinstanceType
43
+
WHERE region ='$region'GROUPBYinstance_type
44
44
45
45
Options
46
46
-------
47
47
48
48
When using `StackqlMagic` as cell magic, you can pass in the following options:
49
49
50
50
- ``--no-display`` : Suppresses the display of the results. Even when this option is enabled, the results are still saved in the `stackql_df` Pandas DataFrame.
51
+
- ``--csv-download`` : Adds a download button below the query results that allows you to download the data as a CSV file.
51
52
52
-
Example:
53
+
Examples
54
+
--------
55
+
56
+
Basic Query
57
+
~~~~~~~~~~
53
58
54
59
.. code-block:: python
55
60
56
61
project ='stackql-demo'
57
62
zone ='australia-southeast1-a'
58
63
region ='australia-southeast1'
59
64
65
+
%%stackql
66
+
SELECT SPLIT_PART(machineType, '/', -1) as machine_type, count(*) as num_instances
67
+
FROM google.compute.instances
68
+
WHERE project ='$project'AND zone ='$zone'
69
+
GROUPBY machine_type
70
+
71
+
Suppressing Display
72
+
~~~~~~~~~~~~~~~~~~
73
+
60
74
.. code-block:: python
61
75
62
76
%%stackql --no-display
@@ -67,6 +81,38 @@ Example:
67
81
68
82
This will run the query but won't display the results in the notebook. Instead, you can later access the results via the `stackql_df` variable.
69
83
84
+
Downloading Results as CSV
85
+
~~~~~~~~~~~~~~~~~~~~~~~~~
86
+
87
+
.. code-block:: python
88
+
89
+
%%stackql --csv-download
90
+
SELECT
91
+
'Python'as language,
92
+
'Development'as mode,
93
+
'PyStackQL'as package
94
+
95
+
This will display the query results in the notebook and add a download button below the results. Clicking the button will download the data as a CSV file named ``stackql_results.csv``.
96
+
97
+
Combining Options
98
+
~~~~~~~~~~~~~~~
99
+
100
+
You can also combine options. For example, if you want to suppress the display but still want a download button:
The results of the queries are always saved in a Pandas DataFrame named `stackql_df` in the notebook's current namespace. This allows you to further process or visualize the data as needed.
@@ -75,8 +121,8 @@ An example of visualizing the results using Pandas is shown below:
75
121
76
122
.. code-block:: python
77
123
78
-
stackql_df.plot(kind='pie', y='num_instances', labels=_['machine_type'], title='Instances by Type', autopct='%1.1f%%')
124
+
stackql_df.plot(kind='pie', y='num_instances', labels=stackql_df['machine_type'], title='Instances by Type', autopct='%1.1f%%')
79
125
80
126
--------
81
127
82
-
This documentation provides a basic overview and usage guide for the `StackqlMagic` extension. For advanced usage or any additional features provided by the extension, refer to the source code or any other accompanying documentation.
128
+
This documentation provides a basic overview and usage guide for the `StackqlMagic` extension. For advanced usage or any additional features provided by the extension, refer to the source code or any other accompanying documentation.
0 commit comments