Giter VIP home page Giter VIP logo

htpy's Introduction

htpy - HTML in Python

htpy is a library that makes writing HTML in plain Python fun and efficient, without a template language.

Define HTML in Python:

from htpy import body, h1, head, html, li, title, ul

menu = ["egg+bacon", "bacon+spam", "eggs+spam"]

print(
    html[
        head[title["Todays menu"]],
        body[
            h1["Menu"],
            ul(".menu")[(li[item] for item in menu)],
        ],
    ]
)

And get HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>Todays menu</title>
  </head>
  <body>
    <h1>Menu</h1>
    <ul class="menu">
      <li>egg+bacon</li>
      <li>bacon+spam</li>
      <li>eggs+spam</li>
    </ul>
  </body>
</html>

Motivation for this project

At Personalkollen, where htpy was originally developed we often found ourselves hitting walls when using classic templates. htpy was created to improve the productiveness and experience of generating HTML from a Python backend.

Key features

  • Leverage static types: Use mypy or pyright to type check your code.

  • Great debugging: Avoid cryptic stack traces from templates. Use your favorite Python debugger.

  • Easy to extend: There is no special way to define template tags/filters. Just call regular functions.

  • Works with existing Python web framework: Works great with Django, Flask or any other Python web framework!

  • Works great with htmx: htpy makes for a great experience when writing server rendered partials/components.

  • Create reusable components: Define components, snippets, complex layouts/pages as regular Python variables or functions.

  • Familiar concepts from React: React helped make it popular writing HTML with a programming language. htpy uses a lot of similar constructs.

Philosophy

htpy generates HTML elements and attributes and provide a few helpers.

htpy does not enforce any particular pattern or style to organize your pages, components and layouts. That does not mean that htpy cannot be used to build sophisticated web pages or applications.

Rather the opposite: you are encouraged the leverage the power of Python to structure your project. Use modules, classes, functions, decorators, list comprehension, generators, conditionals, static typing and any other feature of Python to organize your components. This gives you a lot of power and makes htpy scale from a single small Flask project to bigger applications.

Common patterns can give you some ideas that you can build upon yourself.

The syntax

Child elements are specified using the [] syntax. This may look strange at first but it has some nice benefits. This clearly separates attributes from children elements and makes the code more readable. It is implemented using the __getitem__ method, just like lists or dicts.

Installation

htpy is available on PyPI. You may install the latest version using pip:

pip install htpy

Documentation

The full documentation is available at https://htpy.dev:

htpy's People

Contributors

pelme avatar majsvaffla avatar stabbarn avatar

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.