How can I connect my python script with my HTML file?

人盡茶涼 提交于 2019-11-30 14:17:50

问题


Basically I am getting some data from a webpage, and putting it into an array, I want to output the contents of that array into a table in a HTML file. After some research I found that using a mako template might be the best solution, but I don't understand how to use it? Can any one guide me through the steps or offer a better solution to execute this python script and output its result on the web?

import urllib2
import mako
from bs4 import BeautifulSoup as BS

html = urllib2.urlopen("<link-to-web-page>")
soup = BS(html)
data = []


for each_course in soup.findAll('li',{'class':'<class-name>'}):
    inner_text = each_course.text
    data.append(inner_text)


for i in data:
    print (i+"\n")

回答1:


There are two subproblems in your problem:

  • Generate HTML to show your data
  • Serve that HTML

Mako can help you with the first one. For the second one there are different solutions available that depend on your situation.

Generate HTML

First you have to decide on a template, that means on the general skeleton in which your data will then be filled in. If you only want to show your data without any further information enigmas answer will work, but if it gets more complicated it is useful to use something as mako. How does a general template look like? Here is a really simple one:

<html>
<body>
Hello world!
</body>
</html>

This doesn't do very much. It's just like a single string. So lets get some python in it:

<html>
<body>
${x}
</body>
</html>

This template contains a variable which you will need to provide:

template = Template(filename="yourtemplate.template") # or how ever you named your template
print(template.render(x="Hello World!")

You will at least need for loops:

% for a in [1,2,3]
${a}
% endfor

This is the basic syntax for a loop. Of course you can do more complex things. Imagine mylist is a list of Person instances with a name and a age:

% for person in mylist
Name: ${person.name}
Age: ${person.age}
% endfor

You can use arbitrary HTML inside of that. Ofcourse mako can do more powerfull things, but a single stackoverflow post is to little space for that. You can read the basic usage and/or Syntax page of the mako language for mor information. But with the here presented structures you should be able to finish your task.

Serve HTML

You still need to somehow bring the HTML out to the web or where ever you want it. You have multiple possibilities that depend on what you want:

Static or dynamic

  • Is your data static? That means, will your data change in near time? If no, than you can simply generate the HTML on your local computer and then push the html to a simple webserver that serves html.

  • Is your data dynamic? That means your data changes often and it is not reasonable to powerup your local machine, run your script and then push the HTML. Instead you have to tell the server that serves your webpage to run your script whenever the data changes. There are more then one possibility to do that, you can use CGI (a webserver like nginx or apache calls your python script and serves the output) or a wsgi framework like django or flask or others. Of course these also need to be served, either from a "typical" webserver like apache or nginx or something like gunicorn

Lan or WWW?

  • If you only need it to be available in the LAN you can simply run a webserver on your local computer. If you do not expect much traffic and security is not a concern you could use the http server in the python standard library.

  • If you need it to be available on the web you need to look for a webserver. There are a few services that are free of charge for low traffic. To name a few: heroku which has a focus on python, so it's suited for the dynamic use case. Github pages where you can directly serve HTML from a github repository. I think it can only serve static HTML.




回答2:


data = [1, 2, 3, 4]


def data_to_html_table(data):
    html = '<table><tbody>'
    for item in data:
        html += '<tr><td>' + str(item) + '</td></tr>'
    html += '</tbody></table>'
    return html

print data_to_html_table(data)

results in html equivalent to

<table>
    <tbody>
        <tr>
            <td>1</td>
        </tr>
        <tr>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
        </tr>
        <tr>
            <td>4</td>
        </tr>
    </tbody>
</table>


来源:https://stackoverflow.com/questions/29734208/how-can-i-connect-my-python-script-with-my-html-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!