Onehot-NL / onehot.nl

Code for our data science meetup
0 stars 0 forks source link

Generate HTML pages for entries in events.yaml #13

Closed RCdeWit closed 3 years ago

RCdeWit commented 3 years ago

We should have a webpage for every event we host. I propose the following path:

https://onehot.nl/events/2021-05-18/a-world-in-transition

Ideally we automatically generate these pages based on the contents of events.yaml.

duijf commented 3 years ago

I've just added deserialization and validation of the events.yaml file in a series of commits:

The default error messages weren't great. I had to add a custom pretty printer for their exceptions to make things usable: https://github.com/duijf/onehot/commit/d5ab4e95031f7368d651f71db1181737da4af400

If you make a typo in the events.yaml file, you now get a (pretty good IMO) error message detailing what's wrong:

$ build site
Building..
events.yaml: ERROR reading state at:

[{'title': None,
  'subtitle': None,
  'speaker': {...},
  'abstract': None,
  'keywords': None},
 {'title': "Will my neighbour's Tesla consume all my power?",
  'subtitle': 'Managing the energy transition at Alliander',
  'speakr': {...},
  'abstract': 'The world is moving fast towards more sustainable energy, but '
              'it is difficult for grid operators to keep up! Do they even '
              'know how prepared they are for the transition and can they '
              'mitigate potential failures of the grid while the necessary '
              'infrastructure is not yet finished? In this talk we will look '
              'at some examples of how Alliander, one of the largests grid '
              'operators in the Netherlands, takes on some of the challenges '
              'of the energy transition. We will start by looking at how data '
              'can give better insight into the scope of these challenges. '
              'Afterwards we will look at how a smart grid or a smart customer '
              'can already mitigate a lot of these challenges without laying a '
              'single new cable in the ground.',
  'keywords': 'Energy transition, Smart grid'}]

Missing field 'speaker'

Expected schema:

 - title: <class 'str'>
 - subtitle: <class 'str'>
 - speaker: <class '__main__.Speaker'>
 - abstract: <class 'str'>
 - keywords: <class 'str'>

If we didn't have this additional schema validation, you would get a Python stacktrace from the bowels of Jinja2 while rendering some template, so this is a lot better :smile:

I also renamed some keys in the schema for consistency in https://github.com/duijf/onehot/commit/b50dab2a8d6b5f2cb3aef603fd7523747b5d7d65

Still to be done:

After this is done, we can get started on the HTML + design side of the event pages.

duijf commented 3 years ago

Update: I got fed up with mashumaro and went with Pydantic instead.

Reasons: It didn't actually perform any parsing / validation / type conversion beyond checking whether the correct keys in the YAML were present. That meant we would end up with None or ints where the Python type annotation is a str. That was getting confusing. Pydantic does not have this problem.

These have been implemented now:

Update web/pages/index.html to use the new schema. Add a template for individual events and wire up the building code so we create pages for them.

duijf commented 3 years ago

Update: the Jinja error messages were pretty poor as well. Those have been improved now.

I think we're ready to start working on the HTML + CSS now.

Example error message in events.yaml:

$ build site
Building..
events.yaml: Problems found
1 validation error for State
events -> 0 -> talks -> 0 -> speaker -> name
  field required (type=value_error.missing)

Example error message in template:

Building..
Traceback (most recent call last):
  File "/home/duijf/repos/duijf/onehot/web/pages/code-of-conduct.html", line 4, in top-level template code
    {% extends "layout.html" %}
  File "/home/duijf/repos/duijf/onehot/web/templates/layout.html", line 35, in top-level template code
    {% block header %}
  File "/home/duijf/repos/duijf/onehot/web/pages/code-of-conduct.html", line 9, in block "header"
    {{ asdf }}
jinja2.exceptions.UndefinedError: 'asdf' is undefined
duijf commented 3 years ago

You should be able to get started on the HTML + styling now @RCdeWit !

Also: be sure to update your virtualenv again, I changed some stuff about the requirements.