Last active
February 7, 2022 11:53
-
-
Save poweruser82/521744ef75f3f8d46352d71711a68001 to your computer and use it in GitHub Desktop.
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
import subprocess | |
import os | |
from IPython.core.magic import Magics, cell_magic, magics_class | |
from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring | |
from pygments import highlight | |
from pygments.lexers import MySqlLexer | |
from pygments.formatters import HtmlFormatter | |
from IPython.display import display, HTML | |
def print_out(out: str): | |
for l in out.split('\n'): | |
print(l) | |
def displayHTML(html_code): | |
''' | |
Display HTML in notebook | |
''' | |
display(HTML(html_code)) | |
@magics_class | |
class SQL(Magics): | |
@magic_arguments() | |
@argument('-s', '--style', type=str, help='Pygments style name') | |
@cell_magic | |
def sql(self, line='', cell=None): | |
''' | |
C++ syntax highlighting cell magic. | |
''' | |
global style | |
args = parse_argstring(self.sql, line) | |
if args.style == None: | |
displayHTML(highlight(cell, MySqlLexer(), HtmlFormatter(full=True,nobackground=True))) | |
else: | |
displayHTML(highlight(cell, MySqlLexer(), HtmlFormatter(full=True,nobackground=True,style=args.style))) | |
def load_ipython_extension(ip): | |
os.system('pip install pygments ipywidgets') | |
plugin = SQL(ip) | |
ip.register_magics(plugin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment