|
| 1 | +# Bulma and Jinja2 |
| 2 | + |
| 3 | +Let's start moving towards the goal of providing attractive examples. |
| 4 | +Each example will appear in several "targets", primarily a website like |
| 5 | +the [existing examples](https://pyscript.net/examples/). |
| 6 | + |
| 7 | +In this step, we'll start building the PSC website. |
| 8 | +We will *not* in this step, though, tackle any concept of a build step, nor anything beyond the homepage. |
| 9 | + |
| 10 | +Big ideas: use off-the-shelf CSS framework, static generation, dead-simple web tech. |
| 11 | + |
| 12 | +## Why Bulma? |
| 13 | + |
| 14 | +We're making a website -- the PyScript Collective. |
| 15 | +But we're also making a web app -- the PyScript Gallery. |
| 16 | +As it turns out, we're also shipping a PyPI package -- the PyScript Gallery, aka `psga`. |
| 17 | + |
| 18 | +We need a nice-looking web app. |
| 19 | +Since we're not designers, let's use a popular, off-the-shelf CSS framework. |
| 20 | +I have experience with (and faith in) [Bulma](https://bulma.io): it's attractive out-of-the-box, mature, and strikes the |
| 21 | +right balance. |
| 22 | + |
| 23 | +## Bring In Bulma |
| 24 | + |
| 25 | +The tests are in good shape and having `beautifulsoup` will prove...beautiful. |
| 26 | +We start with a failing test, one using `BeautifulSoup` to fetch the `link[rel="stylesheet"]` node, then fetch the URL pointed to there. |
| 27 | + |
| 28 | +To make it pass, I: |
| 29 | + |
| 30 | +- Downloaded the bits into static |
| 31 | +- Added a `<link rel="stylesheet" href="/static/bulma.min.css" />` to the home page. |
| 32 | + |
| 33 | +I also added the other parts of the Bulma starter (doctype, meta.) |
| 34 | +If we open it up in the browser, it looks a bit different. |
| 35 | + |
| 36 | +## Navbar and Footer |
| 37 | + |
| 38 | +We'd like a common navbar on all our pages. |
| 39 | +Bulma [has a navbar](https://bulma.io/documentation/components/navbar/). |
| 40 | +This also means a download of the PyScript SVG logo, which we'll write a test for. |
| 41 | + |
| 42 | +Ditto for a -- for now, *very* simple -- [footer component](https://bulma.io/documentation/layout/footer/). |
| 43 | + |
| 44 | +## Body |
| 45 | + |
| 46 | +Bulma makes use of `<section>`, `<footer>`, etc. |
| 47 | +Let's put the "main" part of our page into a `<main class="section">` tag. |
| 48 | + |
| 49 | +For the failing test, we'll simply look to see there is a `<main>`. |
| 50 | +But, as this is no longer a static asset, we'll put this in `test_app.test_homepage`. |
| 51 | + |
| 52 | +## Templating |
| 53 | + |
| 54 | +It sucks to repeat the layout across every static HTML file. |
| 55 | +Let's make some Jinja2 templates, then [setup Starlette](https://www.starlette.io/templates/) to use it the templates. |
| 56 | + |
| 57 | +As precursor, install Jinja2 as a dependency. |
| 58 | + |
| 59 | +We'll start by making a templates directory at `src/psc/templates`. |
| 60 | +In there, we'll make `page.jinja2` and `layout.jinja2`. |
| 61 | + |
| 62 | +In `layout.jinja2`, take everything out that isn't in `<main>`. |
| 63 | +Provide a slot for title and main. |
| 64 | +Then change `page.jinja2` to point at `layout.jinja2`, filling those two slots. |
| 65 | + |
| 66 | +In `app.py`, we change the `homepage` route to return a template. |
| 67 | +The context dictionary for the template will have two pieces of data: |
| 68 | +- The title of the current page |
| 69 | +- The HTML that should go in the main block. |
| 70 | + |
| 71 | +Let's do three things: |
| 72 | + |
| 73 | +- Change `index.html` only have the `<main>` part |
| 74 | +- In the route, read the file content |
| 75 | +- Then pass the file contents into `page.jinja2`, using `| safe` |
| 76 | + |
| 77 | +When done correctly, the tests should pass. |
| 78 | + |
| 79 | +## Future |
| 80 | + |
| 81 | +This PSC prototype just uses downloaded Bulma CSS and other assets. |
| 82 | +It doesn't bring in the SASS customizations and software, nor does it look at Bulma forks that bring in CSS Variables. |
| 83 | + |
| 84 | +While we did a little templating, we didn't go far. |
| 85 | +It's going to get a lot more interesting and intricate, as we have different ways we want to package things. |
0 commit comments