1

i started teaching myself the basics of web development with python in Google App Engine and webapp2 web frame.

Basically, i would like to create a homepage where i'll post all the links to different projects. each link will direct to a new url where the relevant py file will run.

for now, all i want is to have one link that direct to a Hello World page. that's it. and for the life of me i can't understand how to write the handler for this event (do i even need a hadler?). can someone please tell me what i'm doing wrong?

my files structure is:

+Main Directory (Folder)
    - app.yaml
    - index.py
    +helloworld (Folder)
        __init__.py
        helloworld.py

app.yaml file:

application: untitam
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /
  script: index.app

- url: /helloworld.*
  script: helloworld.app

- url: /.*
  script: index.app

libraries:
- name: webapp2
  version: latest

the index.py:

import webapp2

menu=""" <nav>
<ul>
<li> <a href="/helloworld">Hello World</a></li>
</ul>
</nav>
"""

class HomePage (webapp2.RequestHandler):
    def get(self):
        self.response.out.write(menu)

class HelloHandler(webapp2.RequestHandler):
    def get(self):
        pass

app = webapp2.WSGIApplication([('/', HomePage),
                               ('/helloworld', HelloHandler)], debug=True)

and the helloworld.py:

import webapp2

class HelloWorld(webapp2.RequestHandler):

    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!')

application = webapp2.WSGIApplication([('/helloworld', HelloWorld),], debug=True)

when i press the Hello World link i do get refer to localhost:8080/helloworld but i see a blank page. the log says: ImportError: No module named app

what should i write in the index.py for the helloworld to run after the user press the link. notice that that index.py and helloworld.py are not in the same folder. each project will have its own folder since later i'll use html/css templates and some javascripts.

thanks in advance

tamiros
  • 339
  • 1
  • 4
  • 10
  • 4
    no module named app is because in helloworld.py you are creating "application" not "app". I suggest you start with a simpler, working example derived from a demo and adjust it step by step until it does what you want, and/or read the tutorial here: http://webapp-improved.appspot.com/guide/routing.html – Paul Collingwood Jan 26 '14 at 11:08
  • Thank you for the reply. i worked the tutorial but couldn't get an answer to my problem. my guess is that i don't know how to tell python to import another file from a different folder. it might be because i'm a noob but browsing though this site i still couldn't figure out how to do it. Any thoughts / notes to my code? – tamiros Jan 26 '14 at 17:47
  • The problem is not importing files from other folders. do this. https://www.udacity.com/course/viewer#!/c-cs253/l-48230541/e-48689147/m-48646718 – Paul Collingwood Jan 26 '14 at 18:24
  • @PaulCollingwood, thanks. But i don't want to just write "Hello World!". i'm interesting in how one can link to another script in a different url. the "helloworld".py is just an example to that other script. – tamiros Jan 27 '14 at 10:36
  • 1
    Best of luck with all that then. – Paul Collingwood Jan 27 '14 at 10:39
  • as to your code, you don't seem to understand what relationship the "app" you are creating has to the app.yaml etc. This is why I'm suggesting you start with a tutorial such as the webapp2 routing guide. – Paul Collingwood Jan 27 '14 at 11:41

1 Answers1

1

As Paul said, I would also start out with a simpler example with a single webapp application (which could still handle multiple URLs). With the following changes your example should work, though:

app.yaml file:

- url: /helloworld.*
  script: helloworld.helloworld.application

helloworld.helloworld.application actually refers to the application variable defined in helloworld.py in the helloworld package (in index.py it is named app instead).

You can then delete the HelloWorld route from index.py since /helloworld is routed to helloworld.py as defined in app.yaml:

the index.py:

app = webapp2.WSGIApplication([('/', HomePage)], debug=True)
oliverdm
  • 381
  • 3
  • 10