我正在尝试制作我的第一个Pyramid应用程序,而且我认为我在回复方面有一个基本的挂断。
我有一个元组列表,我想将它们打印到网页,但是网页是空白的,而列表则显示在终端窗口中。整个应用程序:
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from photoCollect import photoCollect
def printPhotos(request):
photoTable = photoCollect()
return Response(photoTable)
if __name__ == '__main__':
config = Configurator()
config.add_route('productlist','/productlist')
config.add_view(printPhotos, route_name='productlist')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
和photoCollect
定义为:
import urllib
import pandas as pd
def photoCollect():
# pull photo directory
mypath = "http://gwynniebee.com/photos"
mylines = urllib.urlopen(mypath).readlines()
# strip down web page to retain just photo names
photonames = []
for item in mylines:
if ".jpg" in item:
k = item.replace('<',' ')
splitItem = k.split(" ")
for x in splitItem:
if "=" not in x and ".jpg" in x:
photonames.append(x)
photoFrame = pd.DataFrame(photonames, columns=['name'])
# break photo names into vendor-color-order
photoFrame['name2'] = photoFrame['name'].apply(lambda x: x.replace('.jpg',''))
s = photoFrame['name2'].apply(lambda x: pd.Series(x.split('-')))
# concat vendor-color into style
s['style'] = s[0] + "-" + s[1]
s['order'] = s[2]
photoFrame = photoFrame.join(s)
# find first photo for each style and recreate photo name
styleMin = photoFrame.groupby('style')['order'].min()
photoName = pd.DataFrame(styleMin)
photoName = photoName.reset_index()
photoName['name'] = photoName['style'] + "-" + photoName['order']+".jpg"
# generate list to send to web
webList = pd.DataFrame("http://gwynniebee.com/photos/"+ photoName['name'])
webList['style'] = photoName['style']
webList = webList.set_index('name')
photoList = list(webList.itertuples())
print photoList
如何在网页上显示列表呢?我很困惑为什么响应显示在日志中而不是页面上。
答案 0 :(得分:4)
photoCollect()
方法打印输出:
print photoList
打印将数据写入sys.stdout
,在运行WSGI服务器时将其重定向到日志。
您希望返回 photoList
:
return photoList
如果没有return
语句,则默认返回值为None
,而Response(None)
会导致空白页。
使用return
photoCollect()
函数的调用者实际接收该列表。