Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print: Tips and tricks
#11
(Apr-12-2019, 09:52 AM)perfringo Wrote:
(Apr-12-2019, 08:30 AM)microphone_head Wrote: I was toying with the idea of being able to write some chars to the Python shell before deleting it to make a progress bar. Can you give an example Angel ?
Something along those lines (no f-strings, should work right away)?
 import sys, time def status_bar(activity, progres): length = 25 block = int(round(length*progres)) msg = "\r{0}: [{1}] {2}%".format(activity, "#"*block + "-"*(length-block), round(progres*100, 2)) if progres >= 1: msg += " Ready\r\n" sys.stdout.write(msg) sys.stdout.flush() # usage for i in range(100): time.sleep(0.1) status_bar('Activity', i/100.0) status_bar('Activity', 1) 

I like this one. Its more obvious to me what its trying to do but the escape code doesn't work on my machine (Python shell). It just fills up the screen with the different states of the progress bar.

Here's a little of what I get to see:

Output:
Activity: [-------------------------] 0.0% Activity: [-------------------------] 1.0% Activity: [-------------------------] 2.0% Activity: [#------------------------] 3.0% Activity: [#------------------------] 4.0% Activity: [#------------------------] 5.0% Activity: [##-----------------------] 6.0% Activity: [##-----------------------] 7.0% Activity: [##-----------------------] 8.0%
Shocked

Now wait a minute Huh . The out I am looking at in my shell window is nothing like whats shown here Cry .

PS. I'd do a screen dump but I've forgotten where this system dumps the file Wall .
Reply
#12
carriage return \r and the backspace \b don't work within IDLE(don't use it all) because it uses a text control that doesn't render return/backspace properly.
Run it from command line eg python3 pro_bar.py,or use better REPL like ipython or ptpython.
Run it interactive -i from command should also work as then it will not us IDLE.
λ python -i pro_bar.py
Activity: [#########################] 100% Ready

# Using -i mean that can continue explorer code 
>>> i
99
>>> status_bar
<function status_bar at 0x03410858>

Eg with ptpython or ipython i can just copy all code straight into REPL and it work.
With IDLE can not copy in code at all,has to use Enter after each line.
λ ptpython
>>> import sys, time
...
... def status_bar(activity, progres):
...     length = 25
...     block = int(round(length*progres))
...     msg = "\r{0}: [{1}] {2}%".format(activity, "#"*block + "-"*(length-block), round(progres*100, 2))
...     if progres >= 1:
...         msg += " Ready\r\n"
...     sys.stdout.write(msg)
...     sys.stdout.flush()
...
...
...
... # usage
... for i in range(100):
...     time.sleep(0.1)
...     status_bar('Activity', i/100.0)
... status_bar('Activity', 1)
Activity: [#########################] 100% Ready
Reply
#13
(Apr-12-2019, 10:45 AM)snippsat Wrote: carriage return \r and the backspace \b don't work within IDLE(don't use it all) because it uses a text control that doesn't render return/backspace properly. Run it from command line eg python3 pro_bar.py,or use better REPL like ipython or ptpython. Run it interactive -i from command should also work as then it will not us IDLE.
λ python -i pro_bar.py Activity: [#########################] 100% Ready # Using -i mean that can continue explorer code >>> i 99 >>> status_bar <function status_bar at 0x03410858>
Eg with ptpython or ipython i can just copy all code straight into REPL and it work. With IDLE can not copy in code at all,has to use Enter after each line.
λ ptpython >>> import sys, time ... ... def status_bar(activity, progres): ... length = 25 ... block = int(round(length*progres)) ... msg = "\r{0}: [{1}] {2}%".format(activity, "#"*block + "-"*(length-block), round(progres*100, 2)) ... if progres >= 1: ... msg += " Ready\r\n" ... sys.stdout.write(msg) ... sys.stdout.flush() ... ... ... ... # usage ... for i in range(100): ... time.sleep(0.1) ... status_bar('Activity', i/100.0) ... status_bar('Activity', 1) Activity: [#########################] 100% Ready

I should have realised that it wouldn't work in the shell when I saw the sys statements, the penny has finally dropped Blush.

I tried it in Geany, and it nearly worked Dance . It keeps everything on one line like expected but it its a all or nothing deal in that instance, because it looks like it waits for all processing to completed before refreshing the output terminal showing the progress bar at 100%. I got the same effect in the Spider editor, and from the command line in a Terminal window.

I think the problem might be because I am not running full blown Linux, because I'm on a RaspberryPi, I don't think everything gets carried across when they compile the apps for my system Think .

Anyway, thank you all for help Smile, looks like I still got a lot to learn, and maybe a laptop to buy Rolleyes.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner/Intermediate Tips Ex_database 1 2,040 Sep-27-2019, 04:24 PM
Last Post: Larz60+
  Quick tips and tricks for python juliopa 2 34,085 Jul-03-2019, 09:41 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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