Skip to content

Instantly share code, notes, and snippets.

@javier
Last active October 25, 2024 15:56
Show Gist options
  • Save javier/d3ca14497d95109a2d0b21126277c0ee to your computer and use it in GitHub Desktop.
Save javier/d3ca14497d95109a2d0b21126277c0ee to your computer and use it in GitHub Desktop.
demo_basic_jupyter.ipynb
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
jupyter nbconvert --to webpdf demo_basic_jupyter.ipynb --allow-chromium-download
import psycopg as pg
import os
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
# Fetch environment variables with defaults
host = os.getenv('QDB_CLIENT_HOST', '127.0.0.1')
port = os.getenv('QDB_CLIENT_PORT', '8812')
user = os.getenv('QDB_CLIENT_USER', 'admin')
password = os.getenv('QDB_CLIENT_PASSWORD', 'quest')
conn_str = f'user={user} password={password} host={host} port={port} dbname=qdb'
with pg.connect(conn_str, autocommit=True) as connection:
with connection.cursor() as cur:
cur.execute("""\
SELECT
rnd_str('EUR','USD', 'GBP', 'BRL','CLP','AUD','EGP','INR', 'KWD', 'JPY') as currency,
avg(rnd_double()) as value
FROM
long_sequence(20,128349234,4327897);
""")
records = cur.fetchall()
plt.figure(figsize=(3, 3))
currency = [record[0] for record in records]
value = [record[1] for record in records]
plt.bar(currency, value)
plt.xlabel('currency')
plt.ylabel('value')
plt.title('currencies')
plt.xticks(rotation=45)
plt.plot()
pp = PdfPages('report.pdf')
pp.savefig()
pp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment