Jumper-Clown commited on
Commit
7711652
1 Parent(s): fd5da23

start workflows to build pygbag and page

Browse files
Files changed (2) hide show
  1. app.py +120 -9
  2. file_manager.py → github_manager.py +21 -4
app.py CHANGED
@@ -1,20 +1,131 @@
1
  import gradio as gr
2
- import file_manager
 
3
 
4
  def update(text):
5
- file_dir_path = 'test'
6
- file_name = 'file.txt'
7
- response_json = file_manager.push(text, file_dir_path, file_name)
 
 
 
 
8
 
9
- # TODO:
10
- # - call pygbag_build action
11
- # - call pages-build-deployment action
 
 
 
12
 
13
- return response_json
 
 
 
 
 
 
14
 
15
 
16
  with gr.Blocks() as demo:
17
- inp = gr.Textbox(placeholder="Enter Text Here")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  out = gr.Textbox(placeholder="Result")
19
  btn = gr.Button(value="Push")
20
  btn.click(update, inputs=[inp], outputs=[out])
 
1
  import gradio as gr
2
+ import github_manager
3
+
4
 
5
  def update(text):
6
+ file_dir_path = ''
7
+ file_name = 'main.py'
8
+ response = github_manager.push(text, file_dir_path, file_name)
9
+ if response.status_code != 200:
10
+ return response.text
11
+ else:
12
+ print(f"{file_name} pushed")
13
 
14
+ workflow_name = 'pygbag.yml'
15
+ response = github_manager.trigger_workflow(workflow_name)
16
+ if response.status_code != 204:
17
+ return response.text
18
+ else:
19
+ print(f"{workflow_name} workflow started")
20
 
21
+ workflow_name = 'pages-build-deployment'
22
+ response = github_manager.trigger_workflow(workflow_name)
23
+ if response.status_code != 204:
24
+ return response.text
25
+ else:
26
+ print(f"{workflow_name} workflow started")
27
+ return "update successful"
28
 
29
 
30
  with gr.Blocks() as demo:
31
+ inp = gr.Textbox(value="""
32
+ import pygame
33
+ import sys
34
+ import asyncio
35
+
36
+
37
+ # pygame.init() will initialize all
38
+ # imported module
39
+ pygame.init()
40
+
41
+ clock = pygame.time.Clock()
42
+
43
+ # it will display on screen
44
+ screen = pygame.display.set_mode([600, 500])
45
+
46
+ # basic font for user typed
47
+ base_font = pygame.font.Font(None, 32)
48
+
49
+ # create rectangle
50
+ input_rect = pygame.Rect(200, 200, 140, 32)
51
+
52
+ # color_active stores color(lightskyblue3) which
53
+ # gets active when input box is clicked by user
54
+ color_active = pygame.Color('lightskyblue3')
55
+
56
+ # color_passive store color(chartreuse4) which is
57
+ # color of input box.
58
+ color_passive = pygame.Color('chartreuse4')
59
+ color = color_passive
60
+
61
+
62
+
63
+ async def main():
64
+ user_text = ''
65
+ active = False
66
+ while True:
67
+ for event in pygame.event.get():
68
+
69
+ # if user types QUIT then the screen will close
70
+ if event.type == pygame.QUIT:
71
+ pygame.quit()
72
+ sys.exit()
73
+
74
+ if event.type == pygame.MOUSEBUTTONDOWN:
75
+
76
+ if input_rect.collidepoint(event.pos):
77
+ active = True
78
+ else:
79
+ active = False
80
+
81
+ if event.type == pygame.KEYDOWN:
82
+ print(event.key)
83
+ # Check for backspace
84
+ if event.key == pygame.K_BACKSPACE:
85
+
86
+ # get text input from 0 to -1 i.e. end.
87
+ user_text = user_text[:-1]
88
+ elif event.key == 13 or event.key == 1073741912:
89
+ if user_text == "ciao":
90
+ user_text = "Ciao anche a te"
91
+
92
+ # Unicode standard is used for string
93
+ # formation
94
+ else:
95
+ user_text += event.unicode
96
+
97
+ # it will set background color of screen
98
+ screen.fill((255, 255, 255))
99
+
100
+ if active:
101
+ color = color_active
102
+ else:
103
+ color = color_passive
104
+
105
+ # draw rectangle and argument passed which should
106
+ # be on screen
107
+ pygame.draw.rect(screen, color, input_rect)
108
+
109
+ text_surface = base_font.render(user_text, True, (255, 255, 255))
110
+
111
+ # render at position stated in arguments
112
+ screen.blit(text_surface, (input_rect.x+5, input_rect.y+5))
113
+
114
+ # set width of textfield so that text cannot get
115
+ # outside of user's text input
116
+ input_rect.w = max(100, text_surface.get_width()+10)
117
+
118
+ # display.flip() will update only a portion of the
119
+ # screen to updated, not full area
120
+ pygame.display.flip()
121
+
122
+ # clock.tick(60) means that for every second at most
123
+ # 60 frames should be passed.
124
+ clock.tick(60)
125
+ await asyncio.sleep(0)
126
+
127
+ asyncio.run(main())
128
+ """)
129
  out = gr.Textbox(placeholder="Result")
130
  btn = gr.Button(value="Push")
131
  btn.click(update, inputs=[inp], outputs=[out])
file_manager.py → github_manager.py RENAMED
@@ -13,7 +13,10 @@ github_token = os.environ['GIT_TOKEN']
13
 
14
 
15
  def push(new_content, path, file_name):
16
- file_path = f'{path}/{file_name}'
 
 
 
17
 
18
  # Get the current file content
19
  url = f'https://api.github.com/repos/{repository_owner}/{repository_name}/contents/{file_path}?ref={branch_name}'
@@ -27,7 +30,7 @@ def push(new_content, path, file_name):
27
 
28
  # Update the file content
29
  update_data = {
30
- 'message': 'Update file via API',
31
  'content': encoded_content,
32
  'sha': current_sha,
33
  'branch': branch_name
@@ -35,6 +38,20 @@ def push(new_content, path, file_name):
35
 
36
  update_url = f'https://api.github.com/repos/{repository_owner}/{repository_name}/contents/{file_path}'
37
  response = requests.put(update_url, json=update_data, headers=headers)
38
- response_json = response.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- return response_json
 
 
13
 
14
 
15
  def push(new_content, path, file_name):
16
+ if len(path) == 0:
17
+ file_path = file_name
18
+ else:
19
+ file_path = f'{path}/{file_name}'
20
 
21
  # Get the current file content
22
  url = f'https://api.github.com/repos/{repository_owner}/{repository_name}/contents/{file_path}?ref={branch_name}'
 
30
 
31
  # Update the file content
32
  update_data = {
33
+ 'message': f'Update {file_path} via API',
34
  'content': encoded_content,
35
  'sha': current_sha,
36
  'branch': branch_name
 
38
 
39
  update_url = f'https://api.github.com/repos/{repository_owner}/{repository_name}/contents/{file_path}'
40
  response = requests.put(update_url, json=update_data, headers=headers)
41
+ return response
42
+
43
+
44
+ def trigger_workflow(workflow_name):
45
+ api_url = f'https://api.github.com/repos/{repository_owner}/{repository_name}/actions/workflows/{workflow_name}/dispatches'
46
+
47
+ headers = {
48
+ 'Authorization': f'Bearer {github_token}',
49
+ 'Accept': 'application/vnd.github.v3+json',
50
+ }
51
+
52
+ payload = {
53
+ 'ref': 'main'
54
+ }
55
 
56
+ response = requests.post(api_url, headers=headers, json=payload)
57
+ return response