Re: [flask] Re: Newbie.. interacting with another python program
- From:
- Chengi Liu
- Date:
- 2013-02-09 @ 23:43
Hi.
Sorry for not being able to convey my problem..Ahh..
main.py
def main_program(file):
home(file)
if __name__ == "__main__":
main_program(foo.txt)
routes.py
app = Flask(__name__)
@app.route('/<file>')
def home(file):
print file
contents = open(file).read()
contents = contents.decode('utf-8')
return render_template('raw_text.html', contents=contents,file=file)
app.run()
---------------
Now. i do python main.py
It throws an exception exception occured 'NoneType' object has no
attribute 'app' <type 'exceptions.Exception'>
And then if i go to localhost:5000/foo
All I want is somethng very simple.
I start the flask server..
i run the main.py..
and display the contents on the browser.
Please help me.
On Sat, Feb 9, 2013 at 1:00 PM, Andy D'Arcy Jewell
<andy@wild-flower.co.uk>wrote:
> On 09/02/13 20:32, Chengi Liu wrote:
>
> I want to do both actually..
> Closing the window and ultimately cleanly shutting down the server as well.
>
> Hi Chengi Liu,
>
> When you say "cleanly shutting down the server" do you mean just the
> connection to the client, or the web-server itself, or the operating system
> (I assume the first, as I don't think the other two really make any sense).
> If I am right, the you probably don't need to worry, as the
*request*will have already "shut down" (in a sense) by the time the user
sees the
> rendered web page in their browser - the connection doesn't really last any
> longer than it takes to deliver the data to the client, although the same
> *process* will be re-used to server other subsequent requests. It's not
> like having a remote-desktop connection to the server.
>
> If you *do* want to shut down the server, I'd be interested to know why?
>
> If you are trying to provide "real-time" feed-back to the user, there are
> better ways of doing this, but you will have to keep the "state" somehow on
> the server side, and maybe use some simple javascript to poll the server
> state and notify the browser, perhaps by refreshing the page, when it sees
> a change. I wrote a program recently that did just that, see:
> https://gitorious.org/snippit/, if you're interested.
>
> -Andy
>
Re: [flask] Re: Newbie.. interacting with another python program
- From:
- Andy D'Arcy Jewell
- Date:
- 2013-02-10 @ 00:08
On 09/02/13 23:43, Chengi Liu wrote:
> Hi.
> Sorry for not being able to convey my problem..Ahh..
> main.py
> def main_program(file):
> home(file)
> if __name__ == "__main__":
> main_program(foo.txt)
>
> routes.py
>
> app = Flask(__name__)
> @app.route('/<file>')
> def home(file):
> print file
> contents = open(file).read()
> contents = contents.decode('utf-8')
> return render_template('raw_text.html', contents=contents,file=file)
>
> app.run()
> ---------------
>
>
>
> Now. i do python main.py
>
> It throws an exception exception occured 'NoneType' object has no
> attribute 'app' <type 'exceptions.Exception'>
>
> And then if i go to localhost:5000/foo
>
> All I want is somethng very simple.
>
> I start the flask server..
> i run the main.py..
> and display the contents on the browser.
>
> Please help me.
>
Have you tried putting main_program() and home() in the same python
file? __name__ will refer to different objects in each of the modules -
I'm assuming you left out the "import routes" part in main.py?
__name__ refers to the name of the module in which it is defined - which
would be "routes" for the "app = Flask(__name__)" statement, but you
really need it to refer to "main", which might be where your problem is
coming from.
hth
-Andy
Re: [flask] Re: Newbie.. interacting with another python program
- From:
- Chengi Liu
- Date:
- 2013-02-10 @ 00:30
Hi.. the main.py has import routes
But.if i declare app = Flask(__name__) in main.py
how will routes.py work..
I mean on top of every view function is a decorator function
Do you mean something like
main.py
def main_program(file):
home(file)
if __name__ == "__main__":
* app = Flask(__main__)*
main_program(foo.txt)
*app.run(debug=True)*
routes.py
app = Flask(__name__)
@app.route('/<file>')
def home(file):
print file
contents = open(file).read()
contents = contents.decode('utf-8')
return render_template('raw_text.html', contents=contents,file=file)
??
Kindly explain me with an example.
Thanks
On Sat, Feb 9, 2013 at 4:08 PM, Andy D'Arcy Jewell
<andy@wild-flower.co.uk>wrote:
> On 09/02/13 23:43, Chengi Liu wrote:
> > Hi.
> > Sorry for not being able to convey my problem..Ahh..
> > main.py
> > def main_program(file):
> > home(file)
> > if __name__ == "__main__":
> > main_program(foo.txt)
> >
> > routes.py
> >
> > app = Flask(__name__)
> > @app.route('/<file>')
> > def home(file):
> > print file
> > contents = open(file).read()
> > contents = contents.decode('utf-8')
> > return render_template('raw_text.html', contents=contents,file=file)
> >
> > app.run()
> > ---------------
> >
> >
> >
> > Now. i do python main.py
> >
> > It throws an exception exception occured 'NoneType' object has no
> > attribute 'app' <type 'exceptions.Exception'>
> >
> > And then if i go to localhost:5000/foo
> >
> > All I want is somethng very simple.
> >
> > I start the flask server..
> > i run the main.py..
> > and display the contents on the browser.
> >
> > Please help me.
> >
> Have you tried putting main_program() and home() in the same python
> file? __name__ will refer to different objects in each of the modules -
> I'm assuming you left out the "import routes" part in main.py?
>
> __name__ refers to the name of the module in which it is defined - which
> would be "routes" for the "app = Flask(__name__)" statement, but you
> really need it to refer to "main", which might be where your problem is
> coming from.
>
> hth
> -Andy
>
Re: [flask] Re: Newbie.. interacting with another python program
- From:
- Andy D'Arcy Jewell
- Date:
- 2013-02-10 @ 08:42
On 10/02/13 00:30, Chengi Liu wrote:
> Hi.. the main.py has import routes
>
> But.if i declare app = Flask(__name__) in main.py
> how will routes.py work..
> I mean on top of every view function is a decorator function
> Do you mean something like
>
>
> main.py
> def main_program(file):
> home(file)
> if __name__ == "__main__":
> * app = Flask(__main__)*
> main_program(foo.txt)
> *app.run(debug=True)*
>
> routes.py
>
> app = Flask(__name__)
> @app.route('/<file>')
> def home(file):
> print file
> contents = open(file).read()
> contents = contents.decode('utf-8')
> return render_template('raw_text.html', contents=contents,file=file)
> ??
>
>
> Kindly explain me with an example.
> Thanks
>
>
> On Sat, Feb 9, 2013 at 4:08 PM, Andy D'Arcy Jewell
> <andy@wild-flower.co.uk <mailto:andy@wild-flower.co.uk>> wrote:
>
> On 09/02/13 23:43, Chengi Liu wrote:
> > Hi.
> > Sorry for not being able to convey my problem..Ahh..
> > main.py
> > def main_program(file):
> > home(file)
> > if __name__ == "__main__":
> > main_program(foo.txt)
> >
> > routes.py
> >
> > app = Flask(__name__)
> > @app.route('/<file>')
> > def home(file):
> > print file
> > contents = open(file).read()
> > contents = contents.decode('utf-8')
> > return render_template('raw_text.html',
> contents=contents,file=file)
> >
> > app.run()
> > ---------------
> >
> >
> >
> > Now. i do python main.py
> >
> > It throws an exception exception occured 'NoneType' object has no
> > attribute 'app' <type 'exceptions.Exception'>
> >
> > And then if i go to localhost:5000/foo
> >
> > All I want is somethng very simple.
> >
> > I start the flask server..
> > i run the main.py..
> > and display the contents on the browser.
> >
> > Please help me.
> >
> Have you tried putting main_program() and home() in the same python
> file? __name__ will refer to different objects in each of the
> modules -
> I'm assuming you left out the "import routes" part in main.py?
>
> __name__ refers to the name of the module in which it is defined -
> which
> would be "routes" for the "app = Flask(__name__)" statement, but you
> really need it to refer to "main", which might be where your
> problem is
> coming from.
>
> hth
> -Andy
>
>
For simple applications, just keep all the code in the same file -
especially while you are learning. When you reach the point you need to
modularise your code, read the section in the Flask docs on Blueprints.
In your example above, you seem to be confusing a top-down program with
a web-program (which is by nature "event driven"). Web programs
generally need to be structured that way, because of the way the browser
and the web-server interact.
-Andy