如何在Flask中调用Zerodha API

问题描述 投票:0回答:1

我想调用 Zerodha API 来搜索仪器并添加到愿望清单,但是任何人都可以指导我如何让它在 Flask 应用程序中调用仪器搜索,因为 GUI 是 HTML 格式的,以及如何将其连接到 Flask 应用程序首先我做了Tkinter 中的 GUI,但现在我想使用 Flask 在 Web 应用程序中转换它。请指导。

提前致谢。

我正在分享我仅使用 Zerodha API 用 Python 制作的 Tkinter GUI 的代码。

def create_search_bar(self):
        search_frame = ttk.Frame(self.root)
        search_frame.pack(side=tk.LEFT, padx=0, pady=0, anchor='n')

        self.search_entry = ttk.Entry(search_frame, font=('Consolas', 12))
        self.search_entry.pack(side=tk.TOP, padx=0, pady=0)

        self.suggestion_listbox = tk.Listbox(search_frame, font=('Consolas', 12), height=15, width=35)
        self.suggestion_listbox.pack(side=tk.TOP, padx=0, pady=0)
        self.suggestion_listbox.bind('<ButtonRelease-1>', self.add_selected_instrument)
        self.search_entry.bind('<KeyRelease>', self.update_suggestions)

    def update_suggestions(self, event):
        query = self.search_entry.get().strip().upper()
        suggestions = [instrument for instrument in self.all_instruments if query in instrument.upper()]

        if query:
            self.suggestion_listbox.pack(side=tk.TOP, padx=0, pady=0)
        else:
            self.suggestion_listbox.pack_forget()

        self.suggestion_listbox.delete(0, tk.END)
        for suggestion in suggestions:
            self.suggestion_listbox.insert(tk.END, suggestion)

    def add_selected_instrument(self, event):
        selected_instrument = self.suggestion_listbox.get(tk.ACTIVE)
        if selected_instrument:
            wishlist_index = self.prompt_for_wishlist_tab()
            if wishlist_index is not None:
                if len(self.subscribed_instruments[wishlist_index]) < 30:
                    self.add_instrument(selected_instrument, wishlist_index)
                else:
                    messagebox.showinfo("Limit Reached", "You have reached the maximum limit of 30 instruments for this wishlist tab.")


    def prompt_for_wishlist_tab(self):
        response = simpledialog.askinteger("Select Wishlist Tab", "Enter the Wishlist Tab number (1 to 10):",
                                        parent=self.root, minvalue=1, maxvalue=10)
        if response is not None:
            return response - 1
        else:
            return None
    
    def update_note_label(self):
        for i in range(10):
            if len(self.subscribed_instruments[i]) >= 30:
                self.note_label.config(text="Note: Maximum 30 instruments reached for some wishlist tabs. You can't add more.")
                break
        else:
            self.note_label.config(text="Note: Maximum 30 instruments can be added per wishlist tab.")


    def add_instrument(self, instrument, wishlist_index):
        if instrument not in self.subscribed_instruments[wishlist_index]:
            self.subscribed_instruments[wishlist_index].append(instrument)
            self.save_subscribed_instruments(wishlist_index)
            self.show_result(f"{instrument} added to Wishlist {wishlist_index + 1}.")
            self.update_treeview(wishlist_index)
            self.update_note_label()

    def create_wishlist_tabs(self):
        left_frame = ttk.Frame(self.root)
        left_frame.pack(side=tk.LEFT, padx=10, pady=0, fill=tk.BOTH)

        self.notebook = ttk.Notebook(left_frame)
        self.notebook.pack(side=tk.TOP, padx=0, pady=0, fill=tk.BOTH, expand=True)  # Expanded to occupy all available space

        for i in range(10):
            wishlist_tab = ttk.Frame(self.notebook)
            self.notebook.add(wishlist_tab, text=f"Wishlist {i + 1}")

            columns = ("Stock", "Price")
            stock_tree = ttk.Treeview(wishlist_tab, columns=columns, show="headings", height=25)
            stock_tree.heading("Stock", text="Stock")
            stock_tree.heading("Price", text="Price")
            stock_tree.pack(side=tk.TOP, padx=0, pady=0, fill=tk.BOTH, expand=True)  # Expanded to occupy all available space
            stock_tree.bind('<Double-1>', self.show_wishlist_options)
            self.stock_trees.append(stock_tree)

        self.note_label = ttk.Label(left_frame, text="Note: Maximum 30 instruments can be added per wishlist tab.", font=('Consolas', 10), foreground='gray')
        self.note_label.pack(side=tk.BOTTOM, pady=0, fill=tk.X)  # Fill the available horizontal space


    def update_stock_prices_thread(self):
        self.load_subscribed_instruments()
        while True:
            self.update_stock_prices()
            time.sleep(1)

    def show_wishlist_options(self, event):
        selected_index = self.notebook.index(self.notebook.select())
        selected_stock = self.get_selected_stock(selected_index)
        if selected_stock:
            price = self.stock_prices.get(selected_stock, 0)
            self.show_result(f"Selected Stock: {selected_stock}, Price: {price}")
            # Open details window
            self.show_instrument_details(selected_stock, price)

我该如何在 Flask 中调用它,请指导。

python flask flask-restful pyalgotrade
1个回答
0
投票

Zerodha 在这里给出了如何在 Flask 中使用 Zerodha API 的示例... https://github.com/zerodha/pykiteconnect/blob/master/examples/flask_app.py 看一下..可能会有所帮助...

© www.soinside.com 2019 - 2024. All rights reserved.