I've seen a few CS50 Finance help questions regarding /index. I'm trying to get the route to display a user's owned stock information (share number, value, price, etc.). Right now it displays the correct share amounts but does not display the name, value, or price (all blank). The "cash" and "grandTotal" amounts display but grandTotal is not the correct amount. I think I'm just confused on how to access specific values in my returns.
Python/sqlite:
def index():
"""Show portfolio of stocks"""
# sql queries to select stock info
user_id = session["user_id"]
stocks = db.execute("SELECT stock, symbol, SUM(shares) AS totalShares FROM purchases WHERE userid == :userid GROUP BY symbol", userid=user_id)
currentCash = db.execute("SELECT cash FROM users WHERE id == :userid", userid=user_id)
# Global variables to be updated
tableInfo = []
grandTotal = currentCash[0]["cash"]
#Grabbing info from each owned stock
for stockInfo in stocks:
symbol = stocks[0]["symbol"]
shares = stocks[0]["totalShares"]
name = stocks[0]["stock"]
currentStock = lookup(symbol)
price = currentStock["price"]
value = price * shares
grandTotal += value
tableInfo.append(stockInfo)
# Display a table with portfolio info for current user
return render_template("index.html", tableInfo=tableInfo, grandTotal=usd(grandTotal), currentCash=usd(currentCash[0]["cash"]))
HTML:
{% extends "layout.html" %}
{% block title %}
Your Portfolio
{% endblock %}
{% block main %}
<table class="table">
<thead>
<tr>
<th scope="col">Stock</th>
<th scope="col">Number of shares</th>
<th scope="col">Current price</th>
<th scope="col">Total value</th>
</tr>
</thead>
<tbody>
{% for stock in tableInfo %}
<tr>
<td>{{ stock.name }}</td>
<td>{{ stock.totalShares }}</td>
<td>{{ stock.price }}</td>
<td>{{ stock.value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<table class="table">
<thead>
<tr>
<th scope ="col">Cash remaining</th>
<th scope ="col">Grand total</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ currentCash }}</td>
<td> {{ grandTotal }}</td>
</tr>
</tbody>
</table>
{% endblock %}
I have table from sql and i need to make filter on it when click 'search' button filter result based on 'Name' Column.
.py:
#flask.route('/next_lecture')
def nextlec():
check = (session['isadmin'])
check_id = session['Id']
if check == 1:
cursor = mysql.connection.cursor()
cursor.execute('SELECT * FROM `sessioncreate` WHERE `ownerId` = %s ORDER BY Id DESC;', str(check_id))
myresult = cursor.fetchall()
# get info
return render_template("next-lecture.html", myresult=myresult)
.html page:
<table class="table table-striped mb-0" id="search" name="search">
<thead>
<tr class="live-blue text-white">
<th scope="col">Name</th>
<th scope="col">Date</th>
<th scope="col">STime</th>
<th scope="col">ERime</th>
<th scope="col">Age</th>
</tr>
</thead>
<tbody>
{% for row in myresult %}
<tr>
<td>{{row[1]}}</td>
<td>{{row[2]}}</td>
<td>{{row[3]}}</td>
<td>{{row[4]}}</td>
<td>{{row[6]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
Table Picture
How can i achieve that?
Here is my Django Template Used to Display The data in table Format...
enter code here
<table class="content" border="2px">
<thead>
<tr>
<th>S-No</th>
<th>Name</th>
<th>Email</th>
<th>Productname</th>
<th>Quantity</th>
<th>Price</th>
<th>Ordertime</th>
<th>Orderstatus</th>
<th>Actions</th>
</tr></thead>
<tbody>
{% for row in records %}
<tr align="center">
<td>{{row.pid}}</td>
<td>{{ row.username }}</td>
<td>{{ row.email }}</td>
<td>{{ row.productname }}</td>
<td>{{ row.quantity }}</td>
<td>{{ row.price }}</td>
<td>{{ row.ordertime }}</td>
<td>{{ row.orderstatus }}</td>
<td><input type="submit" value="Accept"> <input type="button" value="Reject"></td>
</tr>
{% endfor %}
</tbody>
</table>
And This is my Views.py Page for the fetching up of the data
def viewpro(request):
con = mysql.connector.connect(user='root', password='', host='localhost', database='company1')
cur = con.cursor()
cur.execute("SELECT * FROM user_order")
records=cur.fetchall()
print(records)
con.commit()
cur.close()
con.close()
#context = {'viewpro' : user_order.objects.all()}
return render(request, "viewpro.html", {'records':records})
But Here The problem is it display the no.of table rows same like DB(It have 4 records and table shows 4 rows But no data in the fields).....Pls Help me to get rid of that....Only using raw queries....
I created a django template with the following model
Models.py
class MaterialRequest(models.Model):
owner = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='allotment_sales')
product1 = models.CharField(max_length=500,default=0,blank=True,null=True)
product1_quantity = models.IntegerField(default=0,blank=True,null=True)
product2 = models.CharField(max_length=500,default=0, blank=True,null=True)
product2_quantity = models.IntegerField(default=0,blank=True,null=True)
product3 = models.CharField(max_length=500,default=0, blank=True,null=True)
product3_quantity = models.IntegerField(default=0,blank=True,null=True)
product4 = models.CharField(max_length=500,default=0, blank=True,null=True)
product4_quantity = models.IntegerField(default=0,blank=True,null=True)
product5 = models.CharField(max_length=500,default=0, blank=True,null=True)
product5_quantity = models.IntegerField(default=0,blank=True,null=True)
product6 = models.CharField(max_length=500,default=0, blank=True,null=True)
product6_quantity = models.IntegerField(default=0,blank=True,null=True)
product7 = models.CharField(max_length=500,default=0, blank=True,null=True)
product7_quantity = models.IntegerField(default=0,blank=True,null=True)
product8 = models.CharField(max_length=500,default=0, blank=True,null=True)
product8_quantity = models.IntegerField(default=0,blank=True,null=True)
and I tried displaying this data on the template using this view
def load_salesorder(request):
so_id = request.GET.get('sales_order')
s_o = MaterialRequest.objects.filter(pk=so_id)
print("kits=========",s_o)
return render(request, 'allotment_so.html', {'sales_order': s_o})
HTML
<table class="table table-bordered">
<thead>
<tr>
<th>Product Short Codes</th>
<th>Product Quantity</th>
</tr>
</thead>
<tbody>
<div class="table-container">
{% for i in sales_order %}
<tr>
<td class="align-middle">{{ i.product1 }}</td>
<td class="align-middle">{{ i.product1_quantity }}</td>
</tr>
<tr>
<td class="align-middle">{{ i.product2 }}</td>
<td class="align-middle">{{ i.product2_quantity }}</td>
</tr>
<tr>
<td class="align-middle">{{ i.product3 }}</td>
<td class="align-middle">{{ i.product3_quantity }}</td>
</tr>
<tr>
<td class="align-middle">{{ i.product4 }}</td>
<td class="align-middle">{{ i.product4_quantity }}</td>
</tr>
<tr>
<td class="align-middle">{{ i.product5 }}</td>
<td class="align-middle">{{ i.product5_quantity }}</td>
</tr>
<tr>
<td class="align-middle">{{ i.product6 }}</td>
<td class="align-middle">{{ i.product6_quantity }}</td>
</tr>
<tr>
<td class="align-middle">{{ i.product7 }}</td>
<td class="align-middle">{{ i.product7_quantity }}</td>
</tr>
<tr>
<td class="align-middle">{{ i.product8 }}</td>
<td class="align-middle">{{ i.product8_quantity }}</td>
</tr>
{% endfor %}
</tbody>
</table>
But the problem with is that it also shows the None values of ORM whereas I just want to show the fields that consists the data and leave out the blank values. How can I fix this and also is there a better way to do this ?
You can achieve that with some if statements in the template:
{% for i in sales_order %}
{% if i.product1 and i.product1_quantity %}
<tr>
<td class="align-middle">{{ i.product1 }}</td>
<td class="align-middle">{{ i.product1_quantity }}</td>
</tr>
{% endif %}
<!-- same for other fields -->
{% endfor %}
use can use if statement for each field:
<tr>
<td class="align-middle">{% if i.product1 %}{{ i.product1 }}{% endif %}</td>
<td class="align-middle">{% if i.product1_quantity %}{{ i.product1_quantity }}{% endif %}</td>
</tr>
I am trying to display data on my web app from a CSV file using Flask. The below code reads my CSV file and assigns stocklist as the variable for my data. In the HTML code below it, using jinga logic, I iterate through stocklist however my CSV columns are returned as rows (see sample output and pic). How do I display the rows correctly?
My python function:
#app.route('/stocks')
def Stocks():
filename = 'stock_scraper - dev.csv'
data = pandas.read_csv(filename, header=0)
stocklist = list(data.values.flatten())
return render_template('stocks.html', stocklist=stocklist)
My web app for iterating through stocklist:
<table class="table table-striped table-sm">
<thead>
<tr>
<th>#</th>
<th>Ticker</th>
<th>Closing Price</th>
<th>Closing Date</th>
</tr>
</thead>
<tbody>
{% for eachstocks in stocklist%}
<tr>
<td>{{ eachstocks }}</td>
<td>{{ eachstocks }}</td>
<td>{{ eachstocks }}</td>
<td>{{ eachstocks }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Output:
Haks, i removed the nested loop and added the list position in each value to fix it. Works now.
<tbody>
{% for value in stocklist %}
<tr>
<td>{{ value[0] }}</td>
<td>{{ value[1] }}</td>
<td>{{ value[2] }}</td>
<td>{{ value[3] }}</td>
</tr>
{% endfor %}
</tbody>
output
enter image description here
You shouldn't flatten the list.
Try this:
#app.route('/stocks')
def Stocks():
filename = 'stock_scraper - dev.csv'
data = pandas.read_csv(filename, header=0)
stocklist = list(data.values)
return render_template('stocks.html', stocklist=stocklist)
Then for the Jinja template:
<table class="table table-striped table-sm">
<thead>
<tr>
<th>#</th>
<th>Ticker</th>
<th>Closing Price</th>
<th>Closing Date</th>
</tr>
</thead>
<tbody>
{% for value in stocklist%}
<tr>
<td>{{ value[0] }}</td>
<td>{{ value[1] }}</td>
<td>{{ value[2] }}</td>
<td>{{ value[3] }}</td>
</tr>
{% endfor %}
</tbody>
</table>