Giter VIP home page Giter VIP logo

simplesite's Introduction

Simple Site

Featuring htmx and TailwindCSS

Status GitHub Issues GitHub Pull Requests License


Create a Python-backed frontend with FastAPI, htmx, and TailwindCSS. Build it from scratch and follow the guide. Alternatively, clone the repo/use the template, or better yet, spin up a Codespace and start right away!

Project demo

πŸ“ Table of Contents

🧐 About

Build a beautiful web application using nothing more than Python, htmx, and TailwindCSS. Harness the power of Jinja templates and server-side rendering to create a dynamic, REST-ful web app.

🏁 Getting Started

This repository was prepared for a workshop on how to create a python-backed frontend, featuring Jinja templates for HTML rendering, TailwindCSS for style, and htmx for pizzazz! 😎

The workshop consists of four chapters, each introducing an additional tool on the road to creating a beautiful* Python-backed frontend.

*Note: Beauty is in the eye of the beholder.

Prerequisites

The example app was created with Python 3.11, but it is likely compatible with earlier versions. However, I would highly recommend using the latest version of Python. The rest of the dependencies are listed in the requirements.txt file.

fastapi[all]
jinja2
jinja2-fragments
python-multipart
pytest
pytailwindcss
tinydb

The fastapi[all] dependency installs some other optional dependencies and features. It also includes uvicorn, which is used as the server to run your code. (You could choose to just use fastapi and uvicorn[standard] separately, if you prefer.)

Why is there a pyproject.toml file? If you use a package manager (i.e., I use pdm), you can use your package manager to install dependencies. Otherwise, you can go the more traditional route using the requirements.txt file. If you use Codespaces, you won't need to worry about dependencies!

Installing

Using Codespaces

Press the <> Code button above and select Create a Codespace on main. This will open a new window in your browser, where you can run the code in a virtual environment.

Selecting.Codespaces.mp4

Locally

Create a copy of the repo using the Use this template button above. Select Create a new repository.

Warning Be sure to select Include all branches when cloning the repo.

After cloning or using this template, you will need to create a virtual environment. Navigate to the location where you have cloned the project (your project root) and run the following command in your terminal:

python -m venv .venv

This will create a .venv directory within your project.

Next, activate your environment:

# On Windows
.\.venv\Scripts\activate

# On MacOS/Linux
$ source .venv/bin/activate

Then, install the requirements:

python -m pip install -r requirements.txt

πŸ”§ Running the tests

After activating your virtual environment, you can run tests by typing pytest on the command line. This makes sure that your application runs and can generate a "Hello World" message.

pytest

If everything has gone well so far, all tests should pass.

πŸš— πŸ’¨ Need to Catch Up?

If you are using Codespaces, there is a script you can use to catch up to the current chapter. Just run the following command in your terminal and choose the section we're on:

. catchup.sh
demo.mp4

Or (if you're developing locally) to catch up manually...

Checkout the branch that corresponds to the appropriate section (see below).

🎈 Guide

This repo was created primarily to aid in a workshop setting, so your mileage may vary. Feel free to clone it and make it your own. But most of all, have fun! πŸ₯³

To take a more structured approach, you can follow sequentially with the accompanying markdown files for each branch of the repo.

These Chapters are all located in the "docs" directory. The direct links to the corresponding chapters are listed here for convenience.

✨Build a Python-Backed Frontend With HTMX and TailwindCSS✨

Chapter Title Branch
Preface Getting Started main
Chapter 1 Using Jinja Templates to Render HTML 01_templates
Chapter 2 Harnessing TailwindCSS for Consistent Design 02_tailwindcss
Chapter 3 A Thin Database Layer 03_tinydb
Chapter 4 Modern Browser Features Directly from HTML 04_htmx

⛏️ Built Using

  • FastAPI
  • Jinja2
  • TailwindCSS
  • TinyDB
  • htmx

✍️ Authors

πŸŽ‰ Acknowledgements

  • @kjaymiller - Jay Miller, Senior Cloud Advocate-Python, Microsoft

simplesite's People

Contributors

kjaymiller avatar rooyca avatar tataraba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

simplesite's Issues

The hx-request header creates error when using back button in browser

On 04_htmx branch:

When visiting the "/catalog" endpoint, artist cards are generated. An htmx event toggles from Artist/Members to Artist/Profile displayed (see below):

image

Clicking on the "MORE INFO" link sends you outside of the app (but it also triggers an hx-request). Since this is an hx-request, it triggers this bit of code in routes.py:

if request.headers.get("hx-request"):
        id = request.headers.get("HX-Trigger")
        artist = db.find("id", int(id))
        print(artist)
        return templates.TemplateResponse(
            "artist/profile.html",
            {
                "request": request,
                "artist": artist[0],
                "get_website": get_website,
                "get_profile": get_profile,
            }
        )

At this point, there is no longer an HX-Trigger, which means that the db.find("id", int(id)) is creating a ValueError.

When a user uses the "back" button in their browser to return to the "/catalog" endpoint, they receive an Internal Server Error.

May be able to fix by adding condition to check for "HX-Trigger", but need to make sure response is correct for what the user is trying to do.

Active search - display all artists when search box is empty

For 04_htmx branch:

When selecting the "search" element (<input>), the html response that gets rendered in the "#search-results" div is empty (nothing found).

image

Instead, the element should populate with all artists by default. Once the search is started (on keypress), it filters down appropriately.

Right now, the search.html template has this block:

{% block content %}
<section id="body" class="flex flex-col bg-slate-50 justify-center items-center max-w-screen-lg m-auto">
    <div class="flex flex-col justify-center items-center py-10 min-w-full">
        <h2 class="text-2xl leading-relaxed text-slate-800 uppercase">Search Results</h2>
        <div id="search-results" class="flex flex-row flex-wrap justify-center content-center text-center w-full">
            {% if search|list %}
                {% for result in results %}
                    <p>{{result["name"]}}</p>
                {% endfor %}
            {% else %}
                <p>No results found.</p>
            {% endif %}
        </div>
    </div>
</section>
{% endblock %}

Maybe adding a default for all results in the {% else %} statement?

Make infinite scroll for Artist information

In 04_htmx branch:

Create a new endpoint called /artist to showcase the infinite scroll capabilities of htmx.

See htmx.org for an example.

Maybe create a large artist card that includes active band members, profile, and a picture, and as you scroll down, the next artist will populate with same info.

Add tests

Just opening a general issue to add more testing.

The project is already set up with pytest, but as of right now, it's only set to work with the "hell world" type of app on the main branch. Would like to add testing, especially for branches 03_tinydb and 04_htmx.

Make catalog artist cards toggle

On the 04_htmx branch:

The "/catalog" endpoint displays a list of cards with an Artist name and Members that looks like this:

image

When the container is clicked, with htmx (hx-get), the containing element changes to display the Artist profile, like this:

image

Want to include another htmx event on click that reverts the response within this element back to the Artist name and Members. (making it function mostly as a toggle)

Under-used settings: `APP_DIR`, `STATIC_DIR`, `TEMPLATE_DIR`

Nice little guide. Just a note for the ones reading: the config file is already defining the path for the app folder, the static folder or the templates folder.

So when you are reading Chapter 1, don't forget that you could use the Pydantic Settings class directly instead of giving strings as paths.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.