Flask render select based on a list of tuples - html

I have a data that looks like this that I send from flask render_template
a list of tuples called product_list
[(22, '1', 'false'), (94, '2', 'true'), (95, '3', 'false'), (100, '4', 'false'), (101, '5', 'false'), (102, '6', 'false'), (103, '7', 'false'), (104, '8', 'false'), (105, '9', 'false'), (106, '10', 'false'), (120, '11', 'false'), (121, '12', 'false')]
Essentially
first item in tuple is "product_id", second item is "product_version", third item is "is_product_active"
I am trying to render these tuples in a select tag on my html.
The list is ordered in a way such that first item in the list is suppose to be selected in the dropdown so I tried this
<select size="2" data-width="fit" style="line-height:2px">
<option value="{{product_list[0][0]}}" selected >Version: {{product_list[0][1]}}</option>
{% for product in product_list|slice:"1:" %}
<option value="{{product[0]}}">Version {{product[1]}}</option>
{% endfor %}
</select>
This doesn't work, flask jinja throws error saying jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got 'string'
Mostly coming from product_list|slice:"1:"
I essentially set the first item of the array as selected, then I am attempting to skip the first item in my array and simply add other items in the dropdown.
What is the cleaner way of doing this such that it also helps in readability of the html code.

You can use bracket notation.
{% for product in product_list[1:] %}

Related

How do I iterate a table in Jade assigning each <td> with a unique id?

I am trying to create a table containing 9 rows with 9 columns. I want to create this by iterating through it with the help of Jade. I'm quite new to Jade so I am probably way off here, but this is my code now;
var test1 = ['0', '1', '2', '3', '4', '5', '6', '7', '8']
var test2 = ['0', '1', '2', '3', '4', '5', '6', '7', '8']
for (var i = 0; i < test1.length; i++) {
tr
for (var o = 0; i < test2.length; o++) {
td(id='square-'+test1[i]+test2[o])
}
}
This (obviously?) gives me a syntax error.
How do I go about to iterate a table with 9 rows and 9 cols and give them id=square00, square01, square02 etc..?
Something like this should do the trick:
- var rows = ['0', '1', '2', '3', '4', '5', '6', '7', '8'];
- var columns = ['0', '1', '2', '3', '4', '5', '6', '7', '8'];
table
tbody
each row in rows
tr
each column in columns
td(id="square-" + row + "-" + column) #{row} - #{column}
Note that the javascript is prefaced by - , with the exception of the lines that are doing the iterating. That's because each is a reserved keyword that Jade (now Pug) recognizes as meaning you want to iterate.
Good luck!

Transfer mySQL data structure into new relation database (comma separated to relation table)

