Download Kqlmagic from PyPI and install/update (if latest version ims already installed you can skip this step)
#!pip install Kqlmagic --no-cache-dir --upgrade
%reload_ext Kqlmagic
%kql azure-data-explorer://code;cluster='help';database='Samples'
from datetime import datetime, timedelta
my_limit = 10
my_not_state = 'TEXAS'
my_start_datetime = datetime(2007, 8, 29)
my_timespan = timedelta(days=100)
my_dict = {"a":1}
my_list = ["x", "y", "z"]
my_tuple = ("t", 44, my_limit)
my_set = {6,7,8}
%%kql
let _dict_ = my_dict;
let _list_ = my_list;
let _tuple_ = my_tuple;
let _set_ = my_set;
let _start_time_ = my_start_datetime;
let _timespan_ = my_timespan;
let _limit_ = my_limit;
let _not_val_ = my_not_state;
StormEvents
| where StartTime >= _start_time_
| where EndTime <= _start_time_ + _timespan_
| where State != _not_val_
| summarize count() by State
| extend d = _dict_
| extend l = _list_
| extend t = _tuple_
| extend s = _set_
| sort by count_
| limit _limit_
my_df =_kql_raw_result_.to_dataframe()
my_df
%%kql
let _my_table_ = my_df;
_my_table_ | project State, s, t | limit 3
_kql_raw_result_.parametrized_query
sort_col = 'count_'
my_query = """StormEvents
| where State != 'OHIO'
| summarize count() by State
| sort by {0}
| limit 5""".format(sort_col)
%kql -query my_query
p_dict = {'p_limit':20, 'p_not_state':'IOWA'}
%%kql
-params_dict p_dict
let _limit_ = p_limit;
let _not_val_ = p_not_state;
StormEvents
| where State != _not_val_
| summarize count() by State
| sort by count_
| limit _limit_
%%kql
-params_dict {'p_limit':5,'p_not_state':'OHIO'}
let _limit_ = p_limit;
let _not_val_ = p_not_state;
StormEvents
| where State != _not_val_
| summarize count() by State
| sort by count_
| limit _limit_
_kql_raw_result_.query
_kql_raw_result_.parametrized_query
p_dict = {'p_limit':5,'p_not_state':'OHIO'}
%%kql
-params_dict p_dict
let _limit_ = p_limit;
let _not_val_ = p_not_state;
StormEvents
| where State != _not_val_
| summarize count() by State
| sort by count_
| limit _limit_
p_dict = {'p_limit': 5, 'p_not_state': 'IOWA'}
_kql_raw_result_.refresh()
_kql_raw_result_.submit()
all options can be parametrized. instead of providing a quoted parameter value, specify the python variable or python expression
my_var
, str(type(x))
, [a,1,2]
str( type ( x ) )
, [a, 1, 2]
table_package = 'pandas'
my_popup_state = True
%%kql -tp=table_package -pw=my_popup_state -f=table_package!='pandas'
StormEvents
| where State != 'OHIO'
| summarize count() by State
| sort by count_
| limit 5
all commands can be parametrized. instead of providing a quoted parameter value, specify the python variable or python expression.
my_topic = "kql"
%kql --help my_topic
all values in connection string can be parametrized. instead of providing a quoted parameter value, specify the python variable or python expression
note, if you don't specify the credential's secret you will be prompted.
note, if instead of the python expression, you specify a variable that starts with $, it will be retreived from the environment variables.
beware, that python expression must not have spaces !!!
my_appid = "DEMO_APP"
my_appkey = "DEMO_KEY"
%kql appinsights://appid=my_appid;appkey=my_appkey
my_connection_str = """
loganalytics://workspace='DEMO_WORKSPACE';appkey='DEMO_KEY';alias='myworkspace'
"""
%kql -conn=my_connection_str