import sys
!{sys.executable} -m pip install toolforge
Requirement already satisfied: toolforge in /srv/paws/lib/python3.6/site-packages
Requirement already satisfied: requests in /srv/paws/lib/python3.6/site-packages (from toolforge)
Requirement already satisfied: pymysql in /srv/paws/lib/python3.6/site-packages (from toolforge)
Requirement already satisfied: certifi>=2017.4.17 in /srv/paws/lib/python3.6/site-packages (from requests->toolforge)
Requirement already satisfied: idna<2.8,>=2.5 in /srv/paws/lib/python3.6/site-packages (from requests->toolforge)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /srv/paws/lib/python3.6/site-packages (from requests->toolforge)
Requirement already satisfied: urllib3<1.24,>=1.21.1 in /srv/paws/lib/python3.6/site-packages (from requests->toolforge)
import toolforge
import ipywidgets as widgets


def get_hits(date, tool='*'):
    conn = toolforge.connect(
        dbname='s53734__toolviews_p',
        cluster='labsdb',
        host='tools.db.svc.eqiad.wmflabs',
    )
    try:
        with conn.cursor(pymysql.cursors.DictCursor) as cur:
            sql = (
                "SELECT hits, tool "
                "FROM daily_raw_views "
                "WHERE request_day = %s "
            )
            params = (date.strftime('%Y-%m-%d'),)
            if tool != '*':
                sql += "AND tool = %s "
                params += (tool,)
            sql += "ORDER BY hits desc"
            cur.execute(sql, params)
            return cur.fetchall()
    finally:
        conn.close()

        
widgets.DatePicker(
    description='Pick a Date',
    disabled=False
)