I have a several mySQL tables where I have saved the relation ID of the child table comma separated. Now I have to transfer this entries into a new table where for each relation is one entry.
Is there an easy way to transfer import query into the correct format?
Here the data example, my old table (cat_projects) has the following entries I want convert:
-- export of table cat_projects
INSERT INTO `cat_projects` (`id`, `authors`) VALUES
(2, '4,1'),
(3, '0'),
(4, '8,4,1'),
(5, '13,12'),
(10, '19,4,1'),
(13, ''),
(14, ''),
(15, '28,27,25,12,9,1');
This entries I want just to write into the new relation table (cat_project_relation). The att_id links to the another table where I have save the settings of the old authors column:
-- att_id = 58
-- item_id = id
-- value_sorting = counting from 0 for each item_id
-- value_id = for each relation one entry value
INSERT INTO `cat_project_relation` (`att_id`, `item_id`, `value_sorting`, `value_id`) VALUES
(58, 2, 0, '4'),
(58, 2, 1, '1'),
(58, 3, 0, '0'),
(58, 4, 0, '8'),
(58, 4, 1, '4'),
(58, 4, 2, '1'),
(58, 5, 0, '13'),
(58, 5, 1, '12'),
(58, 10, 0, '19'),
(58, 10, 1, '4'),
(58, 10, 2, '1'),
(58, 13, 0, ''),
(58, 14, 0, ''),
(58, 15, 0, '28'),
(58, 15, 1, '27'),
(58, 15, 2, '25'),
(58, 15, 3, '12'),
(58, 15, 4, '9'),
(58, 15, 5, '1');
I hope it is clear what I try to achieve. Is it possible to do that directly in SQL or do I have to apply an external bash script?
Actually it wasn't as difficult as I thought to write a little bash script. And I guess its the easier solution then program something in SQL.
#!/bin/bash
# input table
table="(2, '4,1'),
(3, '0'),
(4, '8,4,1'),
(5, '13,12'),
(10, '19,4,1'),
(13, ''),
(14, ''),
(15, '28,27,25,12,9,1');"
# fixed attribute id
att_id=58
# read each line into an array
readarray -t y <<<"$table"
# for each array item (each line)
for (( i=0; i<${#y[*]}; i=i+1 ))
do
z=${y[$i]}
# split by comma into array
IFS=', ' read -r -a array <<< "$z"
# loop through each value
for (( j=0; j<${#array[*]}; j=j+1 ))
do
# remove all other characters then number
nr=$(echo ${array[$j]} | sed 's/[^0-9]*//g')
# each first value is the item_id
if [ $j -eq 0 ]
then
item_id=$nr
else
k=$(expr $j - 1)
value_id=$nr
# print output line by line
echo "($att_id, $item_id, $k, '$value_id')," >> output.txt
fi
done
done
The result will be as the on asked in the question.

Count specific key/value pairs in dynamic json object

I have a json object which will contain key-value pairs for clients for example, the clients will vary on a day to day basis, depending on which ones have made a certain transaction on said day of the month. My data will therefore only contain the transactions made by clients. Here is an example of my data object for the first day of the month
$scope.dailybreakdown = [
{'day': '1', 'client':'client1', 'received': '3', 'approved':'0'},
{'day': '1', 'client':'client2', 'received': '8', 'approved':'1'},
{'day': '1', 'client':'client3', 'received': '4', 'approved':'0'},
{'day': '1', 'client':'client4', 'received': '4', 'approved':'0'},
{'day': '2', 'client':'client1', 'received': '3', 'approved':'1'},
{'day': '2', 'client':'client4', 'received': '12', 'approved':'5'},
{'day': '3', 'client':'client1', 'received': '10', 'approved':'2'},
{'day': '3', 'client':'client3', 'received': '5', 'approved':'1'},
{'day': '3', 'client':'client8', 'received': '1', 'approved':'0'},
{'day': '3', 'client':'client9', 'received': '2', 'approved':'2'},
]
The data for the second day may contain more or less clients and they could be different, since clients who have not made any transactions will not be present in the object whatsoever. This object would obviously contain data for every day of the month and not just for the first three day as in the sample i added above.
I am using this data to display it graphically in a table and on flot charts on my view. This is all well and good, but where I am looking for some assistance is to how to sort this data and count it.
For instance my table needs to display the total number of transacations for each day. This means I need to count all the Day 1 kay/value pairs and create totals for each field. (total received, approved & recalculate a percentage)
Since this json object could be gigantic, depending on the number of clients thats transacted each day, should I create a new json array in my controller to hold this daily data or is there a way I can filter through it and count/recalculate each day as needed?
My table row looks as follows: (which would also need to change to accomodate the data filtering done in the controller for the client totals)
<tr ng-repeat="daily in dailystats" ng-click="setSelected($index);" ng-class="{'selected':$index == selectedRow}">
<td>{{daily.day}}</td>
<td>{{daily.received}}</td>
<td>{{daily.approved}}</td>
<td>{{daily.percentage}}</td>
</tr>
Any help would be greatly appreciated since I have not been working long with json data objects and I have not been successful in finding any resources or examples to help me with this task.
To anyone willing to help, you have my gratitude in advance.
Below is a way of going through each row in your json table and using a for loop to find all the days equal to 1, 2, 3...etc then finding the "received" total and adding them all together and pushing that total number to a new array then using ng-repeat displaying that total in a table. I hope this helps you along the right track...
Index.html
<table>
<thead>
<th>Day</th>
<th>Recieved</th>
<th>Approved</th>
<th>Percentage</th>
</thead>
<tbody>
<tr ng-repeat="daily in dailybreakdown" ng-click="setSelected($index);" ng-class="{'selected':$index == selectedRow}">
<td>{{daily.day}}</td>
<td>{{daily.received}}</td>
<td>{{daily.approved}}</td>
<td>{{daily.percentage}}</td>
</tr>
</tbody>
</table>
<table>
<thead>
<th>Day</th>
<th>Recieved</th>
</thead>
<tbody>
<tr ng-repeat="day in days" ng-click="setSelected($index);" ng-class="{'selected':$index == selectedRow}">
<td>Day {{$index + 1}}</td>
<td>{{day}}</td>
</tr>
</tbody>
</table>
app.js
$scope.days = [];
$scope.dailybreakdown = [
{'day': '1', 'client':'client1', 'received': '3', 'approved':'0'},
{'day': '1', 'client':'client2', 'received': '8', 'approved':'1'},
{'day': '1', 'client':'client3', 'received': '4', 'approved':'0'},
{'day': '1', 'client':'client4', 'received': '4', 'approved':'0'},
{'day': '2', 'client':'client1', 'received': '3', 'approved':'1'},
{'day': '2', 'client':'client4', 'received': '12', 'approved':'5'},
{'day': '3', 'client':'client1', 'received': '10', 'approved':'2'},
{'day': '3', 'client':'client3', 'received': '5', 'approved':'1'},
{'day': '3', 'client':'client8', 'received': '1', 'approved':'0'},
{'day': '3', 'client':'client9', 'received': '2', 'approved':'2'},
]
for(var x = 1; x <= 31; x++){
var count = 0;
angular.forEach($scope.dailybreakdown, function(value, key) {
if(value.day == x){
count += parseInt(value.received,10);
}
});
$scope.days.push(count);
console.log($scope.days);
}
http://plnkr.co/edit/TK9QPwkcDWSJ1NjbH95a?p=preview

Is there an equivalent to "tr" within SQL?

To remind you, tr
reads a byte stream from its standard input and writes the result to
the standard output. As arguments, it takes two sets of characters
(generally of the same length), and replaces occurrences of the
characters in the first set with the corresponding elements from the
second set. -- Wikipedia
I need to this within the context of an UPDATE statement and, ideally, without doing 26 nested REPLACE function calls.
EDIT:
For you nosy-Parkers who just have to know what I'm doing: I want to make the in-database equivalent of upsidedowntext.com. Right now, I do this:
SELECT
REVERSE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(UPPER(value),
'A', 'ɐ'),
'B', 'q'),
'C', 'ɔ'),
'D', 'p'),
'E', 'ǝ'),
'F', 'ɟ'),
'G', 'b'),
'H', 'ɥ'),
'I', 'ı'),
'J', 'ظ'),
'K', 'ʞ'),
'L', 'ן'),
'M', 'ɯ'),
'N', 'u'),
'O', 'o'),
'P', 'd'),
'Q', 'b'),
'R', 'ɹ'),
'S', 's'),
'T', 'ʇ'),
'U', 'n'),
'V', 'ʌ'),
'W', 'ʍ'),
'X', 'x'),
'Y', 'ʎ'),
'Z', 'z')) from table;

