Simplest tutorial apps fails, seems to be missing fixture

62 views
Skip to first unread message

Tom Campbell

unread,
Jun 30, 2020, 11:27:38 PM6/30/20
to py4web

Super excited to see py4web docs up. It's been too long!

The Create your first app from scratch appears to be missing a fixture.

# RUN PY4WEB
$ py4web version
1.20200517.1
$ cd ~/code/py4web
$ mkdir apps/myapp
$ echo '' > apps/myapp/__init__.py
$ py4web run
Logged in; password worked, dashboard showed what it should.

# CHECK FOR FILES
Checked for myapp in the dashboard. It's there. The static dir is there with hello.txt

# TRY TO VIEW APP
Got the big orange/red screen with 404 NOT FOUND

I think it now needs a fixture and that perhaps it wasn't required when the tutorial was written. I'm imagining an__init__.py with something not exactly like this:

from py4web import action          
                                                                                                                                                             
@action("index")                                                                
def index():                                                                    
   
return static/hello.txt    # <-- Don't know how to create an action that says "load the file static/hello.txt



What's the best fixture to show the contents of a static file?

Tom Campbell

unread,
Jun 30, 2020, 11:42:44 PM6/30/20
to py4web
Forgot to mention that I tried web2py-like variations along these lines:

@action.uses(Template('static/hello.txt', delimiters='[[ ]]')                          
def index():                                                                    
   
return (dict=dict)

Massimo

unread,
Jul 1, 2020, 1:57:13 AM7/1/20
to py4web
The handler for static files is built-in that means anything in static is automatically served by py4web. You do not need any action.
py4web (currently) is based on bottle.py so you can use from bottle import static_file

you can also do:

@action("index")                                                                
def index():
    filename = os.path.join(os.path.dirname(__file__), 'static/hello.txt')
    with open(filename) as fp:                                                                  
       
return fp.read()


or

@action("index")                                                                
def index():
    redirect(URL('static/hello.txt')) # and use the build-in handler

Tom Campbell

unread,
Jul 1, 2020, 5:19:06 AM7/1/20
to py4web
LOVE that second solution. Should have grokked it myself. Thanks tons. But if no action was needed why did it issue a 404 originally?
Reply all
Reply to author
Forward
0 new messages