DEV Community

Abderrahmane Mustapha
Abderrahmane Mustapha

Posted on

A python website no framework needed part 2

Its the second and the last part of how you can make a website with python using no framework
, the purpose of these two articles is to show to the new developers that its possible to create a website with python without using a framework or any other PyPI package

Show the code

First, we need to separate the views, templates, and logic,
we will have the following folder structure :

--- mainFolder
---- app.py
--------- views
----------- __init__.py
----------- views.py
--------- templates
----------- index.html
--------- utils
----------- __init__.py
----------- render.py
Enter fullscreen mode Exit fullscreen mode

let's start with the render.py file where we are going to write the code to render the HTML templates

# mainFolder/utils/render.py
def render(path):
    with open(path, "r") as f:
        template= f.read()
        return  template.encode("utf-8")
Enter fullscreen mode Exit fullscreen mode

import the render method in the init.py

 #mainFolder/utils/__init__.py
 from .render import render
Enter fullscreen mode Exit fullscreen mode

now let's move to the HTML template add this code to it

<!--  mainFolder/templates/index.html -->

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <header>
      <h1>Home page</h1>
    </header>
    <main>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ut
        pellentesque sapien. Donec id pellentesque sem. Praesent vel urna vitae
        odio hendrerit malesuada et nec enim. Nulla risus dui,
      </p>
      <form action="/" method="POST">
        <input type="text" name="text" value="azaze" />
        <button type="submit">Submit</button>
      </form>
    </main>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

let's add a view now

# mainFolder/views/index.py
from utils import render
def index(environ):
    return render("templates/index.html")
Enter fullscreen mode Exit fullscreen mode

import the index in init.py file of the views folder

# mainFolder/views/__init__.py
from .index import index
Enter fullscreen mode Exit fullscreen mode

last step lets move our main file app.py to where im going to send responses to the client-side and handle post requests

from views import index

def app(environ, start_response):
        data = index(environ)
        if environ['REQUEST_METHOD'] == "POST":
            print("inputs ----------------- ", environ['wsgi.input'].read())
        start_response("200 OK", [
            ("Content-Type", "text/html"),
            ("Content-Length", str(len(data)))
        ])
        return iter([data])


if __name__ == '__main__':
    from gevent.pywsgi import WSGIServer
    WSGIServer(('', 8000),app, spawn=None).serve_forever()
Enter fullscreen mode Exit fullscreen mode

you can run

python app.py
Enter fullscreen mode Exit fullscreen mode

you will see an HTML template with a header, input form, and a submit button

add a text to the input hit the submit button and check the console you will see your text

inputs -----------------  b'text=azaze'
::1 - - [2020-10-02 13:06:19] "POST / HTTP/1.1" 200 777 0.003350
Enter fullscreen mode Exit fullscreen mode

For Further Exploration

Do it your self framework

WSGI handle post request

Social footer

By Toumi abderrahmane twitter . github

Top comments (0)