Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 195501

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:41:15+00:00 2026-05-11T16:41:15+00:00

While I have been playing with Python for a few months now (just a

  • 0

While I have been playing with Python for a few months now (just a hobbyist), I know very little about Web programming (a little HTML, zero JavaScript, etc). That said, I have a current project that is making me look at web programming for the first time. This led me to ask:

What's easiest way to get Python script output on the web?

Thx to the answers, I made some progress. For now, I’m just using Python and HTML. I can’t post my project code, so I wrote a small example using twitter search (pls see below).

My questions are:

  1. Am I doing anything terribly stupid? I feel like WebOutput() is clear but inefficient. If I used JavaScript, I’m assuming I could write an HTML template file and then just update the data. Yes? The better way to do this?

  2. At what point would a framework be appropriate for an app like this? overkill?

Sorry for the basic questions – but I don’t want to spend too much time going down the wrong path.

import simplejson, urllib, time

#query, results per page 
query = "swineflu"
rpp = 25
jsonURL = "http://search.twitter.com/search.json?q=" + query + "&rpp=" + str(rpp)

#currently storing all search results, really only need most recent but want the data avail for other stuff
data = []

#iterate over search results
def SearchResults():
    jsonResults = simplejson.load(urllib.urlopen(jsonURL))
    for tweet in jsonResults["results"]:
        try:
            #terminal output
            feed = tweet["from_user"] + " | " + tweet["text"]
            print feed
            data.append(feed)
        except:
            print "exception??"

# writes latest tweets to file/web
def WebOutput():
    f = open("outw.html", "w")
    f.write("<html>\n")
    f.write("<title>python newb's twitter search</title>\n")
    f.write("<head><meta http-equiv='refresh' content='60'></head>\n")
    f.write("<body>\n")
    f.write("<h1 style='font-size:150%'>Python Newb's Twitter Search</h1>")
    f.write("<h2 style='font-size:125%'>Searching Twitter for: " + query + "</h2>\n")
    f.write("<h2 style='font-size:125%'>" + time.ctime() + " (updates every 60 seconds)</h2>\n")

    for i in range(1,rpp):
        try:
            f.write("<p style='font-size:90%'>" + data[-i] + "</p>\n")
        except:
            continue

    f.write("</body>\n")
    f.write("</html>\n")
    f.close()

while True:
    print ""
    print "\nSearching Twitter for: " + query + " | current date/time is: " + time.ctime()
    print ""
    SearchResults()
    WebOutput()
    time.sleep(60)
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:41:16+00:00Added an answer on May 11, 2026 at 4:41 pm

    It would not be overkill to use a framework for something like this; Python frameworks tend to be very light and easy to work with and would make it much easier for you to add features to your tiny site. But neither is it required; I’ll assume you’re doing this for learning purposes and talk about how I would change the code.

    You’re doing templating without a template engine in your WebOutput function; there are all kinds of neat template languages for Python, my favorite of which is mako. If the code in that function ever gets hairier than it is currently, I would break it out into a template; I’ll show you what that would look like in a moment. But first, I’d use multiline strings to replace all those f.write‘s, and string substitution instead of adding strings:

    f.write("""<html>
    <title>python newb's twitter search</title>
    <head><meta http-equiv='refresh' content='60'></head>
    <body>
    <h1 style='font-size:150%'>Python Newb's Twitter Search</h1>
    <h2 style='font-size:125%'>Searching Twitter for: %s</h2>
    <h2 style='font-size:125%'>%s (updates every 60 seconds)</h2>""" % (query, time.ctime()))
    
    for datum in reversed(data):
        f.write("<p style='font-size:90%'>%s</p>" % (datum))
    
    f.write("</body></html>")
    

    Also, note that I simplified your for loop a bit; I’ll explain further if what I put doesn’t make sense.

    If you were to convert your WebOutput function to Mako, you would first import mako at the top of your file with:

    import mako
    

    Then you would replace the whole body of WebOutput() with:

    f = file("outw.html", "w")
    data = reversed(data)
    t = Template(filename='/path/to/mytmpl.txt').render({"query":query, "time":time.ctime(), "data":data})
    f.write(t)
    

    Finally, you would make a file /path/to/mytmpl.txt that looks like this:

    <html>
    <title>python newb's twitter search</title>
    <head><meta http-equiv='refresh' content='60'></head>
    <body>
    <h1 style='font-size:150%'>Python Newb's Twitter Search</h1>
    <h2 style='font-size:125%'>Searching Twitter for: ${query}</h2>
    <h2 style='font-size:125%'>${time} (updates every 60 seconds)</h2>
    
    % for datum in data:
        <p style'font-size:90%'>${datum}</p>
    % endfor
    
    </body>
    </html>
    

    And you can see that the nice thing you’ve accomplished is separating the output (or "view layer" in web terms) from the code that grabs and formats the data (the "model layer" and "controller layer"). This will make it much easier for you to change the output of your script in the future.

    (Note: I didn’t test the code I’ve presented here; apologies if it isn’t quite right. It should basically work though)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 117k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer orgmode manages to do it by copying the code out… May 11, 2026 at 10:47 pm
  • Editorial Team
    Editorial Team added an answer I think your issue might be that the UIWindow is… May 11, 2026 at 10:47 pm
  • Editorial Team
    Editorial Team added an answer The cookie name is always a string. Do you mean… May 11, 2026 at 10:47 pm

Related Questions

I have been trying to figure out how to retrieve (quickly) the number of
Lately I have been playing a game on my iPhone called Scramble. Some of
So I've just started playing around with Django and I decided to give it
I can use FireFox and FireBug, in a pane, I can open a .css

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.