def _reconnect(**kwargs): if kwargs is None or kwargs.get("notebook_app") != "jupyterlab": display( Javascript( """try {IPython.notebook.kernel.reconnect();} catch(err) {;}""" )) time.sleep(1)
def flush(window_location, **kwargs): if window_location.startswith( "http://localhost") or window_location.startswith( "https://localhost"): parts = window_location.split('/') parts.pop() Help_html.showfiles_base_url = '/'.join(parts) else: if Help_html.notebooks_host: start = Help_html.notebooks_host.find('//') + 2 suffix = '.' + Help_html.notebooks_host[start:] else: suffix = '.notebooks.azure.com' end = window_location.find(suffix) start = window_location.find('//') # azure notebook environment, assume template: https://library-user.libray.notebooks.azure.com if (start > 0 and end > 0): library, user = window_location[start + 2:end].split('-') azure_notebooks_host = Help_html.notebooks_host or 'https://notebooks.azure.com' Help_html.showfiles_base_url = azure_notebooks_host + '/api/user/' + user + '/library/' + library + '/html' # assume just a remote kernel, as local else: parts = window_location.split('/') parts.pop() Help_html.showfiles_base_url = '/'.join(parts) reconnect = False for text, url in Help_html._pending_helps.items(): Help_html.add_menu_item(text, url, False, **kwargs) reconnect = True if reconnect: display(Javascript("""IPython.notebook.kernel.reconnect();""")) time.sleep(1) Help_html._pending_helps = {}
def add_menu_item(text, file_path: str, reconnect=True, **kwargs): # add help link if file_path.startswith('http'): url = file_path elif Help_html.showfiles_base_url is not None: url = Help_html.showfiles_base_url + '/' + file_path else: url = None if url: help_links = get_ipython().kernel._trait_values['help_links'] found = False for link in help_links: # if found update url if link.get('text') == text: if link.get('url') != url: link['url'] = url else: reconnect = False found = True break if not found: help_links.append({'text': text, 'url': url}) if reconnect: display(Javascript("""IPython.notebook.kernel.reconnect();""")) time.sleep(1) elif Help_html._pending_helps.get(text) is None: Help_html._pending_helps[text] = file_path
def __init__(self, width, height, scale, jsSketchFile, SketchClass, obj_json_convert_func): viewer_code = pkgutil.get_data("koebe.graphics.js.js", jsSketchFile).decode("utf8") display(Javascript(viewer_code)) self._sketch = SketchClass(width=width, height=height, scale=scale) self._objs = [] self._anim = [] self._styles = {} self.obj_json_convert_func = obj_json_convert_func
def __init__(self, initial_message=None): super(Messager, self).__init__() with open(makePath('Messager', 'widget.js')) as f: js = f.read() display(Javascript(js)) if initial_message is not None: self.message = initial_message
def force_checkpoint(): """ If in a Jupyter environment, force-saves notebook """ from IPython.core.magics.display import Javascript display( Javascript(''' require(["base/js/namespace"], function() { Jupyter.notebook.save_notebook(); }); ''')) time.sleep(0.5)
def prompt_user(self): self.description_store.before_prompt() prompt_description_js = dedent("""\ // Guard against re-execution. if (IPython.notebook.kernel) { console.log("beginning of js"); var handle_output = function(out){ console.log(out.msg_type); console.log(out); var res = null; if (out.msg_type == "stream"){ res = out.content.text; } // if output is a python object else if(out.msg_type === "execute_result"){ res = out.content.data["text/plain"]; } // if output is a python error else if(out.msg_type == "error"){ res = out.content.ename + ": " + out.content.evalue; } // if output is something we haven't thought of else{ res = "[out type not implemented]"; } console.log(res); }; var callback = { iopub: { output : handle_output, } }; var options = { silent : false, }; var description = prompt("Set description", "default"); var command = "a.description_store.set_description('" + description + "')"; console.log("executing " + command); var kernel = IPython.notebook.kernel kernel.execute(command, callback, options); console.log("end of js"); } """) jso = Javascript(prompt_description_js) display_javascript(jso) description = self.description_store.get_description() print("Description: {}".format(description)) return description
def create_labeled_variables(self, dict_name): command = """ var kernel = IPython.notebook.kernel; for (var i=0; i<localStorage.length; i++) { var key = localStorage.key(i); var existingItem = localStorage.getItem(key); var var_name = key; if (key === null) { continue } var values = existingItem.split(',') for (var j=0; j<values.length; j++) { var command = "{dict_name}['" + var_name + "'] += ['" + values[j] + "']"; kernel.execute(command); kernel.execute("{dict_name}['" + var_name + "'] = list(set({dict_name}['" + var_name + "']))"); } } """ command = command.replace('{dict_name}', dict_name) return Javascript(command)
def create_labeled_variables(self, label_dict): """Inserts labeled entries into a dictonary that can be used in colab. This function takes a dictionary as input. It extracts labeled examples stored in localStorage and inserts them into a dictonary. """ self.label_dict = label_dict output.register_callback('notebook.AddListItem', self.add_list_item) command = """ for (var i=0; i<localStorage.length; i++) { var key = localStorage.key(i); var existingItem = localStorage.getItem(key); var var_name = key; if (key === null) { continue } google.colab.kernel.invokeFunction('notebook.AddListItem', [key, existingItem], {}); } """ return Javascript(command)
def submit_get_notebook_url(self): if self.notebook_app != "jupyterlab": display( Javascript( """IPython.notebook.kernel.execute("NOTEBOOK_URL = '" + window.location + "'");""" ))
def __init__(self, shell): # constants Configurable.__init__(self, config=shell.config) Magics.__init__(self, shell=shell) set_logger(Logger()) get_ipython().magic("matplotlib inline") # Add ourself to the list of module configurable via %config self.shell.configurables.append(self) ip = get_ipython() kql_magic_load_mode = _get_kql_magic_load_mode() if kql_magic_load_mode != "silent": html_str = """<html> <head> <style> .kqlmagic-banner { display: flex; background-color: #d9edf7; } .kqlmagic-banner > div { margin: 10px; padding: 20px; color: #3a87ad; font-size: 13px; } </style> </head> <body> <div class='kqlmagic-banner'> <div><img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAH8AAAB9CAIAAAFzEBvZAAAABGdBTUEAALGPC/xhBQAAAAZiS0dEAC8ALABpv+tl0gAAAAlwSFlzAAAOwwAADsMBx2+oZAAAAAd0SU1FB+AHBRQ2KY/vn7UAAAk5SURBVHja7V3bbxxXGT/fuc9tdz22MW7t5KFxyANRrUQ8IPFQqQihSLxERBQhVUU0qDZ1xKVJmiCBuTcpVdMkbUFFRQIJRYrUB4r6CHIRpU1DaQl/AH9BFYsGbO/MOTxMPGz2MjuzO7M7sz7f0+zszJzv+32X8507PPjJFZSFMMpI3V945sLX3vzLxa5/0fjq/VsvpSmBJv/d9pXlw6upZFg+vLp8eLWLDNHd+L+26yAIugi9fHi1qzBaq9u3b3d54f1bL7V+NS4EAM/MzPSEte2dnihFzCTjmw1WhBC02tK16+cOHJinlCYwBmMyvgQaF0u//d3pXtq4i+A7Ny8JwTP4Q9enO50hrQytGsSdjhL/3fpcGIY9he4q7ubmptaqv/HFhfi+D4BTOVCSHob1h65v3mNLf3rzQqPhAsCE+0PhHGWlnmp7/OTnP/u5o4uL05bFMcbpI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YDC5YwzIz7WZxgFYIzJvdS2PVN527rv3HxhmPhQKjXEVJmeBiHYzb9fSVZAhXRQvX4eSsl7H1y9dv38ZDhBxdCPWiiHDi38+a1n95oCStTH6XlO337/CdNBsfn+AJn7RPYkV8D29yAZ9A36Bn1Dk1brjp2G3Oex6BTA2L6JPAZ9Q7lQpvbggHH/o4+2giDY2moGQaiUphQ4Z4RQIQjnLFpTWIblFSVvGw+I/mc+/e2u93/2zFfvu3/Gn3ZrNZtzChAdo4AJuasPs+ilwJzn3NO/7vvMtevnpaRCMAAkBKOUAGDGCEKodT/MaMVor73pDfoD0iMnfpr8wLeeOu7YTErputL33XqjJgSzbSqliLQCgAEmQTFlzPef//lryQ88d+mkY0vblp5nSYtJyaNF6wCIMRIN9VUC/cnZGYwQjDFBSDWbobH9UVMYqhLuDG3yfYO+IYO+Qd+QQd+gb8igb9A36BsaPf0PJmoM1QL6Q/4AAAAASUVORK5CYII='></div> <div> <p>Kusto is a log analytics cloud platform optimized for ad-hoc big data queries. Read more about it here: http://aka.ms/kdocs</p> <p> • kql language reference: Click on 'Help' tab > and Select 'kql referece'<br> • Kqlmagic configuarion: Run in cell '%config kqlmagic'<br> • Kqlmagic syntax: Run in cell '%kql?'<br> • Kqlmagic upgrate syntax: Run 'pip install git+git://github.com/Microsoft/jupyter-Kqlmagic.git --upgrade'<br> </div> </div> </body> </html>""" Display.show_html(html_str) Display.showInfoMessage( """Kqlmagic package is updated frequently. Run pip install Kqlmagic --upgrade to use the latest version.<br>Kqlmagic version: """ + VERSION + """, source: https://github.com/Microsoft/jupyter-Kqlmagic""") # <div><img src='https://az818438.vo.msecnd.net/icons/kusto.png'></div> _override_default_configuration(ip, kql_magic_load_mode) root_path = get_ipython().starting_dir.replace("\\", "/") folder_name = ip.run_line_magic("config", "Kqlmagic.temp_folder_name") showfiles_folder_Full_name = root_path + "/" + folder_name if not os.path.exists(showfiles_folder_Full_name): os.makedirs(showfiles_folder_Full_name) # ipython will removed folder at shutdown or by restart ip.tempdirs.append(showfiles_folder_Full_name) Display.showfiles_base_path = root_path Display.showfiles_folder_name = folder_name Display.notebooks_host = Help_html.notebooks_host = os.getenv( "AZURE_NOTEBOOKS_HOST") app = ip.run_line_magic("config", "Kqlmagic.notebook_app") # add help link add_kql_ref_to_help = ip.run_line_magic( "config", "Kqlmagic.add_kql_ref_to_help") if add_kql_ref_to_help: Help_html.add_menu_item("kql Reference", "http://aka.ms/kdocs", notebook_app=app) if app is None or app != "jupyterlab": display( Javascript( """IPython.notebook.kernel.execute("NOTEBOOK_URL = '" + window.location + "'");""" )) time.sleep(5)
def execute_query(self, parsed, user_ns, result_set=None): if Help_html.showfiles_base_url is None: # display(Javascript("""IPython.notebook.kernel.execute("NOTEBOOK_URL = '" + window.location + "'")""")) window_location = user_ns.get("NOTEBOOK_URL") if window_location is not None: Help_html.flush(window_location) else: display( Javascript( """IPython.notebook.kernel.execute("NOTEBOOK_URL = '" + window.location + "'");""" )) query = parsed['kql'].strip() options = parsed['options'] suppress_results = options.get( 'suppress_results', False) and options.get( 'enable_suppress_result', self.enable_suppress_result) connection_string = parsed['connection'] if options.get('version'): print('Kqlmagic version: ' + VERSION) if options.get('popup_help'): help_url = 'http://aka.ms/kdocs' # 'https://docs.loganalytics.io/docs/Language-Reference/Tabular-operators' # 'http://aka.ms/kdocs' # 'https://kusdoc2.azurewebsites.net/docs/queryLanguage/query-essentials/readme.html' # import requests # f = requests.get(help_url) # html = f.text.replace('width=device-width','width=500') # Display.show(html, **{"popup_window" : True, 'name': 'KustoQueryLanguage'}) button_text = 'popup kql help ' Display.show_window('KustoQueryLanguage', help_url, button_text) try: # # set connection # conn = Connection.get_connection(connection_string) # parse error except KqlEngineError as e: if options.get('short_errors', self.short_errors): msg = Connection.tell_format(connect_str) Display.showDangerMessage(str(e)) Display.showInfoMessage(msg) return None else: raise # parse error except ConnectionError as e: if options.get('short_errors', self.short_errors): Display.showDangerMessage(str(e)) list = Connection.get_connection_list_formatted() if len(list) > 0: Display.showInfoMessage(list) return None else: raise try: # validate connection retry_with_code = False if options.get( 'validate_connection_string', self.validate_connection_string ) and not conn.options.get('validate_connection_string'): validation_query = 'range c from 1 to 10 step 1 | count' try: raw_table = conn.execute(validation_query) conn.set_validation_result(True) except Exception as e: msg = str(e) if msg.find('AADSTS50079') > 0 and msg.find( 'multi-factor authentication') > 0 and isinstance( conn, KustoEngine): Display.showDangerMessage(str(e)) retry_with_code = True else: raise e if retry_with_code: Display.showInfoMessage( 'replaced connection with code authentication') database_name = conn.get_database() cluster_name = conn.get_cluster() connection_string = "kusto://code().cluster('" + cluster_name + "').database('" + database_name + "')" conn = Connection.get_connection(connection_string) raw_table = conn.execute(validation_query) conn.set_validation_result(True) conn.options['validate_connection_string'] = True if options.get('popup_schema') or ( options.get('auto_popup_schema', self.auto_popup_schema) and not conn.options.get('auto_popup_schema')): Database_html.popup_schema(conn) conn.options['auto_popup_schema'] = True if not query: # # If NO kql query, just return the current connection # if not connection_string and Connection.connections and options.get( 'show_conn_list', self.show_conn_list) and not suppress_results: Display.showInfoMessage( Connection.get_connection_list_formatted()) return None # # submit query # start_time = time.time() raw_table = conn.execute(query, user_ns) end_time = time.time() elapsed_timespan = end_time - start_time # # model query results # if result_set is None: saved_result = ResultSet(raw_table, query, options) saved_result.magic = self saved_result.parsed = parsed saved_result.connection = conn.get_name() else: saved_result = result_set saved_result._update(raw_table) if not connection_string and Connection.connections and options.get( 'show_conn_list', self.show_conn_list): saved_result.conn_info = Connection.get_connection_list_formatted( ) saved_result.start_time = start_time saved_result.end_time = end_time saved_result.elapsed_timespan = elapsed_timespan self.shell.user_ns.update({ options.get('last_raw_result_var', self.last_raw_result_var): saved_result }) result = saved_result if options.get('feedback', self.feedback): minutes, seconds = divmod(elapsed_timespan, 60) saved_result.info.append( 'Done ({:0>2}:{:06.3f}): {} records'.format( int(minutes), seconds, saved_result.records_count)) logger().debug("Results: {} x {}".format( len(saved_result), len(saved_result.columns_name))) if options.get('columns_to_local_vars', self.columns_to_local_vars): #Instead of returning values, set variables directly in the #users namespace. Variable names given by column names if options.get('feedback', self.feedback): saved_result.info.append( 'Returning raw data to local variables [{}]'.format( ', '.join(saved_result.columns_name))) self.shell.user_ns.update(saved_result.to_dict()) result = None if options.get('auto_dataframe', self.auto_dataframe): if options.get('feedback', self.feedback): saved_result.info.append( 'Returning data converted to pandas dataframe') result = saved_result.to_dataframe() if options.get('result_var') and result_set is None: result_var = options['result_var'] if options.get('feedback', self.feedback): saved_result.info.append( 'Returning data to local variable {}'.format( result_var)) self.shell.user_ns.update({ result_var: result if result is not None else saved_result }) result = None if result is None: return None if not suppress_results: if options.get('auto_dataframe', self.auto_dataframe): Display.showSuccessMessage(saved_result.info) else: saved_result.display_info = True else: saved_result.suppress_result = True # Return results into the default ipython _ variable return result except Exception as e: if not connection_string and Connection.connections and options.get( 'show_conn_list', self.show_conn_list) and not suppress_results: # display list of all connections Display.showInfoMessage( Connection.get_connection_list_formatted()) if options.get('short_errors', self.short_errors): Display.showDangerMessage(e) return None else: raise e