1+ """
2+ APIFrame class
3+ """
4+ from increment import Increment
5+ import tkinter as tk
6+ from tkinter import ttk
7+ import webbrowser
8+ import os
9+ import openai
10+ from openaiclient .view .frame import BaseFrame
11+ from tests .unit .fixture import api
12+
13+
14+ class APIFrame (BaseFrame ):
15+ """
16+ A class for creating the frame for the API window when no
17+ API key is discovered on startup
18+ """
19+
20+ def __init__ (self , main , controller ):
21+ super ().__init__ (main , controller )
22+
23+ self ._api_key = tk .StringVar ()
24+
25+ def create (self ):
26+ col = Increment ()
27+ row = Increment ()
28+
29+ tempFrame1 = tk .Frame (self .master )
30+
31+ tempCol = Increment ()
32+ tempRow = Increment ()
33+
34+ self .addHorizSeparator (tempCol , tempRow , tempFrame1 )
35+
36+ tk .Label (
37+ tempFrame1 ,
38+ text = instruction .strip (),
39+ justify = tk .LEFT ,
40+ font = ("" , 10 , "" ),
41+ ).grid (
42+ column = tempCol ,
43+ row = tempRow ,
44+ padx = 10 ,
45+ columnspan = 2
46+ )
47+
48+ ~ tempCol
49+ + tempRow
50+
51+ self .addHorizSeparator (tempCol , tempRow , tempFrame1 )
52+
53+ tempFrame1 .grid (
54+ column = col ,
55+ row = row ,
56+ columnspan = 2
57+ )
58+
59+ ~ tempCol
60+ ~ tempRow
61+
62+ ~ col
63+ + row
64+
65+ tempFrame2 = tk .Frame (self .master )
66+
67+ tempCol = Increment ()
68+ tempRow = Increment ()
69+
70+ tk .Button (
71+ tempFrame2 ,
72+ text = "Get API Key" ,
73+ font = ("" , 10 , "" ),
74+ command = self .getAPI
75+ ).grid (
76+ column = tempCol ,
77+ row = tempRow ,
78+ padx = 10 ,
79+ pady = (5 ,5 ),
80+ ipadx = 2
81+ )
82+
83+ + tempCol
84+
85+ tk .Button (
86+ tempFrame2 ,
87+ text = "Use Test API" ,
88+ font = ("" , 10 , "" ),
89+ command = self .setTestAPI
90+ ).grid (
91+ column = tempCol ,
92+ row = tempRow ,
93+ padx = 10 ,
94+ pady = (5 ,5 ),
95+ ipadx = 2
96+ )
97+
98+ tempFrame2 .grid (
99+ column = col ,
100+ row = row ,
101+ columnspan = 2
102+ )
103+
104+ ~ col
105+ + row
106+
107+ tk .Label (
108+ self ,
109+ text = "API Key:" ,
110+ font = ("" , 10 , "" ),
111+ ).grid (
112+ column = col ,
113+ row = row ,
114+ pady = (5 ,10 ),
115+ padx = (10 ,5 ),
116+ )
117+
118+ + col
119+
120+ tk .Entry (
121+ self ,
122+ width = 30 ,
123+ textvariable = self ._api_key
124+ ).grid (
125+ column = col ,
126+ row = row ,
127+ pady = 5 ,
128+ padx = (5 ,10 ),
129+ )
130+
131+ ~ col
132+ + row
133+
134+ tk .Button (
135+ self ,
136+ text = "Save API Key" ,
137+ font = ("" , 10 , "" ),
138+ width = 30 ,
139+ command = self .setAPIKey
140+ ).grid (
141+ column = col ,
142+ row = row ,
143+ pady = (5 ,10 ),
144+ padx = 10 ,
145+ columnspan = 2
146+ )
147+
148+ def addHorizSeparator (self , col , row , frame ):
149+ ~ col
150+
151+ ttk .Separator (
152+ frame ,
153+ orient = "horizontal" ,
154+ ).grid (
155+ column = col ,
156+ row = row ,
157+ columnspan = 2 ,
158+ sticky = tk .W + tk .E ,
159+ padx = 10 ,
160+ pady = 5
161+ )
162+
163+ + row
164+
165+ def getAPI (self ):
166+ api_url = "https://platform.openai.com/account/api-keys"
167+ webbrowser .open (api_url )
168+
169+ def setTestAPI (self ):
170+ self .controller ._api = api
171+ self .master .destroy ()
172+
173+ def setAPIKey (self ):
174+ self .controller ._module = openai
175+ self .controller ._module .api_key = \
176+ os .environ ['OPENAI_API_KEY' ] = self ._api_key .get ()
177+
178+ self .master .destroy ()
179+
180+
181+ instruction = """
182+ If you would like to use OpenAI's API,
183+ please log into your account and generate
184+ the API key there, then paste the API key
185+ into the designated space below.
186+
187+ You can also click the "Generate API Key"
188+ below to open the login link to get the
189+ API key.
190+
191+ If you just want to test out the application,
192+ select the "Use Test API" button below.
193+ """
0 commit comments