使用Bottle

时间:2015-08-03 22:15:32

标签: python html css bottle

我有大约100篇期刊文章。每篇期刊文章都表示为HTML页面。期刊的HTML页面,其css文件和作为期刊一部分的数字都位于其文件夹中。换句话说,每个html文件引用它自己的css文件和相应的图像文件。我创建了以下文件夹结构(|表示文件夹, - 表示文件):

-- app.py |views -- Disp_Res.tpl -- UI.tpl |static |articles |PMC45677 --PMC45677.html --jats-preview.css --fig1_45677.jpg --fig2_45677.jpg |PMC23456 --PMC23456.html --jats-preview.css --fig1_23456.jpg --fig2_23456.jpg

我的app.py

中有以下代码
@app.get('/articles/<pmc:re:PMC[0-9]*>')
def html_article(pmc):    
    global pmcno
    pmcno = pmc; 
    pmc_article = pmcno + ".html"; print pmc_article        
    rootdir = os.path.join('static/articles', pmcno) #'static/articles/{pmcno}'
    print "html_article", rootdir # This statement displays the right dir
    return static_file(pmc_article, root=rootdir)


@app.get('/<filename:re:.*\.css>')
def stylesheets(filename):
    rootdir = os.path.join('static/articles', pmcno)
    print "stylesheets", rootdir # This statement does NOT display
    return static_file(filename, root=rootdir)


@app.get('/<filename:re:.*\.(jpg|png|gif|ico)>')
def images(filename):
    rootdir = os.path.join('static/articles', pmcno)
    print "images", rootdir # This statement does NOT display
    return static_file(filename, root=rootdir)

不用说这不起作用。当我运行app.py时,它只是给我错误: &#34;抱歉,请求的网址&#39; http://localhost:8080/articles/PMC45677&#39;导致错误: 文件不存在。&#34;

知道我做错了什么吗?或者你有更好的想法实现我的目标?请告诉我。任何帮助将受到高度赞赏!

提前致谢。

1 个答案:

答案 0 :(得分:0)

也许是因为Bottle不知道/ articles /是什么?

尝试在代码顶部添加:

@app.route('/articles/<filepath:path>')
def file_stac(filepath):
  return static_file(filepath,root="./articles")