I've a program which will read data from text file.
There are 3 types of data that I need to read from text file.
visual id
time
tname_pb
Problem
I have 3 columns and need to separate output into different columns.
But, visual id is working perfectly, but time and tname_pb are combined together.
Question
How can I separate time and tname_pb into two different column.
Current views.py based on the picture
with open(path) as input_data:
for line in input_data:
if line.startswith('2_visualid_'):
visual_key = line.lstrip('2_visualid_').rstrip()
data.update({visual_key: []})
if 'time' in tokens:
if search_time in line and visual_key in data:
data[visual_key].append(next(input_data).lstrip('2_mrslt_').rstrip())
if 'tname_pb' in tokens:
if tname_pb in line and visual_key in data:
data[visual_key].append(next(input_data).lstrip('2_mrslt_').rstrip())
Current template based on the picture
<div class="input-group" align="center" style=" left:10px; top:-110px; width:99%">
<table class="table table-bordered">
<thead class="success" >
<tr>
<th class="success">
<b>Visual ID</b>
</th>
<th class="success">
<b>Time Delay Index</b>
</th>
<th class="success">
<b>Tname PB</b>
</th>
</tr>
{% for key, values in output.items %}
<tr class="">
<td rowspan={{ values|length|add:1 }}><b>{{ key }}</b></td>
{% for value in values %}
<tr class="">
<td ><b>{{value}}</b></td>
</tr>
{% endfor %}
</tr>
{% endfor %}
</thead>
</table>
</div>
NOTE
I have tried few examples from Google, but I get errors with them.
I will post them here if they help clarify my question. I left them out because I don't want my question to be confusing.
You have a new table row going on here for each value.
{% for value in values %}
<tr class="">
<td><b>{{value}}</b></td>
</tr>
{% endfor %}
Instead of this, you need to make a new table cell for each value.
<table class="table table-bordered">
<thead class="success">
<tr>
<th class="success">
Visual ID
</th>
<th class="success">
Time Delay Index
</th>
<th class="success">
Tname PB
</th>
</tr>
</thead>
<tbody>
{% for key, values in output.items %}
<tr class="">
<td><b>{{ key }}</b></td>
{% for value in values %}
<td><b>{{ value }}</b></td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Related
I am trying to create a page where it shows records from the database and shows a delete button next to them. One of the elements shown is the variable needed for the delete function my problem is that I do not know how to get and pass that variable for the route that deletes the record from the database.
routes.py
#app.route("/deletetraining",methods=['GET', 'POST'])
def deleteTraining():
return render_template('deleteTraining.html', trainings = getAllTrainings(), title = 'Delete')
#app.route("/delete")
def delete(tid):
deleteTrainingByID(tid)
return redirect(url_for('deleteTraining'))
deleteTraining.html
{% extends 'layout.html' %}
{% block content %}
<h1 class="text-muted">All Trainings</h1>
<table class="table" >
<thead>
<tr>
<th scope="col">TID</th>
<th scope="col">Workplan</th>
<th scope="col">Cost Centre Code</th>
<th scope="col">Objective</th>
<th scope="col">Name</th>
<th scope="col">Location</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Hours</th>
<th scope="col">Days</th>
<th scope="col">Gender Hours</th>
<th scope="col">For MEL</th>
</tr>
</thead>
<tbody>
{% for training in trainings %}
<tr>
{% for element in training %}
<td>{{element}}</td>
{% endfor %}
<td>
<a class="btn btn-danger" type="button" href="/delete">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock content %}
In the template you must pass the id in the delete training button
<a class="btn btn-danger" type="button" href="/delete/{{training.id}}">Delete</a>
And in your #app.route you must add
#app.route("/delete/<tid>")
VIEWS
def Aeging(request):
current_date_thirty = date.today().isoformat()
days_before_thirty = (date.today() - timedelta(days=30)).isoformat()
thirty = journalVoucher.objects.values('account_name_one').order_by('account_name_one').filter(voucher_date_one__range=[days_before_thirty,current_date_thirty]).annotate(outstanding=(Sum('credit_amount_one')-Sum('debit_amount_one')))
for item in thirty:
item['account_name_one'] = CreateLedger.objects.get(id=item['account_name_one'])
return thirty
context ={'thirty':thirty}
return render(request,'report/aeging/aeging_analysis.html',context)
<div class="row">
<div class="table-responsive col-md-6">
<table id="example" class="table table-bordered">
<caption style="caption-side:top; text-align: center;"><h3>0-30 Days</h3></caption>
<thead class="thead-dark">
<tr>
{# <th>S No.</th>#}
<th>Party Name</th>
<th>Total Amount</th>
</tr>
</thead>
<tbody>
{% for item in thirty %}
<tr class="table-success">
{# <td>{{ }} </td>#}
<td>{{item.account_name_one}}</td>
<td>{{ item.outstanding }} </td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr class="totalColumn">
<td style="visibility:hidden;"></td>
<td>Total:</td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
Hi The Query Is perfectly Run on python shell but when i render the data on a template it shows too many values to unpack (expected 2).
I Provide You My Views and Template Code.
Any Solution Of This??
Images Link..
https://drive.google.com/drive/folders/1V7uI4YS4Qg8ng4bmN0aCFvp85Dzi8n5S?usp=sharing
I have the following code:
<table id="MyTable" class="highlight responsive-table" >
<thead>
<tr>
{% with columns as columns %}
{% for columns in columns %}
<th class="th-sm">{{ columns }}</th>
{% endfor %}
{% endwith %}
</tr>
</thead>
<tbody>
{% for trans in transactions %}
<tr>
<td> {{ trans.transactionsequencenumber }}</td>
<td> {{ trans.posid }}</td>
<td> {{ trans.transactionnumber }}</td>
</tr>
{% endfor %}
</table>
which works for displaying static table data, but the idea is that the columns being iterated are being set via a user preferences model, so the table columns can change depending on the user, theres a possible 90+ columns to choose from, and here is where my issue lays.
pseudo code below to help illustrate my goal:
<table id="MyTable" class="highlight responsive-table" >
<thead>
<tr>
{% with columns as columns %}
{% for columns in columns %}
<th class="th-sm">{{ columns }}</th>
{% endfor %}
{% endwith %}
</tr>
</thead>
<tbody>
{% for trans in transactions %}
<tr>
<td> {{ trans.{{columns}} }}</td>
</tr>
{% endfor %}
I've tried simple string concatenation so far, but that just prints the string to the table and not the object data,
any help and/or guidance would be greatly apprecieted.
be kind, this is my first question.
For every object that represent a line in the database attribute, you put it into a column. For example:
<tr>
<th> title </th>
<th> body </th>
</tr>
{% for post in posts %}
<tr>
<td> {{post.title}}</td>
<td> {{post.body}} </td>
</tr>
{% endfor %}
I was hoping to have alternating color for my table. However, all rows become grey after I applied the table-striped class. I tried to load both v3 and v4 boostrap css files. And it still didn't work.
html
<table id="maxDiversificationTable" class="investmentTable table table-striped table-bordered table-hover table-fit" style="margin-top:-55%" >
<thead>
<tr style="color:#337AC7" >
<th >Tickers</th>
<th >Current Weight</th>
<th >New Weight</th>
<th >Conviction</th>
</tr>
</thead>
{% for tableData in dataSet %}
<tbody>
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
</tbody>
{% endfor %}
</table>
My guess is that your <tbody> which is also inside your for loop. So, your table is rendered like this:
<tbody>
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
</tbody>
<tbody>
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
</tbody>
Which is not what you want. What you want is the following:
<tbody>
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
</tbody>
So, try taking tbody out of the for loop and see if it works:
<tbody>
{% for tableData in dataSet %}
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
{% endfor %}
</tbody>
Hope it helps!
The table-striped class is defined in Bootstrap 4's SCSS as follows:
.table-striped {
tbody tr:nth-of-type(odd) {
background-color: $table-bg-accent;
}
}
So, in essence, the $table-bg-accent color will be applied to every odd row (tr) in every table body (tbody) element. Since you're creating a new table body for every row, every row will have the accent color applied.
To fix, don't create a new tbody for every row:
<thead>
<tr style="color:#337AC7">
<th>Tickers</th>
<th>Current Weight</th>
<th>New Weight</th>
<th>Conviction</th>
</tr>
</thead>
<tbody>
{% for tableData in dataSet %}
<tr>
<td>{{tableData.tickers}}</td>
<td>{{tableData.currentWeight}}</td>
<td>{{tableData.newWeight}}</td>
<td>{{tableData.conviction}}</td>
</tr>
{% endfor %}
</tbody>
I have lots of code like this in my template:
<h2>Owners of old cars</h2>
<table id="hor-minimalist-b" summary="Old Cars">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
{% for result in old_cars %}
<tr>
<td>{{result.name}}</td>
<td>{{result.age}}</td>
<td>{{result.address}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h2>Owners of Ford cars</h2>
<table id="hor-minimalist-b" summary="Old Cars">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
{% for result in ford_cars %}
<tr>
<td>{{result.name}}</td>
<td>{{result.age}}</td>
<td>{{result.address}}</td>
</tr>
{% endfor %}
</tbody>
</table>
There will be lots more tables created like those above. As you can see there is lots of duplicate code. Is there anyway to do this so I don't repeat as much code each time a create a table? Thanks.
UPDATE
I'm thinking about creating a new object, called table, and adding a result and the corresponding h2 title to it. I can then create a list of these table objects, called tables, which I can pass to the template. The template can then iterate over them.
Surely what you want, ideally, is:
{% for car in cars %}
<h2>Owners of {{ car.manufacturer }}</h2>
<table id="hor-minimalist-b" summary="Old Cars">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
{% for model in car.models %}
<tr>
<td>{{model.name}}</td>
<td>{{model.age}}</td>
<td>{{model.address}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
I'm not sure what ORM you're using, but in Django that wouldn't be too hard to construct; after all, all you pass in is a list of objects with subobjects and fields.
Again, I can only talk for a django perspective (construct from your own ORM), but you can either deliberately construct the dictionary with explicit requests for each type of manufacturer, or rather have a model for "sales" and a model for "manufacturer" and construct a many-to-many relationship between the two. Your query roughly looks like this, in django python:
result = []
manufacturers = Manufacturer.objects.all() # get all manuf
for m in manufacturer:
mf = dict()
mf["manufacturer"] = m.name
mf["models"] = m.model_set.all() # get all linked models
result.append(mf)
# pass result to template as cars
Does that make sense?
Use template for the header and footer of the table
<table id="hor-minimalist-b" summary="Old Cars">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
Put this in a file named "table_header.html" and include it in your template.
The same for the footer!