GitHub: https://github.com/Sokhavuth/TV-Channel
Vercel: https://khmerweb-tv-channel.vercel.app/
Among Python web frameworks, Bottle.py is one of the very known micro-frameworks due to its simplicity and minimalism. Although many reviewers stated that Bottle.py is only for small project, there is no reason preventing us from using this framework to build decent project such as TV channel website for example.
To build a website using Bottle.py framework, we need to create a virtual environment first by writing command as below:
python3 -m venv venv
As a result, a virtual environment folder "venv" was created. In Linux, We can activate this environment by writing:
source venv/bin/activate
Once the environment is activated, we can install Bottle.py web framework.
pip install bottle
Now, we can start to create the minimal requirement to run our TV Channel Website application. First of all, we need to create a file that will be served as the entry point in our web application. We can name the file anything we like such as index.py for example.
# index.py
from routes.frontend import index
app = index.app
###################################################################
import socket
host = socket.getfqdn()
addr = socket.gethostbyname(host)
if(addr == '127.0.1.1'):
app.run(host='localhost', port=8000, debug=True, reloader=True)
###################################################################
In line 3, we import a module with the same name index.py from "routes/frontend" package. So we need to create "routes" folder inside which the "frontend" package will store all files relating to routes for the frontend. We will use this "index.py" file to define route and handler for the landing page or homepage of the application.
# routes/frontend/index.py
from bottle import Bottle, template, get
app = Bottle()
@app.get('/')
def index():
return template('base')
As the above module will render a template file with the name "base.tpl" in the "views" template folder by default, we need to create this template file and a "views" folder in the root folder to save the file.
<!--views/base.tpl-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>TV Channel Website</title>
</head>
<body>
Hello World!
</body>
</html>
That is everything now to run our minimal web application using Bottle.py web framework. To run this application, we need to write command as below:
Python3 index.py
If we open the browser at the address http://localhost:8000 we will see the sentence "Hello World!" on the browser as the output from our web application.
We should remark that at the bottom of the landing page "index.py" is the necessary part to run our web application locally, on the server, this part will not be used.
Top comments (0)