Skip to content

Commit

Permalink
data update
Browse files Browse the repository at this point in the history
  • Loading branch information
paiv committed Dec 24, 2024
1 parent 209de51 commit 13f4f54
Show file tree
Hide file tree
Showing 5 changed files with 469 additions and 47 deletions.
2 changes: 0 additions & 2 deletions code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

Install dependencies
```sh
python -m venv .venv
. .venv/bin/activate
pip install -r requirements.txt
```

Expand Down
96 changes: 59 additions & 37 deletions code/genpage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import csv
import sys
import template
from datetime import datetime, UTC
from pathlib import Path
from urllib.parse import urlparse
Expand All @@ -9,21 +10,25 @@
def gen_id(row, name='id'):
code = row.get(name)
if (link := row.get('url')):
return f'<div><a href="{link}">{code}</a></div>'
return f'<div>{code}</div>'
return f'<div><a href="{link}">{code}</a></div>\n'
return f'<div>{code}</div>\n'


def gen_link(row, name='url'):
if (link := row.get(name)):
url = urlparse(link)
fn = Path(url.path).name
return f'<div><a href="{link}">{fn}</a></div>'
return '<div></div>'
return f'<div><a href="{link}">{fn}</a></div>\n'
return '<div></div>\n'


def gen_th(name):
return f'<div class="th">{name}</div>\n'


def gen_(row, name):
value = row.get(name, '')
return f'<div>{value}</div>'
return f'<div>{value}</div>\n'


def main(args):
Expand All @@ -41,61 +46,78 @@ def main(args):
else:
fout = Path(fileto).open('w')

ts = datetime.now(UTC)
timestamp = ts.isoformat()

gens = dict(id=gen_id, url=gen_link, image=gen_link, pdf=gen_link)

fieldnames = list(reader.fieldnames)
nf = len(fieldnames)
cssgc = f'repeat({nf}, 1fr)'

print(f'''<!DOCTYPE html>
<html>
def row_entries(row):
for k in fieldnames:
gen = gens.get(k, gen_)
yield gen(row, k)

def entries():
rpl = '<div>\n&entries\n</div>'
for row in reader:
yield template.format(rpl, entries=row_entries(row))

context = dict()
context['timestamp'] = datetime.now(UTC).date().isoformat()
context['ncols'] = len(fieldnames)
context['fieldnames'] = map(gen_th, fieldnames)
context['entries'] = entries()

tpl = '''\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8">
<meta name="viewport" content="width:device-width, initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>FCI Breeds</title>
<style>
:root {{ color-scheme: light dark; }}
.table {{
:root { color-scheme: light dark; --hi: #eef; }
@media (prefers-color-scheme: dark) {
:root { --hi: #433; }
}
.table {
display: grid;
gap: 0.1rem 1rem;
grid-template-columns: {cssgc};
gap: 0;
grid-template-columns: repeat(&ncols, 1fr);
white-space: nowrap;
}}
.table .th {{
}
.table .th {
font-weight: bolder;
text-align: center;
}}
.table > div {{
}
.table > div {
display: contents;
}
.table > div > div {
border-right: 1px solid #ccc;
padding-right: 0.5rem;
}}
padding: 0.1rem 1rem;
}
.table > div:hover > div {
background-color: var(--hi);
}
</style>
</head>
<body>
<div class="info">
<h1>FCI Breeds</h1>
<p><a href="https://ukrainewar.carrd.co/"><img src="StandWithUkraine.svg" alt="standwithukraine"></a></p>
<p>Data compiled from <a href="https://www.fci.be/en/nomenclature/">https://www.fci.be/en/nomenclature/</a>.</p>
<p>Generated at {timestamp}</p>
<p>Generated on &{timestamp}</p>
<p>Download: <a href="https://github.com/paiv/fci-breeds/releases/latest/download/fci-breeds.csv.tar.gz">CSV</a></p>
</div>
<div class="table">''', file=fout)

for k in fieldnames:
print(f'<div class="th">{k}</div>', file=fout)

for row in reader:
for k in fieldnames:
gen = gens.get(k, gen_)
val = gen(row, k)
print(val, file=fout)

print(f'''</div>
<div class="table">
<div>
&{fieldnames}
</div>
&{entries}
</div>
</body>
</html>''', file=fout)
</html>
'''
template.print(tpl, context, file=fout)


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions code/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
lxml
requests
git+https://github.com/paiv/template-py
Loading

0 comments on commit 13f4f54

Please sign in to comment.