0

I went through many tutorials and everything worked. But I started my own website and I get only error 404 not found.

I downloaded a HTML template and managed to clone the files from Github to PythonAnywhere. Right now the structure of my files is: Screenshot

Flask_app.py Code:

from flask import Flask

# set the project root directory as the static folder, you can set others.
app = Flask(flask_app, static_url_path='/home/dubspher/mysite')

app = Flask(__name__)
@app.route('/home/dubspher/mysite/')
def static_file(path):
    return app.send_static_file(index.html)

if __name__ == "__main__":
    app.run()

Should I create another website with templates and use render_template or there's a way I can request a static page with flask?

So far I tried many codes but I don't know what to do with static_url_path

HTML if needed : https://www.pythonanywhere.com/user/dubspher/shares/db715c9b9d7c43b7beb4245af23fb56c/

Let me know if I need to add more informations.

5
  • Did you look at stackoverflow.com/questions/20646822/… Commented Jun 3, 2017 at 2:54
  • Yes this code came directly from this page. Commented Jun 3, 2017 at 2:57
  • The @app.route defines the url path that flask is going to be looking for in the request. So what URL are you requesting? You would have to be requesting yoursite.com/home/dubspher/mysite to hit your index page. Is that what you're requesting or are you requesting yoursite.com? If it's the latter, you want you route to be defined as @app.root('/'). Also, index.html needs to be in quotes. Commented Jun 3, 2017 at 3:10
  • Should I change app = Flask(name) with flask_app.py ? I can't see any wsgi or init file, is that a problem to deploy static pages ? Commented Jun 3, 2017 at 3:25
  • Sometimes I also got 500 Internal Server Error Commented Jun 3, 2017 at 3:30

1 Answer 1

1

Here is your code fixed up to serve up index.html at the root of your site (e.g. http://example.com/):

from flask import Flask

# set the project root directory as the static folder, you can set others.
app = Flask(__name__, static_folder='/home/dubspher/mysite/')

@app.route('/')
def static_file():
    return app.send_static_file('index.html')

if __name__ == "__main__":
    app.run()

That assumes that you have an index.html file in your /home/dubspher/mysite directory.

To follow up on your css question, you can get flask to serve up static files generically by passing in a static_url_path to the Flask constructor. When you do that, any request that comes in matching that static_url_path flask treats as a static file and serves it up based on the static_folder path. In the sample below, I've set static_url_path to static and the static_folder to /home/dubspher/mysite/. When a request for http://example.com/static/css/site.css comes in flask will serve up the file /home/dubspher/mysite/css/site.css.

from flask import Flask

app = Flask(__name__, static_url_path="/static", static_folder='/home/dubspher/mysite/')

@app.route('/')
def static_file():
    return app.send_static_file('index.html')

if __name__ == "__main__":
    app.run()

Here's an example of referencing a stylesheet at /home/dubspher/mysite/css/site.css from index.html.

<html>
<head>
<link rel="stylesheet" type="text/css" href="/static/css/site.css">
</head>
<body>
Hello There
</body>
</html>

If you do use the static_url_path, you need to be very careful that everything under your static_folder path is something you want to be accessible. Flask is going to serve it up as is.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.