Jade.js tables and variables - node.js

I'm having issues where I can easily create a table in Jade if I use a variable that I've defined on the page, but as soon as I try to use anything else it prints a long table of nothing.
For instance I can produce a table with the below code:
table
thead
tr
th Bid ID
th Bid Value
tbody
items = [ {"bid_id":1, "bid_value":1.63},{"bid_id":2, "bid_value":1.75},{"bid_id":3, "bid_value":1.00} ]
each item, i in items
tr
td #{item.bid_id}
td #{item.bid_value}
However when I try to use the following I get a very long table that's completely empty!
table
thead
tr
th Bid ID
th Bid Value
tbody
items = all_bids
each item, i in items
tr
td #{item.bid_id}
td #{item.bid_value}
all_bids contains the exact same JSON as defined explicitly above. If I print it in the Jade view using:
p= all_bids
It prints the array correctly as:
[ {"bid_id":1, "bid_value":1.63},{"bid_id":2, "bid_value":1.75},{"bid_id":3, "bid_value":1.00} ]
Struggling to find any decent documentation on creating tables in Jade so any help would be appreciated!
Thanks!

So... is all_bids an array or maybe it is a json string?? It seems that all_bids is a string in your case. In this case each loops over characters and since characters do not have neither bid_id nor bid_value property you obtain a big and empty table.
Now how did I come up with this stuff?? Let's try to be detectives for a moment, shall we? :) Look at this line: p= all_bids. It produces this output:
[ {"bid_id":1, "bid_value":1.63},{"bid_id":2, "bid_value":1.75},{"bid_id":3, "bid_value":1.00} ]
Normally if it was an array you would get:
"[object Object],[object Object],[object Object]"
because of .toString() call (which happens behind the scene). Therefore all_bids is not an array, it's a string!
When you pass all_bids to Jade, try converting it into an object, i.e. JSON.parse(all_bids);.

Related

I need an Integer but its a string with a comma

I'm using sqlite3 and trying to get the oid by using the title of the row and then trying to use that oid to update a column in my table.
allOID is a tuple, and when I print it i get this:
>>> <class 'tuple'>
>>> [(1,)]
I'm trying to get the integer out of this tuple but the comma is throwing it off and I can't seem to get it.
Here is all of the code being used currently:
c.execute("""SELECT oid FROM books
WHERE title = :title""",
{
'title': title
})
allOID = c.fetchall()
print(type(allOID[0]))
print(allOID)
c.execute("SELECT * FROM books")
c.execute("""UPDATE books SET
rented = :rented
WHERE oid = :oid""",
{
'rented': rentedVar,
'oid': allOID[0]
})
any help and comments are greatly appreciated!
The comma just indicates that it is a tuple with a single element.
Access it using allOID[0][0].
allOID[0] gets you the tuple out of the list of results, going one level further with allOID[0][0] gets you the first element of the tuple.
For more info, see the docs:
Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective.

How to parse the only the second span tag in an HTML document using python bs4

I want to parse only one span tag in my html document. There are three sibling span tags without any class or I'd. I am targeting the second one only using BeautifulSoup 4.
Given the following html document:
<div class="adress">
<span>35456 street</span>
<span>city, state</span>
<span>zipcode</span>
</div>
I tried:
for spn in soup.findAll('span'):
data = spn[1].text
but it didn't work. The expected result is the text in the second span stored in a a variable:
data = "city, state"
and how to to get both the first and second span concatenated in one variable.
You are trying to slice an individual span (a Tag instance). Get rid of the for loop and slice the findAll response instead, i.e.
>>> soup.findAll('span')[1]
<span>city, state</span>
You can get the first and second tags together using:
>>> soup.findAll('span')[:2]
[<span>35456 street</span>, <span>city, state</span>]
or, as a string:
>>> "".join([str(tag) for tag in soup.findAll('span')[:2]])
'<span>35456 street</span><span>city, state</span>'
Another option:
data = soup.select_one('div > span:nth-of-type(2)').get_text(strip=True)
print(data)
Output:
city, state

Button is not getting inserted inside table row in Pug

I am trying to add the button as row element in Pug table.
But button is not getting inserted at specific row
following is the pug code
table
tr#headtag
th DishName
th Quantity
th Currently_Done
th Predicted
th
each order in orders
tr
th=order.dish.name
th=order.numberOfQuantity
th=order.dish.predictedValue
th=order.dish.predictedValue
th=button(class="btn btn-small", type="button") Slett
Getting 'Something failed error' in browser
Without button code is working fine.
table
tr#headtag
th DishName
th Quantity
th Currently_Done
th Predicted
each order in orders
tr
th=order.dish.name
th=order.numberOfQuantity
th=order.dish.predictedValue
th=order.dish.predictedValue
Remove = before button and , (optional) between class and type and try the following instead:
th
button(class="btn btn-small" type="button") Slett
From the docs:
Buffered code starts with =. It evaluates the JavaScript expression
and outputs the result. For security, buffered code is first HTML
escaped.
Therefore th=button(class="btn btn-small", type="button") Slett won't work.

count occurrences of a string in a structure

I have a structure mydata and I need to access one of its fields mydata.myfield, and within that field, access another field mydata.myfield.mysecondfield. In the last field, mydata.myfield.mysecondfield I need to check how many times a particular string ('apple') occurs.
I have tried with:
aaa=unique(mydata.myfield.mysecondfield,'apple')
bbb=cellfun(#(x) sum(ismember(mydata.myfield.mysecondfield,x)),aaa,'un',0)
but I get this error: Attempt to reference field of non-structure array.
The structure contains fields with both strings and numeric values.
The underlying problem may be due to the fact that the structure is a little bit different from how you describe it. Following your question, I created it as follows:
mydata = struct();
mydata.myfield.mysecondfield = {'apple' 'apple' 'orange' 'banana' 'apple' 'kiwi'};
and since I'm not getting the same error you get, I think the underlying types may be slightly mismatching. Anyway, given mydata defined as above, if you change your code as follows, it should work but it will return the count of every unique occurrence within the field:
aaa = unique(mydata.myfield.mysecondfield);
bbb = cellfun(#(x) sum(ismember(mydata.myfield.mysecondfield,x)),aaa,'un',0)
bbb =
4×1 cell array
[3]
[1]
[1]
[1]
If you only want to count the number of apple occurrences, you should use the following approach instead:
apple_count = sum(strcmp(mydata.myfield.mysecondfield,'apple')); % 3

fetch html data from website into excel vba

Object does not support this property or method.
Error occur on this line:
For Each ele In objIE.document.getElementsByClassName("table info-table").getElementsByTagName("tr")
getElementsByClassName() returns a collection of matching elements (even if there's only one match), so you need something like (e.g.):
For Each ele In objIE.document.getElementsByClassName( _
"table info-table")(0).getElementsByTagName("tr")
which will loop over the tr elements in the first table with a matching class name.
If you need a different table you'll need to adjust the (0)

Resources