in this code: import json import requests import time url =…
- User
in this code: import json import requests import time
url = 'https://dfbe-34-143-138-174.ngrok.io/detect' text = "This is great"
Make request
response = requests.get(url, params={'text': text})
def animate_dots():
while response.status_code == 202:
for i in range(3):
print(".", end="")
time.sleep(1)
print("\r", end="")Start animating after request
print("Analyzing...", end="")
animate_dots()result = response.json()
Get label and score
label = result[0]['label']
score = result[0]['score']
score_perc = score * 100Print result
print(f"Language: {label}") print(f"Confidence: {score_perc:.1f}%") I still have to wate until the request done then I see the Analyzing... appear. Here is the result: Analyzing...Language: English Confidence: 99.5%
I want the Analyzing ... and the dot animation run paralell while wait for response. Then it will disppear when we have the result. LIke this:
Analyzing... (and the dot animation)
When we have result then we have this: Language: English Confidence: 99.5%
do it
- Assistant
To achieve your desired output, you can use the threading module to run the dot animation in a separate thread while waiting for the response. Here's an updated version of your code to achieve this:
import json import requests import time import threading url = 'https://e7ff-34-143-138-174.ngrok.io/detect' text = "This is great" # Make request response = requests.get(url, params={'text': text}) end_animation = False def animate_dots(): while not end_animation: for i in range(3): print(".", end="") time.sleep(1) print("\r", end="") # Start animating after request animation_thread = threading.Thread(target=animate_dots, daemon=True) print("Analyzing...", end="") animation_thread.start() # Wait for request to finish response.raise_for_status() # Raise an error if the request fails # Stop the animation end_animation = True animation_thread.join() # Clear the current line and move to a new line print("\r", end="") print(" " * 20) print("\r", end="") result = response.json() # Get label and score label = result[0]['label'] score = result[0]['score'] score_perc = score * 100 # Print result print(f"Language: {label}") print(f"Confidence: {score_perc:.1f}%")