norman
norman

Reputation: 5556

HTML file response in Pyramid

I am trying to adapt the to-do list app in the Pyramid tutorial here to create a Python hangman game. My directory structure is as follows:

/tasks
    tasks.py
    /static
        custom.css
    /templates
        main.html

I want to have a single view main.html (i.e., just the game title, the current picture with blanks, score, etc., and a button for each letter to guess) that gets updated every time the user chooses a letter.

I figured I could do this by dynamically creating a new HTML display with each button press, similar to the jpg serving method shown here. But upon running python tasks.py and opening http://localhost:8080/# in the browser, it gives a server error and: "OSError: [Errno 2] No such file or directory: '/templates/main.html' in the terminal.

Here's my tasks.py:

import os
import logging

from pyramid.config import Configurator
from pyramid.events import NewRequest
from pyramid.events import ApplicationCreated
from pyramid.exceptions import NotFound
from pyramid.httpexceptions import HTTPFound
from pyramid.response import FileResponse
from pyramid.view import view_config

from wsgiref.simple_server import make_server

logging.basicConfig()
log = logging.getLogger(__file__)

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

#@view_config(route_name='home', renderer='main.mako')
#def main_page(request):
#    print request #just checking
#    return {}

@view_config(route_name='home')
def main_page(request):
    response = FileResponse(os.path.join(here,'/templates/main.html'), \
                            request=request, content_type='text/html')
    return response

if __name__ == '__main__':
    settings = {}
    settings['reload_all'] = True
    settings['debug_all'] = True
    settings['mako.directories'] = os.path.join(here, 'templates')


    config = Configurator(settings=settings)
    config.include('pyramid_mako')
    config.add_route('home', '/')

    config.add_static_view('static', os.path.join(here, 'static'))

    config.scan()

    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 8080, app)
    print 'server made'
    server.serve_forever()

Considering the here declaration, I don't see how the file can't be found. As you can see from the commented out view_config, I tried using my initial HTML file (renamed main.mako) as the renderer at first, which worked fine and displayed the main page nicely.

But I don't think I can use that to update dynamically as desired. What am I missing here? Maybe I need to add something else to the settings dictionary?

Upvotes: 2

Views: 1904

Answers (1)

Slava Bacherikov
Slava Bacherikov

Reputation: 2066

You getting this error because you are using absolute path /templates/main.html. Simple example how os.path.join works:

>>> os.path.join('/some/path/to/project', '/templates/main.html')
'/templates/main.html'
>>> os.path.join('/some/path/to/project', 'templates/main.html')
'/some/path/to/project/templates/main.html'

So try change os.path.join(here,'/templates/main.html') to os.path.join(here,'templates/main.html').

Upvotes: 2

Related Questions