9
votes

How do I put this python script:

a = ['f','d','s','a']
x = -1
scope = vars()
for i in a:
    scope['x']+=1
    print a[x]

inside of a html file?

8
Python isn't like PHP, you can't just embed it as a script in HTML. - Rafe Kettler
@Rafe That makes more sense. At first I thought he meant formatting with HTML pre tags. - chrisaycock
@chrisaycock i thought that too and answered. @ones you cant just write some python code in html and execute, there are some frameworks to use python in web-technology like django, app-engine, web2py etc. Python is not a browser scripting language like javascript. - crodjer
...and what on Earth are you trying to achieve by editing the dict returned by vars()? - Johnsyweb

8 Answers

6
votes

Something like this, if you want to create an html, not necessarily display it:

 html_file = open('namehere.html','w')
 a = ['f','d','s','a']
 x = -1
 scope = vars()
 data = ''
 for i in a: #TIP: use a generator
     scope['x']+=1
     data += a[x]
     data += '\n'
 html_file.write(data)
 html_file.close()
4
votes

If your web server supports it, you could run it as a CGI script to output an HTML file - more information here: http://www.penzilla.net/tutorials/python/cgi/

You would need to modify your script to ouput valid HTML, but that tutorial should get you started.

Good luck!

3
votes

Not possible. Python isn't like PHP; I can't just do this

<?php

And be good to go.

However, if your web server has a Python interpreter (most all do, these days), you can write CGI (common gateway interface) scripts to make Python code run on your webpage.

If you're trying to generate dynamic content (like change words in HTML), Javascript or PHP is better. Python is more suited to web applications.

1
votes

Perhaps CGI is what you are looking for:

http://docs.python.org/library/cgi.html

http://www.penzilla.net/tutorials/python/cgi/

For example:

print "Content-Type: text/html"     # HTML is following
print                               # blank line, end of headers

print "<html><head></head><body><pre>"
a = ['f','d','s','a']
x = -1
scope = vars()
for i in a:
    scope['x']+=1
    print a[x]
print "</pre></body></html>"

Hope this helps. Good luck!

1
votes

if the script is in a server , you can run it using remote funcion call through JSON-RPC
you may refer the JSON-RPC documentation here

0
votes

Surround it with a <body> and <head> tag and you're golden.

But seriously, I think what you are trying to do is print fdsa, which would would look like this:

<head>
<body>
fdsa
</body>
</head>

What you have there is not really a python script. You might need to correct that first, and then give a little more explanation what you are trying to do.

0
votes

You can convert the thing into HTML or JavaScript. It would sort of be like this:

<script>
var a = ['f','d','s','a']
var x = -1
//other code
</script>

I am not showing the rest because I am not sure how to make a repeat loop.

-3
votes

You can't. If you want to run script in an HTML File, try another language like JavaScript or PHP. To include javascript, type this:

<script type="text/javascript">
    // ...
</script>

Or in HTML5, you don't even have to type the type attribute:

<script>
    // ...
</script>

To include PHP, type

<?php
    // ...
?>