User input timer.py with tqdm bar

User input timer.py with tqdm bar

ยท

6 min read

Hi there everyone. ๐Ÿ‘‹ How's your life going?
Hope you are doing well. ๐Ÿ™‚

So, as you read the title, I am gonna share with you about creating a timer app in Python using tqdm library.

Prerequisites: ๐ŸŽ’

  • A bit knowledge of python
  • A bit knowledge of tqdm
  • Willing to code.

Coding: โ˜•๏ธ

pip install tqdm
  • Install tqdm python library using the pip command.
    Ignore if already installed.

from tqdm import tqdm trange
  • Importing tqdm package in our python script.

import time
import datetime
  • Importing time package for the bar to work.
  • Also, importing datetime package in our script to add a timestamp at the end of our timer.

i = input("Set the timer range in seconds: ")
  • We will use input() to get the numerical value of time in seconds.

    NOTE: Here, the numerical value is a string and not an integer.


j = input("Task name : ")
  • Again, we will use input() to get the name of the task, which is a string.

print("Timer started :" + i + "s")
  • Using print() to display a message as soon as the timer will get started.

for i in tqdm(range(int(i)),colour='magenta'):
  • Using for loop to run the progress of the bar
  • Using int() to convert the string type value into the integer type value, that we get from the user through input().

time.sleep(1.0)
  • Using time module to see the progress of the bar.
  • Using sleep() to set the intervals in which it will progress.

So far, our code looks like this:

from tqdm import tqdm, trange
import time
import datetime
i = input("Set the timer range in seconds: ")
j = input("Task name : ")
print("Timer started :" + i + "s")
for i in tqdm(range(int(i)),colour='magenta'):
    time.sleep(1.0)

Moving forward: โžค

But before that,
hit enter to change the line.
Yes. It's very important, as we need to separate the next line from the for loop above.

Okay. Now let's move forward.

print("Time's up...!" + " " + "[Task name: " + j + "]")
  • Using print() to display a message at the end of the timer.
  • This will include the task name entered previously by the user.

current_time = datetime.datetime.now()
  • Using datetime module to get a timestamp when the timer ends.

print("Completed at: ", end="")
print(current_time.strftime("%D %H:%M:%S"))
  • Using print() to display the timestamp.

Final Code: โœŒ๐Ÿผ

And we are done with our final code, which will look like this -

from tqdm import tqdm, trange
import time
import datetime
i = input("Set the timer range in seconds: ")
j = input("Task name : ")
print("Timer started :" + i + "s")
for i in tqdm(range(int(i)),colour='magenta'):
    time.sleep(1.0)

print("Time's up...!" + " " + "[Task name: " + j + "]")

current_time = datetime.datetime.now()
print("Completed at: ", end="")
print(current_time.strftime("%D %H:%M:%S"))

DEMO: ๐Ÿ†

  • Here's the demo to show the output.
  • The file name in the demo is timer.py
  • Command used to run the file is python3 timer.py

Nov-24-2020 09-15-34.gif


Summary: ๐Ÿ’๐Ÿฝโ€โ™€๏ธ

  • Modules used:

    • time module
    • datetime module
    • tqdm module and trange module
  • Python library:

    • tqdm
  • Methods used:

    • print()
    • input()
    • int()

So, this is how I created a timer app in python using tqdm.
Hope you like this app and enjoy this tutorial.
See you soon with my next article of this series - Random-Coding.

Feel free to connect with me on Twitter and Discord .

Keep coding, keep growing.! โ˜•๏ธ


References: ๐Ÿ“š