redirecting stdout does not work unless you have a global locker that locks web2py while you do the redirect. Otherwise if two threads (two requests) do it at the same time you get wrong output and you may completely loose stdout.
On Feb 25, 3:53 am, Tiago Almeida <tiago.b.alme...@gmail.com> wrote: > Sorry for the misunderstanding. When I said it was horrible I meant that > redirecting stdout is not a very elegant solution, but works. > Don't know how you could do what you need, showing the progress of the > script. > > Regards, > Tiago > -- > > On Wed, Feb 24, 2010 at 10:35 PM, AsmanCom <d.as...@web.de> wrote: > > Yo Tiango, > > many thanks for your effort. > > > But if you say: > > > this is horrible! > > > I´ll forget about that. > > > > The least you should do is transform your scripts > > > so that instead of printing, they return the strings > > that's what i meant with: "(may i could change "print" to "return")" > > > But i don´t know how to do this? > > > May we could return to my example: > > > def chunk_report(bytes_so_far, chunk_size, total_size): > > percent = float(bytes_so_far) / total_size > > percent = round(percent*100) > > return percent > > > which is called by the def chunk_read: > > while 1: > > chunk = response.read(chunk_size) > > bytes_so_far += len(chunk) > > > if not chunk: > > break > > > if report_hook: > > report_hook(bytes_so_far, chunk_size, total_size) > > > How to display the output of chunk_report properly? (or how to update > > the ProgressBar with that output?) > > > Best regards, > > > Dieter Asman > > > - AsmanCom - > > Germany > > > On 24 Feb., 23:09, Tiago Almeida <tiago.b.alme...@gmail.com> wrote: > > > This is nasty as hell but you can redirect stdout to a string. Execute > > your > > > untouched scripts, all they print goes to the string and then, in the > > web2py > > > controller return that string instead of a dict. > > > > Example: > > > -- file print1_script.py: -- > > > def print1(): > > > print "1" > > > > #call print1 > > > print1() > > > > -- file whatever.py: -- > > > import sys > > > from cStringIO import StringIO > > > > #redirect stdout to string > > > sys.stdout = mystdout = StringIO() > > > > #execute script print1_script.py > > > execfile('print1_script.py') > > > > #reset the stdout > > > sys.stdout = sys.__stdout__ > > > > #the output is available *as a string* in mystdout.getvalue() > > > print mystdout.getvalue() > > > > --- end -- > > > > Again, this is horrible! The least you should do is transform your > > scripts > > > so that instead of printing, they return the strings. Then in web2py > > > controller you just return whatever your function returns. > > > > Regards, > > > Tiago > > > ---- > > > > On Wed, Feb 24, 2010 at 5:33 PM, AsmanCom <d.as...@web.de> wrote: > > > > Hi Tiango, > > > > > no i need an equivalent for print, like: > > > > > def print_1(): > > > > print "1" > > > > print "2" > > > > = > > > > 1 > > > > 2 > > > > > def print_2(): > > > > while True: > > > > print "1" > > > > = > > > > 1 > > > > 1 > > > > 1 > > > > 1 > > > > ... > > > > > I need to execute scripts like in terminal, but output rendered in > > > > html. (should be very simple?) > > > > > Best regards, > > > > > Dieter Asman > > > > > - AsmanCom - > > > > Germany > > > > > On 24 Feb., 18:17, Tiago Almeida <tiago.b.alme...@gmail.com> wrote: > > > > > > for quick development-shots, i need to execute my untouched(may i > > > > > > could change "print" to "return") Python scripts(..with loops) in > > the > > > > > > Controller. > > > > > > The output should be renderd direct via http, without long-winded > > > > > > setting up the view for each function. > > > > > > Is that possible? > > > > > > If I understand you want to return a string directly without being > > > > processed > > > > > by the view. > > > > > On a controller function, if you return a string (not a dict) it will > > be > > > > > rendered as is. > > > > > > Tiago > > > > > -- > > > > > > On Wed, Feb 24, 2010 at 4:48 PM, AsmanCom <d.as...@web.de> wrote: > > > > > > Hi, > > > > > > > for quick development-shots, i need to execute my untouched(may i > > > > > > could change "print" to "return") Python scripts(..with loops) in > > the > > > > > > Controller. > > > > > > The output should be renderd direct via http, without long-winded > > > > > > setting up the view for each function. > > > > > > Is that possible? > > > > > > > To my next concern.... > > > > > > > On my actual Project i have simple form field with submit button on > > my > > > > > > controller: > > > > > > form = SQLFORM.factory(Field('update_url', default='http://. > > > > domain.org/ > > > > > > text_file.txt <http://domain.org/%0Atext_file.txt>', > > > > > > requires=IS_NOT_EMPTY())) > > > > > > > On Submit the text file should be downloaded to Server indicated by > > an > > > > > > Progress Bar on the Page. > > > > > > Next the file should be inserted to the DB line by line(for loop > > with > > > > > > regex) and again indicated by the same Progress Bar ..... > > > > > > > My Code so far(... relies on clienttools example specifically on > > > > > > jqueryUI-progressbar and an urllib2 +report_hook snippet i´ve found > > > > > > somewhere): > > > _______________________________________________________________________ > > > > > > def chunk_report(bytes_so_far, chunk_size, total_size): > > > > > > percent = float(bytes_so_far) / total_size > > > > > > percent = round(percent*100) > > > > > > return percent > > > > > > > if bytes_so_far >= total_size: > > > > > > return ('100.0') > > > > > > > def chunk_read(response, chunk_size=8192, report_hook=None): > > > > > > total_size = response.info > > ().getheader('Content-Length').strip() > > > > > > total_size = int(total_size) > > > > > > bytes_so_far = 0 > > > > > > > while 1: > > > > > > chunk = response.read(chunk_size) > > > > > > bytes_so_far += len(chunk) > > > > > > > if not chunk: > > > > > > break > > > > > > > if report_hook: > > > > > > report_hook(bytes_so_far, chunk_size, total_size) > > > > > > > percent = float(bytes_so_far) / total_size > > > > > > percent = round(percent*100) > > > > > > return percent #bytes_so_far > > > > > > > def url_progress(): > > > > > > wrapper = DIV(progress,_style="width:400px;") > > > > > > jqueryui_progress() > > > > > > form = SQLFORM.factory(Field('update_url', > > > > > > default='http://.domain.org/text_file.txt', > > requires=IS_NOT_EMPTY())) > > > > > > if form.accepts(request.vars, session): > > > > > > response.flash = "Got it!" > > > > > > try: > > > > > > import urllib2 > > > > > > openurl = urllib2.urlopen(request.vars.update_url) > > > > > > chunk_read(openurl, report_hook=chunk_report) > > > > > > except Exception, e: > > > > > > session.flash = DIV(T('Unable to download from url > > > > > > because:'),PRE(str(e))) > > > > > > redirect(URL(r=request)) > > > > > > event.listen('submit',"form","return confirm('Are you sure?');") > > # > > > > > > adds a confirmation to submit > > > > > > page.ready(jq("update_oui").focus()()) > > > > > > # return dict(form=form) > > > > > > return dict(wrapper=wrapper, form=form) > > > > > > > # jQuery progress bar > > > > > > progress = DIV(_id="progress") > > > > > > def jqueryui_progress(): > > > > > > wrapper = DIV(progress,_style="width:400px;") > > > > > > page.include(" > >http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/ > > > > > > jquery-ui.min.js< > > > >http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/%0Ajquery-ui.min.js > > >", > > > > > > download=True) > > > > > > page.include(" > >http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/ > > > > > > themes/ui-darkness/jquery-ui.css< > > > >http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/%0Athemes/ui-dark.. > > > > .>", > > > > > > download=True) > > > > > > callback = js.call_function(get_progress) > > > > > > page.ready(jq(progress).progressbar( dict(value='0') )() ) > > > > > > page.ready(js.timer(callback,2000)) > > > > > > return dict(wrapper=wrapper) > > > > > > > def get_progress(): > > > > > > return jq(progress).progressbar('option', 'value', > > > > > > request.now.second)()# > > > > > > ________________________________________________________________ > > > > > > > But now i stuck.... can anyone put me on the right way? > > > > > > When the Page is loaded, both elemts show up correctly. > > > > > > When i do the submit: > > > > > > - The Progressbar freezes(Callback dont work while downloading the > > > > > > file...) > > > > > > - The Download works and shows 100% when completed(but the while 1 > > > > > > loop, which calls the report_hook dont work at all! and i need it > > to > > > > > > update the Progress bar... I think?) > > > > > > > + I need to change the Progressbar callback source when updating DB > > > > > > after downloading the file... > > > > > > > Anyone any suggestions? > > > > > > > Best regards, > > > > > > > Dieter Asman > > > > > > > - AsmanCom - > > > > > > Germany > > > > > > > -- > > > > > > You received this message because you are subscribed to the Google > > > > Groups > > > > > > "web2py-users" group. > > > > > > To post to this group, send email to web...@googlegroups.com. > > > > > > To unsubscribe from this group, send email to > > > > > > web2py+unsubscr...@googlegroups.com<web2py%2bunsubscr...@googlegroups.com> > > <web2py%2bunsubscr...@googlegroups.com<web2py%252bunsubscr...@googlegroups.com> > > > > > <web2py%2bunsubscr...@googlegroups.com<web2py%252bunsubscr...@googlegroups.com> > > <web2py%252bunsubscr...@googlegroups.com<web2py%25252bunsubscr...@googlegroups.com> > > > > > > > . > > > > > > For more options, visit this group at > > > > > >http://groups.google.com/group/web2py?hl=en. > > > > > -- > > > > You received this message because you are subscribed to the Google > > Groups > > > > "web2py-users" group. > > > > To post to this group, send email to web...@googlegroups.com. > > > > To unsubscribe from this group, send email to > > > > web2py+unsubscr...@googlegroups.com<web2py%2bunsubscr...@googlegroups.com> > > <web2py%2bunsubscr...@googlegroups.com<web2py%252bunsubscr...@googlegroups.com> > > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/web2py?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "web2py-users" group. > > To post to this group, send email to > > ... > > read more » -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web...@googlegroups.com. To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.