{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
1) This first block of code sets up the python environment by loading standard python classes as well as IPython display classes and widgets
" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import sys\n", "import os\n", "import getpass\n", "from IPython.display import display, HTML\n", "import ipywidgets as widgets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2) Next load ODM2API database connection class and models
\n", "a) each model is mapped to an ODM2 table, ODM2API is available on github and the ODM2API models are here:
\n", "https://github.com/ODM2/ODM2PythonAPI/blob/master/odm2api/ODM2/models.py
" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from odm2api.ODMconnection import dbconnection\n", "from odm2api.ODM2.models import *\n", " " ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "3) Now create text widgets and a container for database connection info.
" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#print(\"Enter your ODM2 username\") \n", "container = widgets.Box() # would be nice If I could get a container to hold the \n", "# user name and password prompt, getpass doesn't seem to play well with the other \n", "# widgets though\n", "username_text = widgets.Text(\n", " value='', placeholder='Enter username',\n", " description='', disabled=False)\n", "username_output_text = widgets.Text(\n", " value='', placeholder='Enter username',\n", " description='Username',disabled=False)\n", "database_address_text = widgets.Text(\n", " value='', placeholder='Enter database address',\n", " description='',disabled=False)\n", "database_address_output_text = widgets.Text(\n", " value='',placeholder='Enter database address',\n", " description='database address',disabled=False)\n", "database_text = widgets.Text(\n", " value='', placeholder='Enter database name',\n", " description='', disabled=False)\n", "database_output_text = widgets.Text(\n", " value='', placeholder='Enter database name',\n", " description='database name', disabled=False)\n", "def bind_username_to_output(sender):\n", " username_output_text.value = username_text.value\n", "def bind_database_address_to_output(sender):\n", " database_address_output_text.value = database_address_text.value\n", "def bind_database_to_output(sender):\n", " database_output_text.value = database_text.value \n", " \n", "def login(sender):\n", " #print('Database address : %s, Username: %s, database name: %s' % (\n", " # database_address_text.value, username_text.value, database_text.value))\n", " container.close() \n", " \n", "username_text.on_submit(bind_username_to_output)\n", "login_btn = widgets.Button(description=\"Login\")\n", "login_btn.on_click(login)\n", "container.children = [username_text,database_address_text, database_text, login_btn]\n", "container\n", "#username_text\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4) Now enter the password for the user.
" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "enter your password: \n", "········\n" ] } ], "source": [ "print(\"enter your password: \")\n", "p = getpass.getpass()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5) Now we will use the information entered to establish a database connection. " ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "#createConnection(self, engine, address, db=None, user=None, password=None, dbtype = 2.0, echo=False)\n", "session_factory = dbconnection.createConnection('postgresql', database_address_text.value, database_text.value, \n", " username_text.value, p) \n", "DBSession = session_factory.getSession()\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "8) To finish things off let's retrieve the new variable from the database, display it, and delete it so this script can be run again.
" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "