5

I've gotten pretty comfortable with Python and now I'm looking to make a rudimentary web application. I was somewhat scared of Django and the other Python frameworks so I went caveman on it and decided to generate the HTML myself using another Python script.

Maybe this is how you do it anyways - but I'm just figuring this stuff out. I'm really looking for a tip-off on, well, what to do next.

My Python script PRINTS the HTML (is this even correct? I need it to be on a webpage!), but now what?

Thanks for your continued support during my learning process. One day I will post answers!

-Tyler

Here's my code:

from SearchPhone import SearchPhone

phones = ["Iphone 3", "Iphone 4", "Iphone 5","Galaxy s3", "Galaxy s2", "LG Lucid", "LG Esteem", "HTC One S", "Droid 4",
          "Droid RAZR MAXX", "HTC EVO", "Galaxy Nexus", "LG Optimus 2", "LG Ignite",
          "Galaxy Note", "HTC Amaze", "HTC Rezound", "HTC Vivid", "HTC Rhyme", "Motorola Photon",
          "Motorola Milestone", "myTouch slide", "HTC Status", "Droid 3", "HTC Evo 3d", "HTC Wildfire",
          "LG Optimus 3d", "HTC ThunderBolt", "Incredible 2", "Kyocera Echo", "Galaxy S 4g",
          "HTC Inspire", "LG Optimus 2x", "Samsung Gem", "HTC Evo Shift", "Nexus S", "LG Axis", "Droid 2",
          "G2", "Droid x", "Droid Incredible" 
          ]

print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>table of phones</title>
</head>

<body>
</body>
</html>
"""

#table
print '<table width="100%" border="1">'
for x in phones:
    y = SearchPhone(x)
    print "\t<tr>"
    print "\t\t<td>" + str(y[0]) + "</td>"
    print "\t\t<td>" + str(y[1]) + "</td>"
    print "\t\t<td>" + str(y[2]) + "</td>"
    print "\t\t<td>" + str(y[3]) + "</td>"
    print "\t\t<td>" + str(y[4]) + "</td>"
    print "\t</tr>"
print "</table>"
3
  • What does the variable y, SearchPhone(x) represent? Commented Dec 3, 2012 at 5:04
  • This looks so much like my first PHP pages... some code on top followed by HTML; fond memories ^_^ Commented Dec 3, 2012 at 5:08
  • @enginefree, I'm calling my SearchPhone def from my script, it returns a tuple of prices Commented Dec 3, 2012 at 5:08

3 Answers 3

4

unfortunately, you are not quite right .

For python web development , as a starter you should pick a web development framework,(Django, Flask, Bottle) if you are scared of Django , I suggest you try to look at Web.py / bottle, they are very simiple and easy to catch up and you will learn basic web develpment skills ,know the workflow, interacting etc.

or , you just want to write a static webpage , just write a file with "htm" "html" extentsion

html_str = "<html><head>This is header</head><body>This is body</body></html>"
f = open("yourpage.html","w")
f.write(html_str)
f.close()

Then you can open "yourpage.html" get your first web page written in Python statically ..

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the advice. The static page tip is useful, too (but you're right, I better learn one of these framework, things...)
1

Using Flask framework, you can write your first webapp in five minutes, following the tutorial. Probably it is easier than Django for the start.

By default, Flask uses Jinja2 template engine. You can write something like:

<table>
{% for phone in phones %}
    <tr><td>{{ phone.name }}</td></tr>
{% endfor %}
</table>

where phones is a Python variable passed to template. Not too complex, isn't it?

2 Comments

That looks like my kind of script! Thanks for the link. I'll give it shot tomorrow after class.
this is exactly how django handles tempaltes too for the record! django may seem daunting at first because there are a lot of setup details hurled at you right away.
0

In spite of this question being a few years old...

If you are using a system with a decent shell (which you should) you can leave your code as it is (i.e. writing to stdout) and redirect the output to a file:

$ python html_generating_program.py > output.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.