Skip to content

Instantly share code, notes, and snippets.

Henrik Loeser data-henrik

View GitHub Profile
data-henrik /
Last active Jul 11, 2020
SQL statements to dig into COVID-19 data


Let's assume a simple schema consisting of two tables.

  1. STATISTICS for COVID-19 daily statistics like confirmed cases, deaths, etc.
  2. DEMOGRAPHICS with addition per-country data like population, area, population density and more


This table could have columns such as

  • country_id: Identifies the country by ISO code
data-henrik /
Last active May 7, 2020
Python snippet to download pandas Data Frame as CSV or Excel from notebook in IBM Watson Studio
# Define functions to download as CSV or Excel
from IPython.display import HTML
import pandas as pd
import base64, io
# Download as CSV: data frame, optional title and filename
def create_download_link_csv(df, title = "Download CSV file", filename = "data.csv"):
# generate in-memory CSV, then base64-encode it
csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode())
data-henrik /
Last active Apr 17, 2020
Connect to Db2 on Cloud using an API key
import ibm_db
import pandas as pd
import ibm_db_dbi as dbi
import json,sys,os
def db2test(iamKey,hostname):
conn1 = dbi.connect(connstr)
# Quick test with Pandas and dataframe
df = pd.read_sql("select count(*) from syscat.tables", conn1)
data-henrik /
Created Apr 7, 2020
IBM Cloud Function / Openwhisk action to retrieve credentials
# IBM Cloud Functions / OpenWhisk action to
# 1) obtain an IAM Bearer token based on API key from env
# 2) fetch credentials in JSON object from Key Protect key
# 3) return the arguments, credentials and token
import json,sys,os
import requests,base64
# obtain IAM access token
def getAuthToken(api_key):
data-henrik /
Created Apr 6, 2020
Create new key in IBM Cloud Key Protect from JSON object with credentials
# Obtain the necessary credentials from environment, encode them
# as base64 JSON and upload them a new key to IBM Cloud Key Protect
# The following software is used:
# - base64
# - ibmcloud: IBM Cloud CLI with Key Protect (kp) plugin
data-henrik /
Created Mar 20, 2020 — forked from lounagen/
Example for decoding a JWT Payload with your Shell (bash, zsh...)


Add this to your .profile, .bashrc, .zshrc...

BASE64_DECODER_PARAM="-d" # option -d for Linux base64 tool
echo AAAA | base64 -d > /dev/null 2>&1 || BASE64_DECODER_PARAM="-D" # option -D on MacOS

decode_base64_url() {
  local len=$((${#1} % 4))
  local result="$1"
  if [ $len -eq 2 ]; then result="$1"'=='
data-henrik /
Created Aug 12, 2019
Simple Python-based test of the Db2 on Cloud REST API
# Simple test of the Db2 on Cloud REST API
# Written by Henrik Loeser,
import requests, json, sys, time
# Read credentials from file
def readCreds(filename):
with open(filename) as data_file:
credentials = json.load(data_file)
return credentials
data-henrik /
Last active Nov 27, 2018
Pull data from a [Db2 or SQLAlchemy] database and inject into IBM Watson Discovery collection

From database into Watson Discovery

The code shown is a simple Python example of pulling data from a SQL database supported by SQLAlchemy, and then injecting the data as new JSON document into a collection of Watson Discovery Service on IBM Cloud.

data-henrik /
Last active Oct 5, 2018
Python script to generate wordcloud for given background image
# Masked wordcloud
# ================
# Using a mask you can generate wordclouds in arbitrary shapes.
import sys, getopt
from os import path
from PIL import Image
import numpy as np
import random
import matplotlib.pyplot as plt
data-henrik /
Created Jun 6, 2018
Use IBM Cloud App ID with OpenID Connect client in Python Flask app
# for full example visit
# as part of this tutorial:
# import all kinds of modules
# this one is needed for the OIDC client
from flask_pyoidc.flask_pyoidc import OIDCAuthentication
# initialize Flask, etc.
You can’t perform that action at this time.