The basics - connecting HTML and Python with a bottle
I'm new to Python. I know C, I am trying to understand Python and I succeeded, I can write some small programs with it. I also know the basics of HTML and some basic sites with it.
I have an appointment with these two. I need to make a website where the user can post messages with some attachments, rate, delete or filter existing posts, and users should also be able to choose the look of the website. And I'm only allowed to do this with the bottle and Python 3.4.
So how can you connect these two different languages, HTML and Python? I know how server relationships, requests and responses work, but I couldn't find any basic material online. How can a website be made using a python bottle? I understand the need to use python for a website, but how do I use it? I mean, is there any HTML import like "import website.py" that I can mention in a Python file in my HTML to use it, or vice versa. I need to find out how to connect a python file and html file so that I have a website that uses some python codes internally?
Please explain everything how to talk to a 10 year old boy with clear english, because I saw some information on the internet as full of codes and couldn't even figure out how these code codes work.
Many thanks.
source to share
Basically, HTML is text. Every time the browser makes a request(for example, http://localhost:8080/login
or www.domain.com/route
) some server should serve this html text document.
If your HTML never changes (static website) then you don't need Python. But if you need to generate new HTML for new requests (for example, including values ββfrom a database that may change over time), then a Python program can help you combine a basic HTML template (imagine normal html with some placeholders for variables) with new information ( e.g. inserting variable values) and render a new HTML site.
Bottle is a Python library that enhances the basic Python capabilities by using convenient methods for handling routes, sessions, etc. For example, if you have a template that looks like this in a file named hello_template.tpl
(note the html-> tpl change):
hello_template.tpl
<html>
<head>
<title>Hello World in Bottle</title>
</head>
<body>
<h1>Hello {{name.title()}}!</h1>
</body>
</html>
then you can display it with a variable name
like this from your file server.py
:
server.py
from bottle import route, template, run
@route('/hello')
@route('/hello/<name>')
def hello(name='World'):
return template('hello_template', name=name)
run(host='localhost', port=8080, debug=True)
by running the console:
python server.py
If you go to the http://localhost/alex:8080
bottle server will read the template hello_template.tpl
, fill it with Alex and give it back to you.
source to share