Trying to return a static file in web.py but getting "not found" error message

232 Views Asked by At

I have a web.py server hosted on pythonanywhere.com doing some handy things with python.

Now I'd like to just serve a straightforward html file from the same server i.e. just return the contents of a static html file to the client

The comments/answers below state that it should be possible, out of the box, to serve static files in the static directory, located in the same directory as the main python file which contains the following :

import web

urls = (
    '/', 'hello'
)

app = web.application(urls, globals())

class hello:
    def GET(self):
        return 'Hello, Joe'

if __name__ == "__main__":
    app.run()

The server above works fine, when I go to http://myhost/ it displays "Hello , Joe". The directory static exists and contains a file small.jpg but when I try the url http://myhost/static/small.jpg it gives me "not found"

Previous text of question up to Nov 9th 2022 is below :

original question title : Trying to return a html file in web.py but getting "No template named ....." error message

So I've looked at the web.py documentation on serving static files and templating and I think the following code should work :

import web
render = web.template.render('static/')
# have also tried render = web.template.render('/full/path/to/static/')
urls = (
    '/getlatlongEIRCODE', 'getlatlongEIRCODE', #other stuff
    '/getlatlongGOOGLE', 'getlatlongGOOGLE',    #other stuff
    '/getmonthlyPV', 'getmonthlyPV',   #other stuff
    '/Tomas', 'Tomas',
)

class Tomas:
    def GET(self):
        return render.Tomas()

I have created a folder static at the same level as my file above (which works fine for the other scripts) and i have created a file Tomas.html in the static folder containing

<h1>Help me</h1>

However I get an error message when I go to https://example.com/Tomas

<class 'AttributeError'> at /Tomas
No template named Tomas

P.S. From the static files page it seems to say I should just be able to put the Tomas.html file in a folder called "static" and then access is via https://example.com/static/Tomas.html but that is not working (it returns "not found")

3

There are 3 best solutions below

0
carbontracking On BEST ANSWER

The case of the disappearing /static/ directory was related to the fact that I'm hosting on pythonanywhere.com

Even though the web.py documentation says that the /static/ folder is plugged in by default, that's not the case in pythonanywhere and you need to expressly make the link between the url http://yourhost/static/ and /path/to/static in the Web part of the dashboard.

3
Glenn On

You're using a relative path to your template directory without paying attention to the working directory. See https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

2
pbuck On

You're working too hard. 'static' is built in.

As the documentation says, http://localhost/static/logo.png will return the file logo.png from the existing directory static, which is relative to your webserver root.

Do not use render() for this (not needed). Also, do not list your desired file ('/Tomas') in the urls list (not needed).

Anything under the static directory can be accessed with the url https://localhost/static/...

"static" is hardcoded in the web.py server, so you cannot (easily) change this to some other folder. The suggestion in the web.py documents is to have nginx or apache host your application and use an Alias there to go to web.py static. (I think you can also add StaticMiddleware to your web.py application, but you'd need to investigate that yourself -- look at web.application.run()