Inserting dynamic HTML table to sqlite database with flask - sqlite

I am trying to insert a dynamic table built with JS into my HTML document, and the JS adds rows based on the inputs. Is it possible to loop this table into my sqlite database through flask?
`
<table id="sampleTable">
<thead>
<tbody id="tbody">
<tr id="trow">
<td id="sample1"></td>
<td id="sample2"></td>
<td id="sample3"></td>
<td id="sample4"></td>
</tr>
</tbody>
</thead>
</table>
<form action="/page/" method="POST">
<button type="submit">Submit</button>
My flask code is:
#app.route('/page/', methods=['POST','GET'])
if request.method == 'POST':
connection = sqlite3.connect('sample.db')
with connection:
sample1 = session['sample1']
sample2 = session['sample2']
sample3 = session['sample3']
sample4 = session['sample4']
cursor.execute("""
insert into sample_table (
sample1, sample2, sample3, sample4)value(?,?,?,?)""",
(sample1, sample2, sample3, sample4))
connection.commit()
`
I seem to just get KeyError each time I try.

Related

How to display a data table values in html table using angular js

I have a DataTable in OrderDetails.aspx.vb
Public Outlets As New DataTable
Outlets = objOrdersData.GetOnlineConfigCall()
In my OrderDetails.aspx
<script>
var Outlets = '<%=Outlets%>';
</script>
I want to display outlets details in a html table using AngularJS
I defined my html table as
<table class="table">
<thead>
<tr> <th>Outlets</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="outlet in Outlets">
<td><a >{{outlet.PartnerName}}</a></td>
</tr>
</tbody>
</table>
but it's not working.
Provided that Outlets is a valid js array, you just need to assign it to scope variable in your controller like:
OrderApp.controller("OrderCntrl", ['$scope', function ($scope) {
$scope.outlets = Outlets;
}]);
and then bind your data in the template like:
<tr ng-repeat="outlet in outlets">
<td><a >{{outlet.PartnerName}}</a></td>
</tr>

When iterating with {{#each}} how to not include the entry in Meteor

The person I'm designing this app for requested that she be able to make a email list of people with birthdays within the next 7 days. One of the fields in the collection is Bdate in the format 'YYYY-MM-DD'. I decided to make a registerHelper with a simple algorithm that determines if the birthdate is one that fit the request:
Template.registerHelper('calculateBirthday', function(bdate) {
var birthDate = new Date(bdate);
var current = new Date();
var diff = current - birthDate; // Difference in milliseconds
var sevenDayDiff = Math.ceil(diff/31557600000) - (diff/31557600000);
if (sevenDayDiff <= 0.01995183087435)
return date;
else
return false;
});
The template would have a table that lists the birthdates that are the ones to get for the email list:
<table class="bordered">
<thead>
<tr>
<th>Name</th>
<th>Birthday</th>
</tr>
</thead>
<tbody>
{{#each QueryBirthday}}
<tr>
<tr>{{FullName}}</tr>
<td>{{calculateBirthday Bdate}}</td>
</tr>
{{/each}}
</tbody>
</table>
The problem with this is that it prints all the names with mostly blank birthdates. The algorithm works fine, but how to tell Meteor to only include those names and birthdates that 'should' be on the list?
The quickest way to hide unwanted items is
<table class="bordered">
<thead>
<tr>
<th>Name</th>
<th>Birthday</th>
</tr>
</thead>
<tbody>
{{#each QueryBirthday}}
{{#if calculateBirthday Bdate}}
<tr>
<td>{{FullName}}</td>
<td>{{calculateBirthday Bdate}}</td>
</tr>
{{/if}}
{{/each}}
</tbody>
</table>
I don't know how your application works, but like other people who commented on your question, I would filter and send only the required results from server to client.

highlight table data based on winner values obtained from JSON

I am parsing JSON data and displaying the data to a table using angular JS.
There are many table rows that I populated from JSON file using ng-repeat.
Status.winner could be of values 0 or 1.
If the value for the winner for the table row is 0, I would like to highlight Playerdata[0].playername for that table row to yellow color.
Else If the winner for the table row is 1, then I would like to highlight Playerdata[1].playername for that table row to yellow color.
How do I do that for each row with different values of winner, which can be 0 or 1?
<body ng-app="form-input" ng-controller="ctrl">
<table class="table table-bordered table-condensed ">
<caption>Recent Game Statistic</caption>
<tbody>
<tr class="success" ng-repeat="status in recentGame">
<td ng-bind="status.Winner"> </td>
<td ng-bind="status.Playerdata[0].Playername"> </td>
<td ng-bind="status.Playerdata[1].Playername"> </td>
</tr>
var app2 = angular.module('form-input', []);
app2.controller('ctrl', function($scope,$http) {
var url = "http://...JSON";
$http.get(url).success( function(data) {
$scope.recentGame = data.RecentGames;
});
})
Use ngClass:
<tr class="success" ng-repeat="status in recentGame">
<td ng-bind="status.Winner"></td>
<td ng-bind="status.Playerdata[0].Playername" ng-class="{winner: status.Winner == 0}"></td>
<td ng-bind="status.Playerdata[1].Playername" ng-class="{winner: status.Winner == 1}"></td>
</tr>
Then in CSS define .winner class styles the way you need. For example:
.winner {
background-color: yellow;
}

How to import and save data from csv in web2py database table?

I used SQlite database.
I wrote code like this
Module:
db.py
db = DAL('sqlite://storage.sqlite')
db.define_table('data3')
db.data3.import_from_csv_file(open('mypath/test.csv'),'r')
Controller:
def website_list():
return dict(websites = db().select(db.data4.ALL))
View:
{{extend 'layout.html'}}
<h2>List Of Websites</h2>
<table class="flakes-table" style="width:100% ;">
<thead>
<tr>
<td class="id" >ID</a></td>
<td class="link" >Link</td>
</tr>
</thead>
{{for web in websites:}}
<tbody class="list">
<tr>
<td >{{=web.id}}</td>
<td >{{=web.Link}</td>
</tr>{{pass}}
</tbody>
</table>
But it is showing error as
"type 'exceptions.AttributeError'"
Also error has this line
Function argument list
(self=, key='data3')
I think some thing is wrong in reading csv file. My csv file has following data
"Link_Title","Link"
"Apple's Ad Blockers Rile Publishers","somelink"
"Uber Valued at More Than $50 Billion","somelink"
"England to Roll Out Tailored Billboards","somelink"
Can anyone help in this..?

Razor asp.net webpages - displaying all rows from a database

i am trying to develop a web application using razor view engine. It is an vacation request management system where users log onto the web site and submit vacation requests through a web form. All requests are stored in a database table called "LeaveRequests". At the moment i am trying to alter a page so when a user is logged in all the vacation requests they made are displayed on the web page in a table view. the code shown below works fine to display 1 request made by a user but i need to alter it to display all requests made by the user. i have tried using a foreach statement but keep getting errors whatever i try , can anyone point my in the right direction and tell me how i need to alter my code to achieve what i want ?
var db = Database.Open("Annual Leave System");
var dbCommand2 = "SELECT * FROM LeaveRequests WHERE email = #0";
var row2 = db.QuerySingle(dbCommand2, theEmail);
if(row2 != null) {
description = row2.description;
theLeaveType = row2.leaveType;
startDate = row2.startDate;
endDate = row2.endDate;
shortStartDate = startDate.ToString("dd-MMMM-yyyy");
shortEndDate = endDate.ToString("dd-MMMM-yyyy");
inttotalDays = row2.totalDays;
requestStatus = row2.requestStatus;
}
<fieldset>
<legend>Employee Leave Request Details</legend>
<table border="1" width="100%">
<tr bgcolor="grey">
<th>Description</th>
<th>Leave Type</th>
<th>Start Date</th>
<th>End Date</th>
<th>Total days leave requested</th>
<th>Request Status</th>
</tr>
<tr>
<th>#description</th>
<th>#theLeaveType</th>
<th>#shortStartDate</th>
<th>#shortEndDate</th>
<th>#inttotalDays</th>
<th>#requestStatus</th>
</tr>
</table>
</fieldset>
The QuerySingle method will only return one row. You need to use the Query method to get all rows.
var rows = db.Query(dbCommand2, theEmail);
Then in the HTML part of the file:
<fieldset>
<legend>Employee Leave Request Details</legend>
<table border="1" width="100%">
<tr bgcolor="grey">
<th>Description</th>
<th>Leave Type</th>
<th>Start Date</th>
<th>End Date</th>
<th>Total days leave requested</th>
<th>Request Status</th>
</tr>
#foreach(var row in rows){
<tr>
<td>#row.description</td>
<td>#row.leaveType</td>
<td>#row.startDate.ToString("dd-MMMM-yyyy")</td>
<td>#row.endDate.ToString("dd-MMMM-yyyy")</td>
<td>#row.totalDays</td>
<td>#row.requestStatus;</td>
</tr>
}
</table>
More information here:
http://www.mikesdotnetting.com/Article/214/How-To-Check-If-A-Query-Returns-Data-In-ASP.NET-Web-Pages
http://www.asp.net/web-pages/tutorials/data/5-working-with-data

Resources