Skip to main content

Make interactive web apps using good ol' HTML, CSS and a sprinkling of JavaScript — in Python.

Project description

MakeWeb

Make interactive web apps using good ol' HTML, CSS and a sprinkling of JavaScript — in Python.

Examples

HyperText Markup Lnguage

If we run this script:

# 00-generate-html.py

from makeweb import Doc, body, h1

def generate_html():
    doc = Doc('html', lang='mr')
    with body():
        h1('हा!')
    return str(doc)

print(generate_html())

We should see this output on the screen:

<!doctype html>
<html lang="mr">
<body>
  <h1>हा!</h1>
</body>
</html>

(Whitespace added for clarity.)

Ha! HTML was easy, let us generate CSS from Python code.

Cascading Style Sheets

# 01-generate-css.py

from makeweb import CSS

css = CSS()

css('body', background='white', color='#222')
css('h1', color='darkorange', margin__top='1em')

print(str(css))

Running the above example we see...

body{
  background:white;
  color:#222
}
h1{
  color:darkorange;
  margin-top:1em
}

(Whitespace added for clarity.)

Notice that the double underscore in css('h1', margin__top='1em') gets converted to hyphen in CSS as h1{margin-top:1em}. This pattern is used throughout the library for HTML and CSS attributes.

So... CSS is even easier?! How about something more ambitious?

JavaScript

# 02-generate-js.py

from makeweb import JS

js = JS()

@js.function
def say_hello():
    hello_box = document.getElementById("hello_box")
    hello_box.innerHTML = "Hello, World Wide Web!"

print(str(js))

And we get a JavaScript function out!

function say_hello(){
  var hello_box;
  hello_box=document.getElementById("hello_box");
  hello_box.innerHTML="Hello, World Wide Web!";
}

(Whitespace added for clarity.)

Now let us use these capabilities together!

App

# hello-readme.py

from flask import Flask, Response
from makeweb import (
    Doc, CSS, JS,
    head, title, style, script,
    body, h1, button,
)

# We'll use Flask to serve, you could use any other framework 
# or save as static files, or anything else 
# that you wish to do with generated html.
app = Flask(__name__)  
css = CSS()
js = JS()

css('*,body',   # <-- Add CSS to taste.
    font__family='sans-serif',
    text__align='center')
css('h1', color="darkblue")  


@js.function  # <-- A sprinkling of JavaScript. Look ma, no braces!
def say_hello():
    hello_box = document.getElementById("hello_box")
    hello_box.innerHTML = "Hello, World Wide Web!"


@app.route('/')
def index():
    doc = Doc('html')  # <-- Generate all the HTML your app (really) needs.
    with head():
        title('Hello')
        with style():  # Embed CSS.
            css.embed()
    with body():
        h1('...', id='hello_box')  # Set attributes. 
        button('Hello', onclick="say_hello()")  # <-- hook up say_hello().
        with script():  # Embed JavaScript.
            js.embed()
    return Response(str(doc))  # <-- Serve all the awesome your users desire!


app.run()  # <-- It is time! 

This app transfers ~550 bytes over the network in order to run successfully, that is including the HTTP headers overhead. You read that right, not even one kilobyte! The web technologies are quite simple and straightforward for general use, and very flexible, robust and powerful too!

You might not need (or want the baggage of) complex tooling for a small project. It could be a one time make-and-forget tool at work or a weekend hobby project, or maybe something even larger if you really like this way of working. MakeWeb can come in handy because it makes it almost trivial to build the web like it was intended, straight from your Python code.

Wait, somebody mentioned single-page-apps? How about single source-file apps that don't download half the internet to work? 😂 Kidding, this is a very, very barebones system, and therefore you can use any existing stylesheets or JS libraries alongside MakeWeb.

Check out examples for more demos!

Install

Using Poetry (Recommended)

# Clone the repository
git clone https://github.com/hiway/makeweb.git
cd makeweb

# Install poetry if you haven't already
pip install poetry

# Install makeweb with JS support
poetry install --with js

# For development
poetry install --with dev

# For running examples
poetry install --with examples

# For everything
poetry install --with dev,js,examples

Using pip (Legacy)

Stable

python3 -m venv makeweb
source makeweb/bin/activate
pip install makeweb[js]

Current

python3 -m venv makeweb
source makeweb/bin/activate
git clone https://github.com/hiway/makeweb.git
cd makeweb
pip install -e .[examples]

Development

python3 -m venv makeweb
source makeweb/bin/activate
git clone https://github.com/hiway/makeweb.git
cd makeweb
pip install -e .[dev]
  • libtidy-dev

Test

pytest tests.py

With coverage:

pytest --cov=makeweb --cov-report=term tests.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

makeweb-0.2.1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

makeweb-0.2.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file makeweb-0.2.1.tar.gz.

File metadata

  • Download URL: makeweb-0.2.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.3 Linux/6.8.0-51-generic

File hashes

Hashes for makeweb-0.2.1.tar.gz
Algorithm Hash digest
SHA256 d7d2dc65895a62e0822408957ddf4a0950de48eaa4de69d86efc482d584bea14
MD5 36edfdbbbd83a5a34d34f285f944b46c
BLAKE2b-256 bc54e2c406c88fd1a59943bd556ab47bfa9beb2ae45a101b1c8656ca4e0b8714

See more details on using hashes here.

File details

Details for the file makeweb-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: makeweb-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.3 Linux/6.8.0-51-generic

File hashes

Hashes for makeweb-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4ca9360c7f445002d2a114cc921eabf1ffeae9c078b7143f7a8153b872891ffb
MD5 2d8ab5cfc58a2c5705dc02c45d1a9d4a
BLAKE2b-256 669a3473ed9aa2f571016773df4b69fdf72280fd2992b8807e145cb89b602027

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page