Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
apt_exercises
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andreas Fischer
apt_exercises
Commits
f84b1a58
Commit
f84b1a58
authored
4 years ago
by
Andreas Fischer
Browse files
Options
Downloads
Patches
Plain Diff
Added program exercise and HTML template
parents
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
html_writer.py
+77
-0
77 additions, 0 deletions
html_writer.py
template.html
+23
-0
23 additions, 0 deletions
template.html
with
100 additions
and
0 deletions
html_writer.py
0 → 100644
+
77
−
0
View file @
f84b1a58
#!/usr/bin/env python3
import
csv
def
read_html
():
"""
Read the HTML template file into a string
"""
with
open
(
"
template.html
"
)
as
htmlfile
:
htmlstring
=
htmlfile
.
read
()
return
htmlstring
def
read_csv
(
filename
):
"""
Read a csv file and produce a list of lists with each
inner list representing a row in the csv file:
[ [first, last, email], [first, last, email], ...]
"""
result
=
[]
with
open
(
filename
)
as
csvfile
:
### EDIT BELOW HERE ###
reader
=
csv
.
reader
(
csvfile
,
delimiter
=
"
;
"
)
pass
### EDIT ABOVE HERE ###
return
result
def
csv_to_html
(
csvdata
):
"""
Expects csvdata to be a list of lists:
[ [first, last, email], [first, last, email], ...]
Returns a string with the HTML <tbody> contents for
each row in the csv file:
<tr><td>First name</td><td>Last name</td><td>E-Mail</td></tr>
"
"""
htmlstring
=
""
### EDIT BELOW HERE ###
htmlline
=
"
<tr><td>{}</td><td>{}</td><td>{}</td></tr>
\n
"
pass
### EDIT ABOVE HERE ###
return
htmlstring
def
combine_template_with_data
(
template_string
,
htmldata
):
"""
Combine the HTML template string with the generated
tabular data.
"""
### EDIT BELOW HERE ###
return
""
### EDIT ABOVE HERE ###
def
write_html
(
htmlstring
):
"""
Write the resulting HTML file
"""
with
open
(
"
result.html
"
,
"
w
"
)
as
htmlfile
:
htmlfile
.
write
(
htmlstring
)
def
main
():
"""
This is the main function of the program
"""
template_string
=
read_html
()
csvdata
=
read_csv
(
"
test.csv
"
)
htmldata
=
csv_to_html
(
csvdata
)
fullstring
=
combine_template_with_data
(
template_string
,
htmldata
)
write_html
(
fullstring
)
if
__name__
==
"
__main__
"
:
main
()
This diff is collapsed.
Click to expand it.
template.html
0 → 100644
+
23
−
0
View file @
f84b1a58
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<title>
My formatted table
</title>
<meta
charset=
"UTF-8"
>
<link
rel=
"stylesheet"
href=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
>
</head>
<body>
<div
class=
"container"
>
<h1
class=
"text-center"
>
My formatted table
</h1>
<hr
/>
<table
class=
"table"
>
<thead>
<tr><th>
First name
</th><th>
Last name
</th><th>
E-Mail
</th></tr>
</thead>
<tbody>
{}
</tbody>
</table>
<hr
/>
</div>
</body>
</html>
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment