我正在尝试制作我的第一个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
如何使列表显示在网页上?我对为什么响应显示在日志中而不是页面上感到困惑。
该photoCollect()
方法输出输出:
print photoList
打印将数据写入sys.stdout
,在运行WSGI服务器时,该数据将重定向到日志。
您想返回 photoList
:
return photoList
如果不使用return
语句,则默认返回值为None
,并Response(None)
导致空白页。
随着return
对调用者photoCollect()
函数实际上接收该列表。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句