I need to make a webpage for an assignment, it doesn\'t have to be uploaded to the web, I am just using a local .html file. I did some reading up and came up with the follow
Do you have a web server like apache setup this is running on?
If you don't I'm not sure this will work so you may want to have a look at Mamp To allow it to execute your python script you will also need to edit the httpd.conf
file
From:
Options Indexes FollowSymLinks
AllowOverride None
To:
Options +ExecCGI
AddHandler cgi-script .cgi .py
Order allow,vdeny
Allow from all
Alternatively
If you simply want to make an actual HTML file without setting up a server a very basic but crude way of doing this would be to simply write everything to a HTML file you create like:
fo.write("Content-type:text/html\r\n\r\n")
fo.write("")
fo.write("")
fo.write("Hello - Second CGI Program ")
fo.write("")
fo.write("")
fo.write("Your name is {}. {} {}
".format("last_name", "first_name", "last_name"))
fo.write("")
fo.write("")
fo.close()
Which will create a HTML document called yourfile.html
in the same directory as your python project.
I don't recommend doing this, but I realise since it's an assignment you may not have the choice to use libraries. In case you, are a more elegant way would be to use something like yattag which will make it much more maintainable.
To copy the Hello World example from their website.
from yattag import Doc
doc, tag, text = Doc().tagtext()
with tag('h1'):
text('Hello world!')
print(doc.getvalue())
Update:
Another alternative if you don't have a local web server setup is to use Flask as your web server. You'll need to structure your project like:
/yourapp
basic_example.py
/static/
/test.css
/templates/
/test.html
Python:
__author__ = 'kai'
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('test.html')
@app.route('/hello', methods=['POST'])
def hello():
first_name = request.form['first_name']
last_name = request.form['last_name']
return 'Hello %s %s have fun learning python
Back Home' % (first_name, last_name)
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 3000)
HTML:
CV - Rogier
Study
At my study we learn Python.
This is a sall example:
CSS (If you want to style your form?)
p {
font-family: verdana;
font-size: 20px;
}
h2 {
color: navy;
margin-left: 20px;
text-align: center;
}
Made a basic example based on your question here Hopefully this helps get you on the right track, good luck.