TV Channel Website: Boilerplate Code
Tags: webdev, python, bottle, tutorial
Published: 2022-08-22 04:12:00

GitHub: [https://github.com/Sokhavuth/TV-Channel](https://github.com/Sokhavuth/TV-Channel)
Vercel: [https://khmerweb-tv-channel.vercel.app/](https://khmerweb-tv-channel.vercel.app/)
<br/>

<p>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.</p>

<p>To build a website using Bottle.py framework, we need to create a virtual environment first by writing command as below:</p>

```
python3 -m venv venv
```
<p>As a result, a virtual environment folder "venv" was created. In Linux, We can activate this environment by writing:</p>

```
source venv/bin/activate
```
<p>Once the environment is activated, we can install Bottle.py web framework.</p>

```
pip install bottle
```
<p>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.</p>

```python
# 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)
    
###################################################################
```

<p>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.</p>

```python
# routes/frontend/index.py

from bottle import Bottle, template, get


app = Bottle()

@app.get('/')
def index():
    return template('base')
    

```

<p>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.</p>

```html
<!--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>
```

<p>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:</p>

```
Python3 index.py
```
<p>If we open the browser at the address <a href="http://localhost:8000/">http://localhost:8000</a> we will see the sentence "Hello World!" on the browser as the output from our web application.<p>

<p>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.</p>