Import CSV to Django, save data and show results within template - python-3.x

I need your help with uploading data(CSV) to django app and save data to database. Also i want to show row wise results within the template. Attached is the page image for your reference. Please guide how can i implement the same.image

I had worked in some similar project similar, recive a csv and upload the data in different databases.
I used pandas to deal with different types of user friendly tabular extensions, but if you gonna work only with csv you can use the csv lib from python.
you can simply put some form for upload the csv to allow the user to send the file to django application.
these example we used this to catch a xlsx file and then transform in a dataframe(using pandas) to be easy to deal.
archiveXLSX = request.FILES['xlsx_file']
df = pd.read_excel(archiveXLSX,keep_default_na=False)
now you have the data inside the django backends and need transform these data and make shure who these data are Ok to load in the database.
after all verification we put the data in a list(without save this).
list_example = [
ExampleModel(data1,data2,data3),
ExampleModel(data1,data2,data3),
]
To upload to database, is interesting insert the data using bulk_create(to avoid execute multiples inserts) bulk_create can do it more fast than create one per one .
Ex:
ExampleModel.objects.using('database').bulk_create(list_example)
so if you want show these data in a template i recommend you to create some table in the database who will indicate the date of upload, file that user uploaded and the id of the data upload from these file. ex:
id, file_name , date , id_data
1 ,example.csv,20 - 04 - 2020, 2
2 ,example.csv,20 - 04 - 2020, 4
3 ,example.csv,20 - 04 - 2020, 2
Now. To deal whit these two models you need to create the csv model objects while create the model objects for the data from csv (make sure you will save first the model of the Data from csv in these case is ExampleModel).
Let's suppose who after verify these data, processing and these common things. You put them in some dictionary, the function to save them can be like this:
csvDataToCreate = []
dataToCreate = []
for data in dataDict:
example = ExampleModel(data['data1'],data['data2'],data['data3'])
dataToCreate.append(example)
csvData = ExampleCsvModel(filename, datetime.now().date(), example)
csvDataToCreate.append(csvData)
now you have all the information you need.
The view for csvData can be a simple select.
csvData.objects.filter(file_name='file.csv',date='xx-xx-xxxx')
and the template is simple too:
<table class="table table-striped">
<thead>
<tr>
<th scope="col" style="width: 35%;">data1</th>
<th scope="col" style="width: 45%;">data2</th>
<th scope="col" style="width: 5%;">data3</th>
</tr>
</thead>
<tbody>
{% for csv in csvitens %}
<tr>
<td style="width: 35%;"><div class='btn'>{{csv.id_data.data1}}</div></td>
<td style="width: 35%;"><div class='btn'>{{csv.id_data.data2}}</div></td>
<td style="width: 35%;"><div class='btn'>{{csv.id_data.data3}}</div></td>
<tr>
{% endfor %}
</tbody>
</table>
for this task you can use the css framework of your choice, here i ised boostrap4 .

Related

Hiding unfilled fields in pdf/html templates via source code

I'd like to make certain fields invisible/disappear when remain unfilled.
i.e. Email:
This will result in the email text field not being printed out
Here is an example of how I do that. It's a custom field, on mine, but the logic is the same. The ?has_content piece is what tests for data in the field.
<#if record.custbody_bill_to_email?has_content>
<tr>
<td style="width: 147px;"><span style="font-size:10px;"><strong>Email</strong></span></td>
<td style="width: 175px;"><span style="font-size:11px;">${record.custbody_bill_to_email}</span></td>
</tr>
</#if>

Create Pyspark Dataframe from Key Value Pair log file with variable contents

I have a log file in key=value pair format and would like to read the contents into an rdd, process the rdd into a data frame, and perform aggregations/analysis with spark SQL. I can read the raw data to rdd but I haven't been able to find an example of how to process key value pairs into a tabular format.
To complicate matters, the log can and does have missing key value pairs, so the format is variable. I would hope to be able to get around this by having NULL values in rows where that 'column'/key=value is missing once processed to data frame.
Below is an example of the log :
"Date"="2017-07-11T15:55:07-07:00","recordType"="ap_data","apName"="ap1","numClients"="5","version"="2.1"
"Date"="2017-07-11T15:55:07-07:00","recordType"="ap_data","apName"="ap2","numClients"="4","version"="2.1"
"Date"="2017-07-11T15:55:07-07:00","recordType"="ap_data","apName"="ap3","version"="2.1"
Notice the third event is missing the "numClients" key-value pair.
All I've managed to do so far is read the raw content to RDD:
#Initializing PySpark
from pyspark import SparkContext, SparkConf
from pyspark.context import SparkContext
from pyspark.sql.types import Row
sc = SparkContext.getOrCreate()
# Read raw contents to a new RDD and print first 2 results
raw_data = sc.textFile("log_sample.log")
raw_data.take(2)
Kindly please provide some help with reading key-value pair formatted data and processing to tabular format. Else, if this is not the right approach, I'm open to suggestion(s). Thank you!
Below is the data frame structure I hope to produce:
EDIT: Apologies, for clarity I'm not trying to produce any HTML, just wanted to show an example of tabular result, not sure why the html is showing and not just rendering the table.
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-yw4l{vertical-align:top}
</style>
<table class="tg">
<tr>
<th class="tg-yw4l">Date</th>
<th class="tg-yw4l">recordType</th>
<th class="tg-yw4l">apName</th>
<th class="tg-yw4l">numClients</th>
<th class="tg-yw4l">version</th>
</tr>
<tr>
<td class="tg-yw4l">2017-07-11T15:55:07-07:00</td>
<td class="tg-yw4l">ap_data</td>
<td class="tg-yw4l">ap1</td>
<td class="tg-yw4l">5</td>
<td class="tg-yw4l">2.1</td>
</tr>
<tr>
<td class="tg-yw4l">2017-07-11T15:55:07-07:00</td>
<td class="tg-yw4l">ap_data</td>
<td class="tg-yw4l">ap2</td>
<td class="tg-yw4l">4</td>
<td class="tg-yw4l">2.1</td>
</tr>
<tr>
<td class="tg-yw4l">2017-07-11T15:55:07-07:00</td>
<td class="tg-yw4l">ap_data</td>
<td class="tg-yw4l">ap3</td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l">2.1</td>
</tr>
</table>

