librelist archives

« back to archive

Newbie.. interacting with another python program

Newbie.. interacting with another python program

From:
Chengi Liu
Date:
2013-02-09 @ 00:09
Hi,
  I am new to flask world. I was wondering if someone can help me with this.


1) I have a program in python which by some logic fetches a file..
2) And then I want to display it to the browser.
3) If ok.. then press [close] button on html and stop the server.
    else.. fetch another file.

Now, here is how my view code looks like:

Re: Newbie.. interacting with another python program

From:
Chengi Liu
Date:
2013-02-09 @ 00:12
Sorry pressed sent by mistake
So far this is what i have got.
@app.route('/')
def home():
    file = "foo.txt"
    contents = open(file).read()
    contents = contents.decode('utf-8')
    return render_template('raw_text.html',  contents=contents)

Now.. rather than just hard coding.. I want another program to send the
file name..
How do i pass file as an argument to this function?

Also, to close .. I put javascript on my page..

<script type="text/javascript">
    function close_window(){
        if (confirm("Close Window?")){
            close();
        }
    }
</script>
<div class ="post_page" >
    Your request has been submitted.<br/>
    Please exit to shell
    <a href="javascript:close_window();">close</a>
</div>

But javascript being client side.. has no effect on achieving the goal..
Please help me out.. :(
Many thanks.


On Fri, Feb 8, 2013 at 4:09 PM, Chengi Liu <chengi.liu.86@gmail.com> wrote:

> Hi,
>   I am new to flask world. I was wondering if someone can help me with
> this.
>
>
> 1) I have a program in python which by some logic fetches a file..
> 2) And then I want to display it to the browser.
> 3) If ok.. then press [close] button on html and stop the server.
>     else.. fetch another file.
>
> Now, here is how my view code looks like:
>
>

Re: [flask] Re: Newbie.. interacting with another python program

From:
Markus Unterwaditzer
Date:
2013-02-09 @ 09:40
Now, do you want to stop the server or just close the window of the 
browser?

Regarding passing the filename, look at 
http://flask.pocoo.org/docs/quickstart/#variable-rules

@app.route('/greet/<name>')
def greet(name):
     return "Hello, " + name + "!"

On 2013-02-09 01:12, Chengi Liu wrote:
> Sorry pressed sent by mistake
> So far this is what i have got.
> @app.route('/')
> def home():
>     file = "foo.txt"
>     contents = open(file).read()
>     contents = contents.decode('utf-8')
>     return render_template('raw_text.html',  contents=contents)
>
> Now.. rather than just hard coding.. I want another program to send
> the file name..
> How do i pass file as an argument to this function?
>
> Also, to close .. I put javascript on my page..
>
> <script type="text/javascript">
>     function close_window(){
>         if (confirm("Close Window?")){
>             close();
>         }
>     }
> </script>
> <div class ="post_page" >
>     Your request has been submitted.<br/>
>     Please exit to shell
>     <a href="javascript:close_window();">close</a>
> </div>
>
>  But javascript being client side.. has no effect on achieving the 
> goal..
> Please help me out.. :(
> Many thanks.
>
> On Fri, Feb 8, 2013 at 4:09 PM, Chengi Liu <chengi.liu.86@gmail.com> 
> wrote:
>
>> Hi,
>>   I am new to flask world. I was wondering if someone can help me 
>> with this.
>>
>> 1) I have a program in python which by some logic fetches a file..
>> 2) And then I want to display it to the browser.
>> 3) If ok.. then press [close] button on html and stop the server.
>>     else.. fetch another file.
>>
>> Now, here is how my view code looks like:

Re: [flask] Re: Newbie.. interacting with another python program

From:
Chengi Liu
Date:
2013-02-09 @ 20:32
Hi.
Thanks for reply.
 I want to do both actually..
Closing the window and ultimately cleanly shutting down the server as well.
And second solution is awesome :)
Thanks


On Sat, Feb 9, 2013 at 1:40 AM, Markus Unterwaditzer <
markus@unterwaditzer.net> wrote:

> Now, do you want to stop the server or just close the window of the
> browser?
>
> Regarding passing the filename, look at
> http://flask.pocoo.org/docs/quickstart/#variable-rules
>
> @app.route('/greet/<name>')
> def greet(name):
>      return "Hello, " + name + "!"
>
> On 2013-02-09 01:12, Chengi Liu wrote:
> > Sorry pressed sent by mistake
> > So far this is what i have got.
> > @app.route('/')
> > def home():
> >     file = "foo.txt"
> >     contents = open(file).read()
> >     contents = contents.decode('utf-8')
> >     return render_template('raw_text.html',  contents=contents)
> >
> > Now.. rather than just hard coding.. I want another program to send
> > the file name..
> > How do i pass file as an argument to this function?
> >
> > Also, to close .. I put javascript on my page..
> >
> > <script type="text/javascript">
> >     function close_window(){
> >         if (confirm("Close Window?")){
> >             close();
> >         }
> >     }
> > </script>
> > <div class ="post_page" >
> >     Your request has been submitted.<br/>
> >     Please exit to shell
> >     <a href="javascript:close_window();">close</a>
> > </div>
> >
> >  But javascript being client side.. has no effect on achieving the
> > goal..
> > Please help me out.. :(
> > Many thanks.
> >
> > On Fri, Feb 8, 2013 at 4:09 PM, Chengi Liu <chengi.liu.86@gmail.com>
> > wrote:
> >
> >> Hi,
> >>   I am new to flask world. I was wondering if someone can help me
> >> with this.
> >>
> >> 1) I have a program in python which by some logic fetches a file..
> >> 2) And then I want to display it to the browser.
> >> 3) If ok.. then press [close] button on html and stop the server.
> >>     else.. fetch another file.
> >>
> >> Now, here is how my view code looks like:
>

Re: [flask] Re: Newbie.. interacting with another python program

From:
Andy D'Arcy Jewell
Date:
2013-02-09 @ 21:00
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:
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