From f91aa887de7c86248cf1bcecf1879f891f4b38ab Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 18 Dec 2018 19:40:31 -0800 Subject: rename sqlite-notebook --- README.md | 15 ++++++++----- sqlite-markdown.py | 66 ------------------------------------------------------ sqlite-notebook.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 72 deletions(-) delete mode 100755 sqlite-markdown.py create mode 100755 sqlite-notebook.py diff --git a/README.md b/README.md index 7fafc49..1b3ada2 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,15 @@ -An un-inventively named quick script to process markdown documents, executing -in-line SQL statements against a sqlite database. +# sqlite-notebook -You write a markdown file with code tagged `sql`, then run this script against -it, along with a local sqlite3 database file, and you get HTML output with the -query results in table form. Sort of like a crude/simple jupyter notebook. +An un-inventively named quick script to process markdown documents with inline +SQL queries. -Requires the python package `mistune`. Try something like: +You write a markdown file with code blocks tagged `sql`, then run this script +against it, along with a local sqlite3 database file, and you get HTML output +with the query results in table form. Sort of like a crude/simple jupyter +notebook. + +Requires the python package `mistune` to parse markdown. Try something like: # debian/ubuntu sudo apt install python3-mistune diff --git a/sqlite-markdown.py b/sqlite-markdown.py deleted file mode 100755 index 3c9e72c..0000000 --- a/sqlite-markdown.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python3 - -import sys -import sqlite3 -import mistune -import argparse - -class SqliteMarkdownRenderer(mistune.Renderer): - - def __init__(self, conn): - super(SqliteMarkdownRenderer, self).__init__() - self.conn = conn - - def block_code(self, code, lang): - if lang.strip().lower() == 'sql': - # remove comment lines - core = code.strip() - code = '\n'.join( - [line.strip() for line in code.split('\n') if not (line.strip().startswith('#') or not line.strip())]) - core = code.strip() - sys.stderr.write("executing: {}\n".format(code.strip())) - cursor = self.conn.cursor() - result = cursor.execute(code) - ret = "" - if cursor.description is None: - ret = "(empty result)" - else: - ret += "\n \n" - for k in cursor.description: - ret += " \n".format(k[0]) - ret += "\n" - if not result.rowcount: - ret += "" - for row in result: - ret += "\n" - for v in row: - if v is None: - v = '' - if type(v) == str and (v.startswith("https://") or v.startswith("http://")): - ret += ' \n'.format(v, v) - elif type(v) == str and v.startswith("10."): - ret += ' \n'.format(v, v) - else: - ret += ' \n'.format(v) - ret += "\n" - ret += "
{}
(no rows returned)
{}{}{}
" - ret += "
QUERY: {}
\n
".format(code) - return '
' + ret + "
" - else: - return "\n" + code + "\n" - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument("markdown_file", - default=sys.stdin, type=argparse.FileType('rt')) - parser.add_argument("sqlite_db_path", - type=str) - args = parser.parse_args() - - conn = sqlite3.connect(args.sqlite_db_path) - renderer = SqliteMarkdownRenderer(conn) - markdown = mistune.Markdown(renderer=renderer) - print(markdown(args.markdown_file.read())) - -if __name__ == '__main__': - main() diff --git a/sqlite-notebook.py b/sqlite-notebook.py new file mode 100755 index 0000000..3c9e72c --- /dev/null +++ b/sqlite-notebook.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 + +import sys +import sqlite3 +import mistune +import argparse + +class SqliteMarkdownRenderer(mistune.Renderer): + + def __init__(self, conn): + super(SqliteMarkdownRenderer, self).__init__() + self.conn = conn + + def block_code(self, code, lang): + if lang.strip().lower() == 'sql': + # remove comment lines + core = code.strip() + code = '\n'.join( + [line.strip() for line in code.split('\n') if not (line.strip().startswith('#') or not line.strip())]) + core = code.strip() + sys.stderr.write("executing: {}\n".format(code.strip())) + cursor = self.conn.cursor() + result = cursor.execute(code) + ret = "" + if cursor.description is None: + ret = "(empty result)" + else: + ret += "\n \n" + for k in cursor.description: + ret += " \n".format(k[0]) + ret += "\n" + if not result.rowcount: + ret += "" + for row in result: + ret += "\n" + for v in row: + if v is None: + v = '' + if type(v) == str and (v.startswith("https://") or v.startswith("http://")): + ret += ' \n'.format(v, v) + elif type(v) == str and v.startswith("10."): + ret += ' \n'.format(v, v) + else: + ret += ' \n'.format(v) + ret += "\n" + ret += "
{}
(no rows returned)
{}{}{}
" + ret += "
QUERY: {}
\n
".format(code) + return '
' + ret + "
" + else: + return "\n" + code + "\n" + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("markdown_file", + default=sys.stdin, type=argparse.FileType('rt')) + parser.add_argument("sqlite_db_path", + type=str) + args = parser.parse_args() + + conn = sqlite3.connect(args.sqlite_db_path) + renderer = SqliteMarkdownRenderer(conn) + markdown = mistune.Markdown(renderer=renderer) + print(markdown(args.markdown_file.read())) + +if __name__ == '__main__': + main() -- cgit v1.2.3