Skip to content Skip to sidebar Skip to footer

Display Python Output In Html

What is the simplest way to display the Python ystockquote (http://goldb.org/ystockquote.html) module output in HTML? I am creating an HTML dashboard which will be run locally on m

Solution 1:

I would use a templating system (see the Python wiki article). jinja is a good choice if you don't have any particular preferences. This would allow you to write HTML augmented with expansion of variables, control flow, etc. which greatly simplifies producing HTML automatically.

You can simply write the rendered HTML to a file and open it in a browser, which should prevent you from needing a webserver (though running python -m SimpleHTTPServer in the directory containing the HTML docs will make them available under http://localhost:8000)

Solution 2:

Here is a simple server built using web.py (I have been playing with this for a while now, so this was a fun question to answer)

import web
import ystockquote

urls = (
    '/', 'index'
)
app = web.application(urls, globals())

class index:
    def POST(self):
        history = ystockquote.get_historical_prices(web.input()['stock'], web.input()['start'], web.input()['end'])
        head = history[0]
        html = '<html><head><linkhref="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" ><body><tableclass="table table-striped table-bordered table-hover"><thead><tr><th>{}<th>{}<th>{}<th>{}<th>{}<th>{}<th>{}<tbody>'.format(*head)
        for row in history[1:]:
            html += "<tr><td>{}<td>{}<td>{}<td>{}<td>{}<td>{}<td>{}".format(*row)
        return html

    def GET(self):
        return """<html><head><linkhref='//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css'rel=''><body><formmethod='POST'action='/'><fieldset>
                    Symbol <inputtype='input'name='stock'value='GOOG'/><br/>
                    From <inputtype='input'name='start'value='20130101'/><br/>
                    To <inputtype='input'name='end'value='20130506'/><br/><inputtype='submit'class='btn'/></fieldset></form>"""

if __name__ == "__main__":
    app.run()

Post a Comment for "Display Python Output In Html"