Do you know Bottle, a web framework?
Django is a well-known web framework for Python, but one of my personal favorite frameworks is Bottle.
You can easily create a web application just by preparing one Python file and HTML file :)
You can install Bottle with the following command.
$ pip install bottle
I've created some web applications with Bottle, but they always have the same directory structure.
It's a hassle to create a similar environment every time, so I created a tool that automatically generates folders and files :)
The source code is on GitHub.
When you run the tool (creator.py), you will be asked for the project name and the CSS framework to use.
When the input is completed, it will be automatically generated with the following directory structure.
├── app.py
├── static
│ └── main.css
└── views
└── index.html
Let's take a look at the generated file.
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="../static/main.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
<title>test3</title>
</head>
<body>
<section class="hero is-medium is-primary is-bold">
<div class="hero-body">
<div class="container">
<h1 class="title">
{{message}}
</h1>
</div>
</div>
</section>
</body>
</html>
app.py
# -*- coding: utf-8 -*-
from bottle import Bottle, template, static_file, url
import os
app = Bottle()
@app.route('/static/:path#.+#', name='static')
def static(path):
return static_file(path, root='static')
@app.route('/')
def index():
message = "Hello, Bottle!"
return template('index', message=message)
@app.error(404)
def error404(error):
return "Error 404. Try again later."
@app.error(500)
def error500(error):
return "Error 500. Try again later."
app.run(host='localhost', port=8080, debug=True)
When the generation is finished, go to the project folder and try running app.py.
$ cd <project-name>
$ python app.py
If you open http: // localhost: 8080, you should see something like the following.
(The display depends on the CSS framework you select.)
All you have to do is write the code and enrich your project!
I think that there are many points that are not enough as a tool, but please use it if you like :)
Links
Bottle: https://bottlepy.org/docs/dev/index.html
Scaffolding tool: https://github.com/ShogoMurakami/create-bottle-app
Thanks, shogo
Recommended Posts