LPTHW ex.50 - where to put files? Can'r run app and index

#1

I have created app.py and index.html in ex.50 but when I run app.py I do not get my html page returned*.

Is it because things aren’t in the right place?

Current folder/file structure is:

projects/
    gothonweb/
        bin/
        docs/
        gothonweb/
            __init__
            app.py
        templates/
            index.html
        tests/

(* What I do get is output from the first iteration of app.py, which is “Hello, {greeting}!”, but that’s another story)

#2

Hello @S2Stats

Put index.html in the templates folder.
And all other html files.
Then it should work.

#3

It already is. I edited the folder/file structure to show it more clearly

#4

Hello @S2Stats

How about your

 __init__ 

?

Did you just miss the “.py” in this post?
Do you have an empty file called:

 __init__.py 

in your ex50 folder (same folder as app.py)?

This file help python to find modules.

#5

Yes, I think that’s the problem. Try that @S2Stats and let us know if that works.

#6

Yes, it’s there, and with the .py suffix. I just forgot to put it in my schematic. image

#7

Other possible causes:

  1. you cd into gothonweb to run app.py. Everything in python works from the current directory so if you try to open templates/index.html, but you cd into gothonweb, then it won’t be able to access templates/
  2. You didn’t configure flask correctly to use templates. What’s your flask file contents?
  3. You aren’t telling flask to load the right file. Maybe you tell it templates/index.html instead of index.thml.

Try those.

#8

I’m getting v confused now. I’ll set out what I do so you can hopefully see where I’m going wrong.

This is with the folder/file structure set out at the top of this page.

In PowerShell I run the virtual environment using this code:

.\.venvs\lpthw\Scripts\activate

From the prompt I cd to project/gothonweb/gothonweb. From here I run:

python app.py

That starts the Flask server running.

I go to the browser and connect to http://localhost:5000/

The browser returns this output, which is the same as when I ran the first version of app.py from page 217 of the book.

Hello, {greeting}!

This is confusing on several levels because app.py is returning output for code that no longer exists (which doesn’t work properly, with {greeting} not formatting properly).

Maybe I can’t get v2 of app.py to run because the first version is cached or something?

This is the current code for app.py and index:
app.py

from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route("/")
def index():
    greeting = "Hello World"
    return render_template("templates/index.html", greeting=greeting)
    
if __name__ == "__main__":
    app.run()

index.html

<html>
    <head>
	    <title>Gothons of Planet Percal</title>
    </head>
<body>

{% if greeting %}
    I just wanted to say
	<em> style="color: green; font-size: 2em; ">{{ greeting }}</em>
{% else }
    <em>Hello</em>, world!
{% endif %}

</body>
</html>
#9

This is the problem. You don’t cd down into lower directories. You stay in the root of the project always. You would run it like this:

python gothonweb/app.py

You do not cd into gothonweb at all. This is true for tests too. People do this:

cd tests/
nosetests

But that’s wrong. You stay above tests and never go down. I’m not sure why people think you have to do this but I suspect it’s because throughout the book they run only files in the current directory like:

python ex30.py

But I should maybe throw in some times they do:

python exercises/ex30.py

To make sure they know they can run things above the script.

#10

That works, thanks.

But I still get the same output as before in the browser:

image

So, why isn’t app.py accessing index.html?

Also, in PowerShell I don’t seem to see any evidence of requests to Flask

image

There are no lines about GET requests etc

I’m v stumped and a bit fed up I’m having to ask for help on this, but thankyou very much for your assistance.

#11

I have a suggestion that’s going to sound wild and counter-intuitive but I want you to try it. TRUSSSST me, this totally works, and you’ll be way better off if you do it.

  1. Any time you get frustrated it’s time for a break. I literally want to throw my computer out a window daily. I go paint or play death metal on my guitar instead.
  2. This is especially a good time to go to sleep. Your brain keeps working on the problem while you sleep and many times the answer pops in your head.
  3. Stop running for help at every error. You have to spend time on your own trying to solve it. Take some time with this to figure it out.
  4. Email yourself an explanation of the problem and you many times figure it out. I’m not kidding.
  5. Figuring it out might mean shifting your attitude from “it’s not working” to “oops, I did something wrong”. It’s subtle, but the “it’s not working” attitude acts as if it got that way magically on its own. The “oops, I did something wrong” attitude forces you to go back and confirm everything you did.
  6. If you go through all of my instructions again and still can’t see, ERASE AND START OVER. It sounds insane but part of the problem is people code code CODE, and then nothing’s working because they didn’t build it up gradually. I can tell you did this because if you built it up gradually then you would have been using the correct command from the beginning. Instead, you typed a ton of code and then it doesn’t work, but that’s why it doesn’t work. Erasing what you did and then building it slowly and making it work in little pieces as you go will help you get right.

Now, #5 sounds nuts but that’s how I code. If I’ve been coding and the path I’m going down is full of errors and not working I nuke it and start over. That first attempt is just a study and I use what I learned about my mistakes there to do it better the second or even third time. It’s better to start over rather than beat your head against a pile of poorly written sloppy code.

Starting over also has the ability to help you get over the idea that you can’t do it again reliably. One thing about programming is if you did it once you can do it again, but most people don’t attempt the same thing multiple times so they don’t realize this. If it was really hard to make they assume it was all magic and luck and then don’t want to touch it for fear of failing at it. But, the reality is your progress on is a learning process, so if you did it once you learned something, and then the next time you do it you get even better.

So, try to solve it like this, with the assumption that you did something wrong, not “it’s not working”, and if you can’t figure it out trash that garbage and start over. Your second attempt will work a lot better.

Finally, build this in little pieces. Get a basic app that loads an html. Then put text up. Then one room. Then a bit more, and so on until it’s working. I NEVER ever ever never ever write tons of code before I run things. I and every single professional programmer who is competent builds things in small pieces and constantly runs it as they go. So try that. Think of it like building a house. You don’t just throw all the wood and cement onto the lot and try to stack it correctly. You go in small increments of the foundation, then framing, then roof, then wiring, then sheet rock, and and you walk in and around and test what you made as you go. You don’t erect a huge pile of wood and cement–never walking into it or testing anything once–then expect it to be a solidly built house. Same goes for code.

#12

I guess you’re right. I’ll tear it down. Maybe go back a chapter or two, look elsewhere to get some other perspectives etc. Thanks

A free service run by Zed A. Shaw for learncodethehardway.org.