Reputation: 37
I am newbie on Python. I know C, I've been trying to figure Python out and I succeeded a little bit, I can write some little programs with it. I also know HTML basics and did some basic websites with it.
I have an assignment with these two. I need to do a website that user can post some messages with some attachments, can rate, delete or filter existing messages, and users also should be able to choose the appearance of the website. And I am allowed to do it only with "bottle" and Python 3.4.
So, how is this possible to connect those two different languages, HTML and Python? I know how the server, request and response relations work but I could'nt find any basic stuff on internet about it. How is it possible to do a website using python bottle? I understand the need of python for a website but how to use it? I mean, is there any import on HTML like "import website.py" that I can mention the Python file in my HTML to use it, or vice versa. The thing I need to learn is how to connect python file and html file so that I have a website that uses some python codes inside?
Please explain everything just like talking to a 10 year old boy with a clear English, because I saw some information on web like full of codes and could not even understand how does those bottle codes work.
Thank you very much.
Upvotes: 3
Views: 2225
Reputation: 40963
In essence HTML is text. Each time the browser makes a request (for example to http://localhost:8080/login
or www.domain.com/route
) some server has to 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 which could change in time) then a Python program can help combine a basic HTML template (imagine normal html with some placeholders for variables) with the new info (for example inserting variable values) and render the new HTML website.
Bottle is a Python library that enhances basic Python capabilities by having convenience methods to handle routes, sessions, etc. For example if you have a template that looks like this in file called hello_template.tpl
(note the change html->tpl):
hello_template.tpl
<html>
<head>
<title>Hello World in Bottle</title>
</head>
<body>
<h1>Hello {{name.title()}}!</h1>
</body>
</html>
then you can render it with the variable name
like this from your server.py
file:
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 in the console:
python server.py
If you then go to http://localhost/alex:8080
the bottle server will read the template hello_template.tpl
, fill it with the name Alex and return it to you.
Upvotes: 5