First of all let me tell you that I'm a beginner at this, I'm on the final project of CS50x. My project consists in a webpage that lets you add some weights into a db table, and then it displays those weights and shows you the weight gain/loss. I'm trying to show the results of a query in a table rendered in html using jinja (and python). RP is the identifier(you search for the rp). The desired output is something like this:
[Desired output]
My python code is the following:
#app.route("/weightquery", methods=["GET", "POST"])
#login_required
def weightquery():
if request.method == "POST":
weights = db.execute("SELECT rp, weight, date FROM weights WHERE rp=:rp AND sex=:sex AND user_id=:user_id ORDER BY date DESC",
rp=request.form.get("rp"), sex=request.form.get("sex"), user_id=session["user_id"])
gains = db.execute("SELECT weight FROM weights WHERE rp=:rp AND sex=:sex AND user_id=:user_id ORDER BY date DESC",
rp=request.form.get("rp"), sex=request.form.get("sex"), user_id=session["user_id"])
animal = request.form.get("rp")
for i in range(len(gains)):
for weight in gains[i]:
if i >= 0 and i < (len(gains)-1):
dif= gains[i][weight] - gains[i + 1][weight]
# Store the dif somewhere I can access.
gains[i].update({'weight': dif})
# Since the dif will always have one item less, I make sure to delete the last item.
gains[i].popitem()
return render_template("weightqueried.html", weights=weights, gains=gains, animal=animal, dif=dif)
else:
return render_template("weightquery.html")
My Html template for weightqueried.html is:
{% block main %}
<div class="container">
<h3>{{ animal }}'s information</h3>
<table class="table table-striped">
<thead>
<tr>
<th>Date</th>
<th>Weight(kg)</th>
<th>Weight dif(kg)</th>
</tr>
</thead>
<tbody>
{% for rp in weights %}
<tr>
<td>{{ rp['date'] }}</td>
<td>{{ rp['weight'] }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
</tfoot>
</div>
{% endblock %}
Any tips and pointers are greatly appreciated since I'm trying to learn and right now my brain is fried!
Maybe it can be simplified if gains was removed entirely. Iterate over weights, do the dif calculation, and add the result to each dictionary. Then in the template, add a <td> element for rp['dif']. If I understand the problem correctly.....
Related
I am having trouble getting the code to properly display the portfolio in index.html.
My logic with this function is to get a list of all the stocks and cash one user has, and then in a "for" loop, look up the company name and current price for each stock, the total value, and then insert all of that information into a new list of dictionaries (display_portfolio). Then the render_template should display "display_portfolio" with this information, as well as the user's total cash and total value of everything. However, with my current setup, it is displaying the total cash and grand total, but nothing from the individual stocks. I am really not sure why that is, and I am unsure if the issue is in my html or in the flask function itself.
This is the function:
#app.route("/")
#login_required
def index():
"""Show portfolio of stocks"""
# Retrive portfolio
portfolio = db.execute("SELECT symbol, SUM(amount) as amount FROM purchases WHERE id = ? ORDER BY symbol", (session["user_id"]))
user_cash = db.execute("SELECT cash FROM users WHERE id = ?", session["user_id"])
cash = user_cash[0]["cash"]
display_portfolio = []
shares_total = 0
# loop through portfolio symbols to access name and share price for each symbol
for row in portfolio:
requested_quote = lookup(row["symbol"])
symbol = row["symbol"]
amount = row["amount"] #shares
price = float(requested_quote["price"])
name = requested_quote["name"]
share_total = (float(amount) * price)
shares_total = shares_total + share_total
display_portfolio.append({'symbol':symbol, 'name':name, 'shares':amount, 'price':price, 'share_total':share_total})
grand_total = shares_total + cash
return render_template("index.html", display_portfolio = display_portfolio, cash = cash, grand_total = grand_total)
This is index.html:
{% extends "layout.html" %}
{% block title %}
Portfolio
{% endblock %}
{% block main %}
<div>
<table class="table table-striped">
<thead>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Shares</th>
<th>Price</th>
<th>TOTAL</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3"></td>
<td>TOTAL</td>
<td>{{ grand_total | usd }}</td>
</tr>
</tfoot>
<tbody>
{% for row in display_portfolio %}
<tr>
<td>{{ display_portfolio.symbol }}</td>
<td>{{ display_portfolio.name }}</td>
<td>{{ display_portfolio.shares }}</td>
<td>{{ display_portfolio.price }}</td>
<td>{{ display_portfolio.total }}</td>
</tr>
{% endfor %}
<td colspan="3"></td>
<td>Cash</td>
<td>{{ cash | usd }}</td>
</tbody>
</table>
</div>
{% endblock %}
I should also note, that when I add "| usd" to "display_portfolio.price" in that it reads:
<td>{{ display_portfolio.price | usd }}</td>
I am also getting a completely separate error, and not sure why it would work with cash and not this.
I can confirm that there exists purchases in the sql database the "portfolio" variable is retrieving.
This is what it looks like:
Display
Any help will be appreciated, thanks!
From MDN doc on tfoot:
Permitted parents: A <table> element. The <tfoot> must appear after
any <caption>, <colgroup>, <thead>, <tbody>, or <tr> element. Note
that this is the requirement as of HTML5.
Suspect the Cash line is displayed because it is missing <tr> tags.
Nu HTML Validator from W3C is your friend :)
I am completely stuck in making index function work in cs50 finance! This function should return in a html page a table with the details of the transactions made online (details are saved in a database). But it's not working: even if there is a transaction in my database, my function doesn't find it, the table is empty.
this is my code:
def index():
"""Show portfolio of stocks"""
rows = db.execute("SELECT symbol, price, shares FROM transactions WHERE id = :user_id", user_id=session["user_id"])
transactions_info = []
total_worth = 0
for transaction_info in rows:
symbol = rows [0]["symbol"]
shares = rows [0]["shares"]
price = lookup(symbol)
worth = shares * price ["price"]
total_worth += worth
transactions_info.append(transaction_info)
return render_template("index.html", rows=rows, transactions_info=transactions_info)
And this is my HTML page:
{% extends "layout.html" %}
{% block title %}
Index
{% endblock %}
{% block main %}
<table class="table table-striped" style="width:100%">
<tr>
<th>Symbol</th>
<th>Company</th>
<th>Shares</th>
<th>Price</th>
<th>TOTAL</th>
</tr>
{% for transaction in transactions %}
<tr>
<td>{{ transaction_info.symbol }}</td>
<td>{{ transaction_info.name }}</td>
<td>{{ transaction_info.shares }}</td>
<td>{{ transaction_info.price }}</td>
<td>{{ transaction_info.worth }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
Thanks for your help!
In index() you are sending a list called transactions_info here
return render_template("index.html", rows=rows, transactions_info=transactions_info)
In the html you are looping over a list called transactions here {% for transaction in transactions %}.
My app is currently making over 1000 SQL queries and taking around 20s to load the page. I can't seem to find a way to come up with a solution to get the same data displayed on a table in my template faster. I wan't to display 100 results so that's way my pagination is set to 100.
These are the methods in my my models.py used to get the count and sum of the orders, these two are in my Company model and the get_order_count is also in my Contact model
def get_order_count(self):
orders = 0
for order in self.orders.all():
orders += 1
return orders
def get_order_sum(self):
total_sum = 0
for contact in self.contacts.all():
for order in contact.orders.all():
total_sum += order.total
return total_sum
views.py
class IndexView(ListView):
template_name = "mailer/index.html"
model = Company
paginate_by = 100
template
{% for company in company_list %}
<tr id="company-row">
<th id="company-name" scope="row">{{ company.name }}</th>
<td>{{ company.get_order_count }}</td>
<td id="order-sum">{{ company.get_order_sum|floatformat:2 }}</td>
<td class="text-center">
<input type="checkbox" name="select{{company.pk}}" id="">
</td>
</tr>
{% for contact in company.contacts.all %}
<tr id="contact-row">
<th scope="row"> </th>
<td>{{ contact.first_name }} {{ contact.last_name }}</td>
<td id="contact-orders">
Orders: {{ contact.get_order_count }}
</td>
<td></td>
</tr>
{% endfor %}
{% endfor %}
Your two methods are very inefficient. You can replace them with annotations which calculate everything in one query in the database. You haven't shown your models, but it would be something like:
class IndexView(ListView):
template_name = "mailer/index.html"
model = Company.objects.annotate(order_count=Count('orders')).annotate(order_sum=Sum('contacts__orders__total'))
paginate_by = 100
Now you can access {{ company.order_count }} and {{ company.order_sum }}.
I have this views function to fetch userid and accountnumber from AWS Dynamodb :
def dsctbl2(request):
dynamodb=boto3.client('dynamodb', region_name='us-west-2')
response = dynamodb.scan(
TableName='User-Account')
filtered = response['Items']
length = len(filtered)
for k in range(length):
accnum = filtered[k]['AccountNum']['S']
uid = filtered[k]['UserId']['S']
f = dict(AccountNum=accnum,userID=uid)
rows = list(f.items())
return render('useradd.html',{'rows': rows})
I have tried almost everything but the rows value is just not getting passed to my template. I even tried passing a simple string value and even that is not getting passed. This is my template table where I wish to display the userid and accountnum.
<div class="mytable">
<table>
<thead>
<tr>
<th>Account #</th>
<th>User Id</th>
</tr>
</thead>
<tbody>
{% for row in rows %}
<tr>
<td>{{ row.AccountNum }}</td>
<td>{{ row.UserId }}</td>
</tr>
{% endfor %}
</table>
</div>
When I hit the template , nothing shows up. No values are getting displayed except the table headings. Why is render not passing the context variable (list value)from my views to template ? I have been stuck with for about 6 hours ! Can someone resolve this issue ?
return render(request,'useradd.html',{'rows': rows})
you need to pass request as first parameter,
I defined a route and template to edit data. Instead, it creates new data. How do I fix this?
#home_blueprint.route('/table/<int:data_id>/edit', methods=['GET', 'POST'])
def edit(data_id):
ed_form = EditTableForm(request.form)
data_to_edit = db.session.query(Diary).filter_by(id=data_id)
if ed_form.validate_on_submit():
if ed_form.title.data:
data_to_edit.title = ed_form.title.data
if ed_form.weight.data:
data_to_edit.weight = ed_form.weight.data
if ed_form.kkal.data:
data_to_edit.kkal = ed_form.kkal.data
if ed_form.carbs.data:
data_to_edit.carbs = ed_form.carbs.data
if ed_form.proteins.data:
data_to_edit.proteins = ed_form.proteins.data
if ed_form.fats.data:
data_to_edit.fats = ed_form.fats.data
db.session.add(data_to_edit)
db.session.commit()
flash('New data was successfully posted. Thanks.')
return redirect(url_for('home.table'))
else:
return render_template('edit.html', data_to_edit=data_to_edit, ed_form=ed_form, data_id=data_id)
return render_template("edit.html", diary=diary)
<table class="table table-bordered">
<thead>
<tr>
<th>Product</th>
<th>Weith</th>
<th>Kkal</th>
<th>Protein</th>
<th>Fat</th>
<th>Carbs</th>
</tr>
</thead>
<tbody>
{% for data in data_to_edit %}
<tr>
<form class="form-message" role="form" method="post" action="/table">
{{ ed_form.csrf_token }}
<td>{{ed_form.title(placeholder=data.title)}}</td>
<td>{{ed_form.weight(placeholder=data.weight)}}</td>
<td>{{ed_form.kkal(placeholder=data.kkal)}}</td>
<td>{{ed_form.carbs(placeholder=data.carbs)}}</td>
<td>{{ed_form.proteins(placeholder=data.proteins)}}</td>
<td>{{ed_form.fats(placeholder=data.fats)}}</td>
<td><button class="btn btn-sm btn-success" type="submit">Post</button></td>
</form>
</tr>
{% endfor %}
</tbody>
</table>
Link to projects Git repository. Each users input into table has own id, I query data by id and then trying to Edit data there with no luck so far :)
UPDATE
I have changed code to:
data_to_edit = db.session.query(Diary).filter_by(id=data_id).first()
if ed_form.validate_on_submit():
if not data_to_edit:
data_to_edit = Diary(
ed_form.title.data,
ed_form.weight.data,
ed_form.kkal.data,
ed_form.carbs.data,
ed_form.proteins.data,
ed_form.fats.data,
current_user.id
)
db.session.add(data_to_edit)
db.session.commit()
But yet no luck, it doesn't change old data but ads new.
The issue is that you are adding the object again in session, thus inserting it into the database.
Sample code
data_to_edit = db.session.query(Diary).filter_by(Diary.id=data_id).first()
if not data_to_edit:
data_to_edit = Diary()
db.session.add(data_to_edit)
...
# edit properties
...
db.session.commit()
The idea is to add the new object only when it's actually new.