Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
flask
#1
a friend showed me how he was setting up web pages in flask.  one thing about how this was done bothered me.  every time he added a new page, in addition to adding the file for it in the file system he added a path for it in another place (that was specific to flask).  i asked what would happen if did not add that path.  he said it would get a 404 (file not found) error.  this does not follow one of the principles i use in all my programming and system/network administration.  that is that each thing that is configured should be in one place.  the idea is that it is not necessary to keep different things in sync.  does flask really need to work that way?  if it does, is there an alternative?  i have built a couple web sites in which web page files (including CGI) would come and go as needed (various processes would add, delete, or replace them.

at the time i wanted to use python (instead of C) to put together a web site for a major client.  so i put together my own web server in python to do it.  the site was internal, only, so high performance and security were not issues.  it all ran under one single system user (all files were readable by that user).  new pages could be added just by adding the file ... a python script (because this was all about a database or administration data for then hosts it ran on) which was a module import-loaded file with a specific function (named handle_http_request).

what does WSGI impose for this?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
well, you need to tell your app the url (e.g. /somepage, that it will translate to the full url, e.g. approot/somepage, by including the approot) and of course you need a static file for the page it will render. So basically it's one place where you configure the app/url and the other thing is the page itself. Depending on the app, you may render one page with different URLs by supplying different data.
flask is an excellent micro-framework and one can start very quickly. see the snippsat tutorial
also this mega-tutorial
Reply
#3
i don't have to do that with a regular web server. it get a requested url from the client. it maps the url into a directory space designated to have the files. that ends up with a remaining file path that is use relative to the designated directory to access the file. if the file is there it either runs it or sends it. if it is not here, sends that infamous 404 error. simple enough, but this is not what flask does.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
@app.route('/<path>')
@app.route('/cubdir/<path:path>')
def srvpath(path):
    pass

@app.route('/subdir/<path:filename'>)
def srvfile(filename):
    pass
Something like this.
I can't tell why is double decorated and how all this works and what is doing. I don't know Flask. But I came across this two days ago. I've saved the web page but can't remember where exactly.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Apr-04-2017, 12:46 AM)Skaperen Wrote: that is that each thing that is configured should be in one place.
It's all in one place the Application Object
Design Decisions in Flask

Quote:in addition to adding the file for it in the file system he added a path for it in another place (that was specific to flask).
You add file in the Application Object folder and path in html,i can not see problem at all.

To show some code so maybe it's cleaner or not.
my_page holds all files for flask.
Folder structure my_page(this name is free of choice).
my_page\
app.py\
 templates\
   index.html
   about.html
   404.html
 static\ #CSS,JS,images ..ect goes in here in own folders
app.py:
from flask import Flask, render_template, jsonify, request

app = Flask(__name__)
@app.route('/')
def index():
   return render_template("index.html")

@app.route('/about',  methods=['GET'])
def about():
   return render_template("about.html")

@app.errorhandler(404)
def page_not_found(e):
   return render_template('404.html'), 404

if __name__ == '__main__':
   app.run()
index.html:
<!doctype html>
<html>
  <head>
    <title>Some title</title>
    <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" />
  </head>
  <body>
    <div id="start">
      <h1>Start page</h1>
      <ul>
        <li class="page_1"><a href="/about">About</a></li>
      </ul>
    </div>
  </body>
</html>
about.html:
<div class="foo">
  <h1>The About Page</h1>
  <p>Learn flask</p>
</div>
404.html:
<h1>server got 404</h1>
Running python app.py in browser http://127.0.0.1:5000/
So running it all should work fine.
Wrong url eg http://127.0.0.1:5000/about99 then [email protected](404) get run.
All files live in my_page,if a file should be missing or wrong name name about99.html the server would return 500.
Reply
#6
the path in the filesystem that you use when you store the file there is one of the pieces of information.  information you put in the application object is another.  if they must be the same, then that is the "two places" issue.  what i seek in a web server is the ability to have one directory tree for a site and by just putting the file there is the only thing that needs to be done to make that URL work.  when i watched my friend do this, i saw that extra step of adding the file path string to flask as an extra burden.  i never had to do that for HTML or CGI under apache or PHP (a system i want tp get away from for other reasons).  i hope i don't have this issue with WSGI.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
That's the whole point of using wsgi, though. You don't want one url == one file. ALL urls get directed to a single file (normally something like index.py or main.py), which then uses pattern matching (some variant of regular expressions) which matches the url requested with a callable object. In Django, that's the urls.py file. In flask, the convention is to let decorators handle that for you.

But that's not unique to wsgi. Wordpress (which is php) works the same way. As do most php frameworks. Our forum uses a similar setup. It's the main way websites have pretty urls.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020