Skip to content

Instantly share code, notes, and snippets.

@amjith
Last active November 24, 2017 06:13
Show Gist options
  • Save amjith/9c50bf5d28f998bc5049ae573dbab375 to your computer and use it in GitHub Desktop.
Save amjith/9c50bf5d28f998bc5049ae573dbab375 to your computer and use it in GitHub Desktop.
Template for creating awesome REPLs with prompt-toolkit
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.contrib.completers import WordCompleter
from pygments.lexers.sql import SqlLexer
import sqlite3
history = FileHistory('history.txt')
completer = WordCompleter(['select', 'insert', 'delete', 'drop', 'from'])
connection = sqlite3.connect(':memory:')
while 1:
try:
inp = prompt('prompt> ', history=history, lexer=SqlLexer, completer=completer)
with connection:
print (connection.execute(inp).fetchall())
except EOFError:
print ('Good bye!')
exit(0)
except Exception as e:
print (str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment