Create a quick UI for Cosmos DB with Flex

daniel Cosmos DB, Python Leave a Comment

Cosmos DB is one database for great building hubs. Sometime it is needs to offer some UI to the business users.

How easy it is to display or export the data in a CSV on a fly we want to show here in this post.

First we start a page.

@app.route("/")
def hello():
    msg=buildjson_fromcosmos()
    return render_template('content.html', msg=msg)

In the html page we define the table.

<table id="table" class="table-striped table-sm  bright"
					data-show-export="true"
					data-pagination="true"
					data-click-to-select="true"
					data-sortable="true"
					data-show-refresh="true"
					data-show-columns="true"
					data-toolbar="#toolbar"
					data-show-toggle="false"
					data-search="true"
					data-advanced-search="true"
					data-id-table="advancedTable"
					data-url="/getdatacomos">
					<thead class="thead-dark">
						<tr>
							<th data-field="state" data-checkbox="true"></th>
							<th data-field="id">ID</th>				
							<th data-field="ident" data-sortable="true">IDENT</th>
							<th data-field="contact_channel" data-sortable="true">Contact Channel</th>
							<th data-field="usage" data-sortable="true">Usage</th>
							<th data-field="valid_from" data-sortable="true">Valid from</th>
							<th data-field="valid_to" data-sortable="true">Valid to</th>
							<th data-field="last_contact" data-sortable="true">Last Contact</th>
							<th data-field="source" data-sortable="true">Source</th>
							<th data-field="source_last_contact" data-sortable="true">Source Last Contact</th>
							<th data-field="source_obj" data-sortable="true">Source Obj</th>
							<th data-field="source_id"data-sortable="true" >Source Id</th>
							<th data-field="optin_version" data-sortable="true">Optin Version</th>
							<th data-field="validity"data-sortable="true">Validity</th>
							<th data-field="contact_type" data-sortable="true">Contact Type</th>
							<th data-field="status" data-sortable="true">Status</th>	
						</tr>
					</thead>
					</table>

In the URL /getdatacosmos we getting the data from Cosmos

cosmos_date = None

    search = request.args.get("search")
    
    print ('Search is ', search)
    print (type(search))
    if search is None or len(search) < 1:
        cosmos_date = datetime.datetime.today().strftime('%Y-%m-%d')
        cosmos_date = time.mktime(datetime.datetime.strptime(cosmos_date, '%Y-%m-%d').timetuple())
        search = None


def search_cosmos(search, cosmos_date):

    
    
    if search is not None:
            query = {'query': 'SELECT TOP 100000 * FROM c where CONTAINS(c.ident, "%s")' % search}
            print (query)
    else:
            query = {'query': 'SELECT * FROM c where c._ts >= %s' % cosmos_date }
            print (query)

    # Query them in SQL
    # SELECT * FROM c WHERE c.DOC_TYPE = "r" AND c.REQUEST_ID = "vjlIhTwLxdloG4EIXe6MXCEQ0qOYNwrw3VrxkrSjF8o="
    

    options = {}
    options['enableCrossPartitionQuery'] = True
    options['maxItemCount'] = -1
    result_iterable = client.QueryItems(app.config['DBLINK'], query, options)

    results = list(result_iterable)

    results = json.dumps(results, ensure_ascii=False)

    

    # results = results[1:-1]

    d = json.loads(results)
    
    return d
    

Leave a Reply