How Insert Python code in a Html File ?
<html>
<head>
</head>
<h1>Hello Python!</h1>
<body>
Here i want to insert Python Code
</body>
</html>
-
2Do you want to display it or run it?Qurben– Qurben2014年06月28日 15:35:33 +00:00Commented Jun 28, 2014 at 15:35
-
1Are you coding this is Python? What problems are you having with inserting some code? I suspect I know what problems you are facing, but those are not unique to Python.Martijn Pieters– Martijn Pieters2014年06月28日 15:36:59 +00:00Commented Jun 28, 2014 at 15:36
-
Create the complete file with Python or only the body and include it. ( like Virtual include in Apache)smartmeta– smartmeta2014年06月28日 15:40:56 +00:00Commented Jun 28, 2014 at 15:40
-
I want a simple example for <TR> <TD> and For...Loop of Python ?user3786016– user37860162014年06月28日 15:42:13 +00:00Commented Jun 28, 2014 at 15:42
-
You can't insert Python in HTML, you can only print HTML from Python. Python is not PHP.furas– furas2014年06月28日 16:09:18 +00:00Commented Jun 28, 2014 at 16:09
4 Answers 4
simple example for CGI
print('Content-Type: text/html')
print('\n\n')
print('''<html>
<head>
</head>
<h1>Hello Python!</h1>
<body>''')
# Here insert Python Code
print('''</body>
</html>''')
simple example for WSGI
def application(environ, start_response):
output = '''<html>
<head>
</head>
<h1>Hello Python!</h1>
<body>'''
# Here insert Python Code which appends string to `output`
output += '''</body>
</html>'''
status = '200 OK'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
4 Comments
mod_wsgi or mod_cgi to run Python, Perl or other script. Did you see previous link ? There is Apache config example.CGI seems to be easer to use but with WSGI you can use Python web frameworks like Django or mini web frameworks like FlaskWSGI configuration in Flask docs You cannot (or at least should not) mix python with HTML like you can do with PHP. For producing HTML dynamically, you can use any templating library like Jinja. You can also manipulate HTML as string directly in python script but that is obviously not recommended!
2 Comments
.py files. But why are you writing raw python for web? Why not a python web framework?I think you try to archive the same thing as with PHP and
<php> smth </php> tags.
And it is impossible. You may try using something like Brython.
This is only true options. Or just try Coffeescript which is pretty similar to Python and Ruby.
But if you need to do it on server side only, that mean the code will be executed on you side of wire and not on browser you can look at things like Flask. This framework is really simple and easy to use and how great templating engine.
PS. Including any code other than JS or language compiled to JS inside HTML is not a good idea. It is just asking for troubles.
Comments
I think you want to design a website or web service with python language. You must use flask ot django ( my prefer is flask ) for this. Flask contains werkzeug and jinja2 packages for it. But you can't write something like javascript with python.