I have a file 'you.html' in static, how do I open it in my web2py app?

571 views
Skip to first unread message

Steve Joe

unread,
May 13, 2016, 11:14:45 AM5/13/16
to web2py-users
I tried the following but it didn't work.
in controller:

def rough():
    import os
    file = open(os.path.join(request.folder, 'static','index.html'))
    return locals()


in view:

{{extend 'layout.html'}}
{{=file}}

Kiran Subbaraman

unread,
May 13, 2016, 12:05:28 PM5/13/16
to web...@googlegroups.com
I would do it this way.. use the 'with' option, and return the file contents.

Controller:
with open(os.path.join(request.folder, 'static','index.html')) as x:
    file_contents = x.read()
return locals()

In the view:
{{=file_contents}}
________________________________________
Kiran Subbaraman
http://subbaraman.wordpress.com/about/
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
May 13, 2016, 12:13:41 PM5/13/16
to web2py-users
The static folder is for serving files directly. If you have a template you want to include inside a view, you should use the {{include}} directive. Put the html file somewhere in the /views folder, and then in the view of this action, do:

{{include 'mytemplate.html}}

Anthony

JoeCodeswell

unread,
Jun 9, 2017, 5:14:25 PM6/9/17
to web2py-users
Hi Mike.

Here's my hack.

static/anHTMLfile.html

<!DOCTYPE html>
<html lang="en">
<head>
 
<meta charset="UTF-8">
 
<title>anHTMLfile</title>
</head>
<body>
HELLO from anHTMLfile.html
<br/>

see
<a href="https://groups.google.com/forum/#!topic/web2py/Wqipxb_5-j4">https://groups.google.com/forum/#!topic/web2py/Wqipxb_5-j4</a>
</body>
</html>

controllers/default.py

def showHTMLFile():
   
'''
    https://groups.google.com/forum/#!topic/web2py/Wqipxb_5-j4

    '''
    import os
   
with open(os.path.join(request.folder, 'static', 'anHTMLfile.html')) as x:
        file_contents
= x.read()

   
return dict(file_contents=XML(file_contents))

views/default/showHTMLfile.html

{{=file_contents}}

Love and peace,
Joe

Ben Lawrence

unread,
Jun 11, 2018, 8:28:51 PM6/11/18
to web2py-users
And another way to do it:
no routers.py

app init

controller default.py
# ---- example index page ----
def index():
    return dict()

views index.html
{{=redirect(URL('static','pages/landingpage/index.html'))}}

and all the html css js  files and everything in the
static/pages/landingpage directory.


On Friday, May 13, 2016 at 8:14:45 AM UTC-7, Mike Stephenson wrote:

Anthony

unread,
Jun 12, 2018, 12:52:30 PM6/12/18
to web2py-users
views index.html
{{=redirect(URL('static','pages/landingpage/index.html'))}}

Note, that would not insert the contents of the static page into the view but simply redirect to the URL of the static page. If that is what you want, this would not be the most efficient or straighforward way to do so. Just do a redirect from the controller (though its not clear why you would create a controller whose sole purpose is to redirect to a static file).

Anthony
Reply all
Reply to author
Forward
0 new messages