简体   繁体   中英

Rendering multiple files on bottle web server

I am trying to render a couple of files on Bottle web server. The first HTML file has a button which links to another file. So I need both of these to run on the server. My (updated)Project structure is something like this

   -app.py
   -static
         -css
               bootstrap.css
               bootstrap.min.css
         -fonts
         -js
               jquery.js
               etc etc

  -index.html    
  -visualization.html

My index.html file has to be rendered first. From which the user has to option to click on a button which takes him to visualization.html .

My pages are not rendering. What could be the reason for this?

The routing snippet from app.py is as show below:

from bottle import route, run, template, static_file, response, request

@route('/noob')

    def map():
        return static_file('index.html',root = './static')
    run(host='0.0.0.0', port='8030')

This is how I access those files in my index.html :

<script src="./static/js/jquery.js"></script>

  <link href="./static/css/grayscale.css" rel="stylesheet">

I'm relatively new to Python and Bottle . Is this right? I get a 404 error

.

Also, how to I place two files on the bottle server. As explained above, a button in index.html links to visualization.html. So I'm guessing this should also be running on Bottle server. Do I run it in the same file? Different port?

Thanks in advance.

You need to put index.html in static folder.

-app.py
-static
     various css and img files being used in my two html files
     index.html    
     visualization.html

A better way for accessing static files and templates would be to rename index.html to index.tpl like this:

-app.py
    -static
       -js
         bootstrap.min.js (example)
       -css
       -fonts
 index.tpl    
 visualization.tpl
 profile.tpl

And:

from bottle import route, run, template, static_file, response, request

@route('/noob')
def map():
    return template('index.tpl')

@route('/profile')
def profile():
    return template('profile.tpl')

@route('/static/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='./static/')

run(host='0.0.0.0', port='8030')

And in your tpl files use path like this:

<script src="/static/js/bootstrap.min.js"></script>

Hope this answers your question.

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