SQL Syntax Error/Cannot Add or update a child row

When I try to insert the values into the table "medlimir" I get this error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (2508962989_funi.medlimir, CONSTRAINT fk_medlimir_poststod1 FOREIGN KEY (poststod) REFERENCES poststod (postnumer) ON DELETE NO ACTION ON UPDATE NO ACTION)"
If I try to insert the values one by one, I get the following:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2
I have no idea why this happens, I've looked at some similar questions but I haven't seen one that applies to this. Here's my code;
INSERT INTO medlimir VALUES
('ChildID1', 'Childname1', 'ChildHome1', 'Phone', 'Sport', 'M', '61907792409', 220),
('ChildID2', 'Childname2', 'Childhome2', 'Phone', 'Kúla', 'M', '15776113692', 220),
('ChildID3', 'Childname3', 'CHildHome3', 'Phone', 'Þríþraut', 'M', '94364592306', 111),
('ChildID4', 'Childname4', 'ChildHome4', 'Phone', 'Hlaup', 'M', '30848878228', 111),
('1410908008', 'Tsunayoshi Sawada', 'Namimori High', '6966873', 'Hlaup', 'M', '28028131468', 220),
('2608903265', 'Ryohei Sasagawa', 'Namimori Box', '4476920', 'Box', 'M', '91010668790', 220),
('0909906666', 'Gokudera Hayato', 'Namimori High', '1024632', 'Spjót', 'M', '63229758735', 220),
('2404903568', 'Takeshi Yamamoto', 'Namimori High', '', 'Kúla', 'M', '62750060985', 220),
('2501106575', 'Giotto Primo', 'Vongola Mansion', '', 'Langst.', 'M', '37282894310', 600),
('0512904873', 'Chrome Dokuro', 'Kokuyo', '', 'gólfleikf.', 'F', '17904297752', 111),
('0505903157', 'Hibari Kyoya', 'Kondo', '8018296', 'Tvíslá', 'M', '64388759435', 000),
('2805991337', 'Lambo', 'Namimori', '', 'Þríþraut', 'M', '81540318862', 600),
('1309035730', 'Reborn', 'Vongola Mansion', '6118960', 'Spjót', 'M', '45849294908', 600),
('0707031683', 'Colonnello', 'Vongola Mansion', '8663306', 'Kringla', 'M', '35282586046', 111),
('20020342489', 'Lal Mirch', 'Vongola Mansion', '947033', 'Kringla', 'F', '71126656838', 111);
The first 4 values were real, so I changed them a bit, rest is fake information. Any idea as to what may be wrong?
There is a missing symbol in the fifth line.
Instead of
('ChildID4', 'Childname4, 'ChildHome4', 'Phone', ...
it should be
('ChildID4', 'Childname4', 'ChildHome4', 'Phone', ...
^
|____ this is the missing symbol