def shell(args=None):
    """Start an IPython shell hooked up to the jupyterhub database"""
    from .app import JupyterHub

    hub = JupyterHub()
    hub.load_config_file(hub.config_file)
    db_url = hub.db_url
    db = orm.new_session_factory(db_url, **hub.db_kwargs)()
    ns = {'db': db, 'db_url': db_url, 'orm': orm}

    import IPython

    IPython.start_ipython(args, user_ns=ns)
Start an IPython shell hooked up to the jupyterhub database
jupyterhub/dbutil.py