Refresh data in python script while running in Terminal - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Refresh data in python script while running in Terminal (/thread-32293.html) |
Refresh data in python script while running in Terminal - frankenchrist - Feb-02-2021 Hi, I have gotten a script to pull in new helpdesk tickets from Zendesk and display then in a terminal window (Ubuntu). It pulls the information without any issue but what I need to it do is refresh the data in the same session so for every minute, it will display any new tickets that have been requested by staff. This is the code that I have that works: from urllib.parse import urlencode import requests import os os.system('clear') credentials = 'zendesk_email' + '/token', 'zendesk_token' session = requests.Session() session.auth = credentials params = { 'query': 'type:ticket status:new', 'sort_by': 'created_at', 'sort_order': 'desc' } url = 'https://subdomain.zendesk.com/api/v2/search.json?' + urlencode(params) response = session.get(url) if response.status_code != 200: print('Status:', response.status_code, 'Problem with the request. Exiting.') exit() # Print the subject of each ticket in the results data = response.json() for result in data['results']: print(result['subject'])Now, I have played around with a couple of ways to refresh the content in the script in real time. I entered these at the bottom of the above coding in the same script. One was to reload the script as a module: import open_tickets import importlib import time importlib.reload(open_tickets) time.sleep(60)Now that sort of worked, as it would reload the script, but it wouldn't update the content when a new ticket is requested by a user. Another method I hacked together was while True statement, something along the lines of: while True: print(result['subject']) time.sleep(60) os.system('clear')I need the script to reload the data automatically and in real time and display any new helpdesk ticket everytime one has been create. I hope this makes sense and helps. Cheers RE: Refresh python script while running in Terminal - Larz60+ - Feb-02-2021 You need to use an event timer. It will run independent of other precesses, and create an event (interrupt) when the terminal time is reached. here's a quick example: import threading, time def do_work(): print("OK ... I'm doing my work here") def testit(seconds): timer = threading.Event() while True: print(f"delay {seconds} seconds") timer.wait(seconds) do_work() if __name__ == '__main__': testit(2)sample output:
RE: Refresh python script while running in Terminal - frankenchrist - Feb-02-2021 Hi Larz60+, Thanks for the reply, appreciate the help. I have used the code you suggested and have put it at the bottom of my script like so: import threading, time def do_work(): os.system('clear') data = response.json() for result in data['results']: print(result['subject']) def testit(seconds): timer = threading.Event() while True: print(f"delay {seconds} seconds") timer.wait(5) do_work() if __name__ == '__main__': testit(5)I added the clear code so that it doesn't keep listing the tickets like in your sample output. I started running the script with your code and it did work and pulled the tickets from zendesk and displayed them. However, when I submitted a new test ticket to zendesk, the python script didn't pick up the new ticket and display it. It just kept displaying the list of tickets that were there before I submitted the test ticket. This is what the output looks like before and after submitting a new ticket with the new code: Cheers
RE: Refresh data in python script while running in Terminal - frankenchrist - Feb-03-2021 Never mind, I had a play around and managed to figure it out. This is what the final code looks like: from urllib.parse import urlencode import requests import os import threading, time def do_work(): os.system('clear') credentials = 'zendesk_email' + '/token', 'zendesk_api_token' session = requests.Session() session.auth = credentials params = { 'query': 'type:ticket status:new', 'sort_by': 'created_at', 'sort_order': 'desc' } url = 'https://subdomain.zendesk.com/api/v2/search.json?' + urlencode(params) response = session.get(url) if response.status_code != 200: print('Status:', response.status_code, 'Problem with the request. Exiting.') exit() # Print the subject of each ticket in the results data = response.json() for result in data['results']: print(result['subject']) def testit(seconds): timer = threading.Event() while True: #print(f"delay {seconds} seconds") timer.wait(30) do_work() if __name__ == '__main__': testit(30)Now it refreshes every 30 seconds and pulls in the new tickets when they arrive. Cheers again for the help. RE: Refresh data in python script while running in Terminal - Larz60+ - Feb-03-2021 Glad to hear it. Also, the function names that I used were from writing code quickly. Probably not the best choice, so please feel free to change. |