def query(fn, sql): connection = get_connection() cursor = connection.cursor() cursor.execute(sql) if fn: result = fn(cursor) connection.commit() connection.close() cursor.close() return result
def load_ui(): global connection, client_ui STATUS.SetLabel("Checking for saved connections...") params = mc.Parameters() params['client_name'] = CONFIG.GetValue('client_name') params['client_address'] = CONFIG.GetValue('client_address') params['client_user'] = CONFIG.GetValue('client_user') params['client_pass'] = CONFIG.GetValue('client_pass') client = params['client_name'] print "OnLoad connection to %s with url %s" % (params['client_name'], params['client_address']) if client: STATUS.SetLabel("Connecting to %s" % client) client = TORRENT_CLIENTS[params['client_name']] try: connection = get_connection(client, url=params['client_address'], username=params['client_user'], password=params['client_pass']) except Exception, e: print "ERR: %s" % e connection = None # Revert to config screen if not connection: STATUS.SetLabel("Connection failed, launching configuration.") show_config(True) else: print "Got connection" STATUS.SetVisible(False) try: client_ui = get_ui(client, connection) except Exception, e: print "ERR: %s" % e client_ui = None if not client_ui: show_config(True)
# x = pg_connect(database='mit',port=5432,host='localhost',username='******',password='******') # # x.get_table_values('users_mituser') stages = [(0, 'nothing'), (1, 'connected/ask graph'), (2, 'graph plotted')] stage = 0 while True: _msg = raw_input("Hello, What's you commend?\n\n") if ratio(_msg, 'exit') > 90 or ratio(_msg, 'bye') > 90: exit else: if stage == 0: connection = get_connection(_msg) if connection: stage = 1 print '\nConnected to Database' print '\nAll Table available are : \n\n' tables = connection.get_all_table() for i in tables: print i pass elif stage == 1: chart_params = get_chart_params(_msg, tables=tables) if 'error' in chart_params: print chart_params pass
def dashboard(request): operation = None table = None chart = False query = False table_content = None if request.method == "POST": if int(os.environ["stage_nlp"]) == 0: connection, address = get_connection(request.POST.get('message')) tables = connection.get_all_table_with_columns() tab = connection.get_all_table() os.environ["tab"] = str(tab) os.environ["tables"] = str(tables) os.environ["address"] = address os.environ["message"] = request.POST.get('message') if connection: os.environ["connected"] = 'True' os.environ["stage_nlp"] = '1' elif int(os.environ["stage_nlp"]) == 1: if request.POST.get('_method') == 'disconnect': os.environ['connected'] = 'False' os.environ["stage_nlp"] = '0' elif request.POST.get('chart'): chart = True operation = get_operation(request.POST.get('message'), ast.literal_eval(os.environ["tab"])) print operation elif request.POST.get('query'): query = True connection, address = get_connection(os.environ["message"]) x = connection.get_table_values( query=request.POST.get('message')) table_content = x.to_html xdata = [ "Apple", "Apricot", "Avocado", "Banana", "Boysenberries", "Blueberries", "Dates", "Grapefruit", "Kiwi", "Lemon" ] ydata = [52, 48, 160, 94, 75, 71, 490, 82, 46, 17] chartdata = {'x': xdata, 'y': ydata} charttype = "pieChart" chartcontainer = 'piechart_container' if os.environ.get('connected', False) == 'True': connected = True tables = ast.literal_eval(os.environ["tables"]) address = os.environ["address"] else: connected = False tables = ['none'] address = 'none' data = { 'charttype': charttype, 'chartdata': chartdata, 'chartcontainer': chartcontainer, 'extra': { 'x_is_date': True, 'x_axis_format': '', 'tag_script_js': True, 'jquery_on_ready': True, }, 'connected': connected, 'tables': tables, 'address': address, 'stage': os.environ["stage_nlp"], 'chart': chart, 'table': table, 'query': query, 'table_content': table_content, 'operation': operation } return render(request, 'chat.html', data)