Forums

accessing a static file in Bottle template

I have tried several iterations and read all I can find in this forum and googling but still can make this simple task work. I have the following link find the image.

have set the following on web config page.
URL DIRECTORY /static/ "/user/bsneddon/files/home/bsneddon/mysite/static"

<img src="/static/2-14.jpeg" alt="family picture" style="width:304px;height:228px;">

The file also exists in the static directory above.

I have tried with this code in my bottle_app.py

  `@route('/static/<filename>')

def server_static(filename): return static_file(filename, root='/user/bsneddon/files/home/bsneddon/mysite/static')

@route('/static/<filepath:path>') def server_static(filepath): return static_file(filepath, root='/user/bsneddon/files/home/bsneddon/mysite/static')

The render template code is as follows

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

Have also tried above with /home/bsneddon/mysite/static path.

Bill

With /static => /home/bsneddon/mysite/static in your static files mappings and /static/2-14.jpeg as the URL of the image, it should work.

Glenn,

I edited the mapping and removed trailing forward slashes to match your example. I reloaded the site still did not work.

I remove the code from bottle_app.py with the static routes and reloaded the site still did not work. The template is in the views directory not the static directory. Any other thoughts?

Thanks again for your help.

Bill

Now working not sure what I changed but cut and pasted url from file download link.

Bill

Cool. Glad to hear it.

For people working with the bottle framework attached is an example that includes access to static files, view files and also compatibility between a windows local host and pythonanywhere. So, now, I can prototype on my windows localhost, sync the files to github, and then 'git pull origin master' from the pythonanywhere console to get everything to pythonanywhere, reload the web app, and everything works. (I'm leaving out configuring the DNS info at your domain name provider - in my case GoDaddy).

I'll do four posts here. 1) The file layout for the web app 2) Skeleton of the app code with some comments 3) The wsgi file code 4) index.html snippets

Using all four of these together should let you replicate this local development / pythonanywhere deployment and also help you get started with a bottle framework on PA. Note that there is a very nice suggestion somewhere else in the forums to build the default pythonanywhere bottle app, and then use those created files as templates to get your app up and running. This can be very helpful for pythonanywhere applications without the windows localhost compatibility.

First the file layout, here the leading '/' marks a directory, and this shows two files favicon.ico and index.html, a real site would have more files than just these two:

    /webapp
           - - -  webapp.py
            - - - /static
                   - - - /css
                   - - -/font
                   - - -/images
                            - - - favicon.ico
                        /js
            - - - / views
                      - - - index.html
# this is the skeleton app code
from bottle import route, run, template, static_file, url, default_app, request, get, post
import bottle
import os
import sys

def calculate_something(input_data):
    return "the answer"

path = os.path.abspath(__file__)
dir_path = os.path.dirname(path)

if(sys.platform == 'win32'):
    templates_dir = os.path.join(dir_path, 'views')
    if templates_dir not in bottle.TEMPLATE_PATH:
        bottle.TEMPLATE_PATH.insert(0, templates_dir)
    # the code above allows the same file layout to be used on the localhost and 
    # pythonanywhere site. In the app directory is app.py and two directories
    # static and views.  Static has the css/js/images, views contains index.html
    # on pythonanywhere the bottle.TEMPLATE_PATH is set in the app_wsgi.py file
    # located at /var/www

@route('/')
def home():
    ''' A bit of documentation
    '''
    return template('index.html')

@route('/<filename:path>')
def send_static(filename):
    ''' This makes the extant template start working
       Woo-Hoo!
    '''
    return static_file(filename, root=dir_path+'/'+'static/') 
    # the dir_path+'/'+ needed to be added to get this to serve static pages on PythonAnywhere
    # also I had to create a 'views' directory and put the index.html file into the views directory

@route('/hello')
def hello():
    ''' A bit of documentation
    '''
    return '<h1>Hello World!</h1>'

@route('/hello/', method='GET')
def hello():
    ''' A bit of documentation
    '''
    return '<h1>Hello World (two slash...) !</h1>'

@route('/location', method = 'POST')
def location():
    return calculate_something(input_data)

#
# the lines below recommended by PythonAnywhere forum for Bottle webapp
#

application = default_app()
if __name__ == '__main__':
    bottle.debug(True)                              # remove this for production
    bottle.run(host='0.0.0.0', port=8080)
# This is the WSGI file - for local host compatibility
# This file contains the WSGI configuration required to serve up your
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Bottle project
import bottle
import os
import sys

# add your project directory to the sys.path
project_home = u'/home/your_name/bottle/webapp'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# make sure the default templates directory is known to Bottle
templates_dir = os.path.join(project_home, 'views/')
if templates_dir not in bottle.TEMPLATE_PATH:
    bottle.TEMPLATE_PATH.insert(0, templates_dir)

# import bottle application
from webapp import application
Below is partial index.html showing paths consistent with the webapp directory structure above
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Your_Title</title>
<link href="/images/favicon.ico" rel="shortcut icon">
<link href="css/style.css" rel="stylesheet" type="text/css">
<script src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/smoothscroll.js"></script>
</head>

Note: for the four posts above, I have not run exactly the code and html in the posts above, these are mostly edited down versions of running bottle files. It's possible I've introduced an issue or issues with the editing...

Hopefully this information will still be useful

Cool. Thanks for that, ydnayabe

One other setup/configuration item: corresponding to the files / postings above there is a performance optimization possible with PA by configuring the static files on the PA Web tab.

In the web tab, static files section enter this:

Under the URL heading: enter /static/

Under the Directory heading: enter /home/your_name/bottle/webapp

And for me, the webapp directory is located in a bottle directory under my home directory