I have an async main() that starts the gui build using tkinter and I would like it to fetch_data from an api while gui is building.
As of now I have an issue as the gui finished but it does not start the fetch_data.
from concurrent.futures import ThreadPoolExecutor
import tkinter as tk
from fetch_data import fetch_data
from Filters import DataFilter
import json
import asyncio
from tkinter import filedialog
from FileProcessor import FileProcessor
from PIL import Image, ImageTk
async def main():
root = tk.Tk()
root.title("Assets report")
submit_button = tk.Button(root, text="Generate the Report", state=tk.DISABLED, command=save_selected_dates, bg = "white")
#gui setting here......
def check_mana_status(root, data_future, submit_button):
if data_future.done():
submit_button["state"] = tk.NORMAL
else:
root.after(100, lambda: check_mana_status(root, data_future, submit_button))
loop = asyncio.get_event_loop()
with ThreadPoolExecutor() as executor:
data_future = loop.run_in_executor(executor, fetch_data)
check_mana_status(root, data_future, submit_button)
root.mainloop()
asyncio.run(main())
The fetch_data file looks like this:
from FileProcessor import FileProcessor
from Filters import DataFilter
import requests
import asyncio
import aiohttp
async def fetch_data():
url_exp = "https://example.url"
try:
print('start calls')
results = await asyncio.gather(
get_data(url_exp, headers),
get_data(url_exp, headers),
get_data(url_exp, headers),
get_data(url_exp, headers)
)
response_data_co, response_data_ci, response_data_cr, response_data_hw = results
print('ready')
except requests.exceptions.RequestException as e:
print("Error:", e)
return [response_data_co + response_data_cr + response_data_ci, response_data_hw]