csv2html - with Python
If you have a list of Things that you are maintaining and using as a single source of information for various documents, why not put the list into a csv and then use Python to generate the document?
Here is how to do this for a tiny index.html
kind of webpage.
Create and maintain the .csv file
This is probably the hardest part.
- Create a
.csv
file with the relevant column headers.
For a blog, this might be date
, author
, title
, and abstract
.
- Add the information to the
.csv
file.
One blog post by row, fill the columns with the information about the blog posts that you have.
- Maintain the .csv file
Everytime a new blog post appear, add it to the .csv file.
Write a tiny Python script to generate HTML from the .csv
Here is an example I use to generate the extenso.html page. Yes, it is hard-coded. Yes, I might change it. Yes, it works.
The idea is to… :
- Import the .csv into a dataframe (using pandas), or read it line by line.
I found it easier to build with pandas because I already know how it works, feel welcome to adjust to your needs and current level of skill.
- Decide on how you want to format the file
Maybe you need every date
to be in bold, maybe you want the title
to be a <h2>
heading…
- Write a script that logs to the terminal every tag you need, with all the information.
E.g. for our .csv
file that generates an index.html
, I could have:
# first, let's go row by row and output one row
for row in dataframe.iterrows():
# we start a paragraph
print("<p>")
# we print the blog post with the following formatting
# <DATE> : <TITLE>, by <AUTHOR>. <ABSTRACT>
print(dataframe['date'] + ":" + "dataframe['title'] + ', by ' + dataframe['author'] + '.' + dataframe['abstract'])
# we could add a link with the following:
print('<a href="' + dataframe['url'] + '">' + dataframe['title'] + '</a>')
# we close the paragraph
print("</p>")
We could concatenate all the arguments of the print
s into one print
to avoid skipping line - or we could look at the Python docs to do it properly.
But remember - we are duct-taping a website tooling suite here, no need to go into all the details!
- Copy-paste the HTML log from the terminal to the
index.html
template you created earlier on
That’s it. We are done!
Potential lists to consider for this approach
I’ve used similar scripts combined with a .csv file to make TeX and HTML documents of…
- blog posts
- arts projects
- jobs
- education
Or, generally speaking, just anything that could be on a CV ?
But why ??
Learning how things are made is a great way to improve your skills.
Knowing when to reduce the scope is a nice way to learn more things.
The problem I am trying to solve with these idea is the fact that if you are blogging infrequently, or for any website really, using a framework might be more work on your part than just having a bunch of .md
files that you move onto .html
with a script.
Would I put this in production for a Major Website? No. Do I have a Major Website? Also no.
Feel welcome to adapt the extenso.py
script to your needs, and have fun focusing on the writing part, not the tooling.