xpages dijit.form.checkbox multiple values

Using xpages on domino 8.5.3 server.
Can a djcheckbox be use with muiltiple value field similar to a checkboxgroup ?
if so, would it be possible to supply a code snippet.
Thanks
dijit.form.CheckBox can deal with only one value and that's true for djCheckBox too as it's based on dijit.form.CheckBox.
You could combine several djCheckBox controls and let it look like a checkBoxGroup. Bind every djCheckBox to a viewScope variable initialized by a document item and write values back at document save.
Here is an example for UI similarity to checkBoxGroup:
<fieldset
class="xspCheckBox">
<table>
<tbody>
<tr>
<td>
<xe:djCheckBox
label="abcdefg"
id="djCheckBox4"
value="#{viewScope.abcdefg}">
</xe:djCheckBox>
</td>
<td>
<xe:djCheckBox
label="hijklmno"
id="djCheckBox5"
value="#{viewScope.hijklmno}">
</xe:djCheckBox>
</td>
<td>
<xe:djCheckBox
label="pqrstuvwxyz"
id="djCheckBox6"
value="#{viewScope.pqrstuvwxyz}">
</xe:djCheckBox>
</td>
</tr>
</tbody>
</table>
</fieldset>
I am not sure though what's the reason for your question and if it's worth the extra effort.

Adding new xElement after ALL found Descendants

I have an xDocument with multiple various xElements.
I can successfully find a specific xElement by searching via it's xAttributes & then Add a new xElement after it using the code below:
xDocument.Descendants("td").LastOrDefault(e => ((string)e.Attribute("ID")) == "3").Add(new XElement("b", "Just a test."));
The problem is that I wish to Add this new xElement after all found instances of the Descendants, not just LastOrDefault or FirstOrDefault.
My xDocument is created dynamically & there is no way before hand to know how many 'td' xElements with 'ID' = '3' that there are going to be.
Any help would be appreciated.
Thanks
ADDED CODE AS REQUESTED
<html> .... etc....
<body>
<table>
<tr>
<td>Image</td>
<td>Description</td>
<td>Date</td>
</tr>
<tr>
<td ID="1">*.jpg</td>
<td ID="2">some image</td>
<td ID="3">01/01/1901</td> <--CHANGING THIS PART OF CODE-->
<--THIS TABLE ROW REPEATS AN UNDETERMINED NUMBER
OF TIMES RELATING TO THE NUMBER OF FILES CONTAINED IN WHATEVER DIRECTORY IS BEING SEARCHED USING A FOREACH LOOP IN ANOTHER PART OF
THE CODE-->
</tr>
</table>
</body>
</html>
So I am trying to add a tag between the <td> with ID = 3.
This <b> tag also contains a string variable i.e.
new xElement("b", DateTaken)
& needs to be created at runtime and not hard coded as it relates to each loaded image at the start of the table row.
So I am trying to add this <b> tag to every occurrence of <td> with ID=3 & not just the first or the last.
Hope this extra info helps.

Watir: How to access a table without an ID or NAME

I am trying to write my watir script to grab the following data (the table body headers and the table row data, but I am having trouble trying to figure out how to access the table. (Once I get that, teh rest is a piece of cake).
Can anyone come up with something that will help me access the table? It doesn't have a name or an ID...
<div id="income">
<table class="tHe" cellspacing="0">
<thead>
<tr>
<th id="companyLabel" class="tFirst" style="width:30%"> Quarter Ending </th>
<th id="201004" class="tFirst right">Apr 10 </th>
<th id="201001" class="tFirst right">Jan 10 </th>
<th id="200910" class="tFirst right">Oct 09 </th>
<th id="200907" class="tFirst right">Jul 09 </th>
<th id="200904" class="tFirst right">Apr 09 </th>
</tr>
</thead>
<tbody id="revenueBody">
<tr>
<td class="indtr">Totals</dfn></td>
<td class="right">2849.00</td>
<td class="right">3177.00</td>
<td class="right">5950.00</td>
<td class="right">4451.00</td>
<td class="right">3351.00</td>
</tr>
...
ie.table(:class=>'tHe') should work if there's no other tables with the same class name
ie.table(:after?, ie.div(:id, 'income')) should work if there's no other div with id 'income'
or ie.table(:index=>0) - you would need to check your page to see what the correct index value for your table is.
But wait, there is more! :)
browser.div(:id => "income").table(:class => 'tHe')
browser.div(:id => "income").table(:index => 1)
...
There is also XPath if you are stuck.
If you fire up the page and access it through Firebug or your browser's native developer tools, you can find the xpath expression for the table and then plug that into the Watir API call.
I think it was in later versions of Watir 1.5.x that support for advanced page querying came in (basically your problem, where there are no ID tags). This page on the watir wiki should help:
Ways Available To Identify HTML Element

Resources