Skip to content

Instantly share code, notes, and snippets.

@cpcloud
Created April 11, 2018 18:41
Show Gist options
  • Save cpcloud/511137aa20c3ef728e2542619871d0be to your computer and use it in GitHub Desktop.
Save cpcloud/511137aa20c3ef728e2542619871d0be to your computer and use it in GitHub Desktop.
Simple BigQuery shell using ibis
import pandas as pd
import ibis
import click
from prompt_toolkit import prompt
from prompt_toolkit.layout.lexers import PygmentsLexer
from pygments.lexers.sql import PostgresLexer
@click.command()
@click.argument('project')
@click.argument('dataset')
def main(project, dataset):
lexer = PygmentsLexer(PostgresLexer)
con = ibis.bigquery.connect(project, dataset)
while True:
query = prompt('bqsh> ', lexer=lexer)
try:
cursor = con.raw_sql(query)
result = pd.DataFrame(cursor.fetchall(), columns=cursor.columns)
except Exception as e:
print(e)
else:
print(result)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment