Hi, is it possible to skip using python frameworks here and just code python to generate html? For example, I just want to have this in a file:
print(“<b>hello world</b>“)
Thanks!
Hi, is it possible to skip using python frameworks here and just code python to generate html? For example, I just want to have this in a file:
print(“<b>hello world</b>“)
Thanks!
You can avoid frameworks, but it's not as simple as just printing things -- you'd need to write code that was compatible with the WSGI protocol that is used to communicate between the web server and Python code. That is pretty fiddly to get right; here's a (fairly) minimal example:
HELLO_WORLD = "<b>hello world</b>"
def application(environ, start_response):
if environ.get('PATH_INFO') == '/':
status = '200 OK'
content = HELLO_WORLD
else:
status = '404 NOT FOUND'
content = 'Page not found.'
response_headers = [('Content-Type', 'text/html'), ('Content-Length', str(len(content)))]
start_response(status, response_headers)
yield content.encode('utf8')
You might find this blog post relevant, though -- it covers how to convert a Python script into a website using Flask, with minimal fuss.
Thank you!!! Hmm yeah, my dilemma is that I am trying to get my 9 year old to play with serverside python, but using a framework is just too much to keep him engaged. Putting html code as part of a python string without colorcoding makes it harder to learn. I was looking for an online python IDE’s to accelerate learning process, and thought pythonanywhere was the only one and amazing, but this limitation is just inconvenient.
A minimal Flask app is really simple, though -- this would be enough to serve up a page with "Hello world" on it:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "<b>hello world</b>"
Ah, I missed what you said about colour-coding of the HTML there. That does make it a little more complicated, but you could still do it in two files -- a Flask site like this:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template("index.html")
...and then a template in the appropriate directory called index.html
with
<b>hello world</b>
That said, if all you need is a static site with just HTML, you could just do that -- we have a help page on how to set one up.
Yes, this definitely works for static HTML, I think someone here, possibly you, showed this to me before. But the problem is I can’t embed python into html this way. I guess I might be looking for something that’s not even possible with python. Here is what I know works in php and would be ideal for me. is this possible in python though?
<html> <head> <title>Embed PHP in a .html File</title> </head> <body> <h1><?php echo "Hello World" ?></h1> </body> </html>
I also remember embedding asp.net vbscript (yes, yuck!) into html 20 years ago with <% %> tags, but I know that’s Microsoft’s stuff
Do you see what I am getting at?
Yes, we get what you're getting at. That style of development is generally not what is available in Python. There may be a library or package that provides something like that, but it's not likely to be a well-known, widely used one.