简体   繁体   中英

bottle web server: how to route to text file

I am writing a web app using bottle. I need to pass certain text files to a javascript function which process the file and displays an image on the web.

I would like to be able to make a route with a variable directory ie

./database/*/CONTCAR.xyz

so that i can call a url of the form ./database/6Ni@32Ag_npo/CONTCAR.xyz and get that CONTCAR.xyz returned, where "6Ni@32Ag_npo" will be different for each URL.

Here is what I have in my server

import bottle as b
@b.route('/database/<folder>/CONTCAR.xyz')
  def server_static(filename):
     return b.static_file( "CONTCAR.xyz" , root='./database/<folder>')

In my javascript I try to to call the url as follows:

<canvas class='xyz' url='/database/6Ni@32Ag_npo/CONTCAR.xyz' filetype='xyz'></canvas>

the xyz class is a class that allows me to process this CONTCAR file.

I get the following error:

TypeError: server_static() got an unexpected keyword argument 'folder' localhost - - [19/Jan/2014 13:10:46] "GET /database/6Ni@32Ag_npo/CONTCAR.xyz?uid=1390158646852 HTTP/1.1" 500 794

You're using the name folder in the route's path, but filename as the sole parameter name. Luckily, the fix is easy: just use the same name in both places. (And also correct your use of folder in static_file 's root param:

@b.route('/database/<folder>/CONTCAR.xyz')
  def server_static(folder):
     return b.static_file('CONTCAR.xyz', root='./database/{}'.format(folder))

For two levels of folders, you'd do something like this:

@b.route('/database/<folder1>/<folder2>/CONTCAR.xyz')
  def server_static(folder1, folder2):
     return b.static_file('CONTCAR.xyz', root='./database/{}/{}'.format(folder1, folder2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM