Spaces:
Runtime error
Runtime error
Commit
·
fd5da23
1
Parent(s):
67a3984
test pygame and github upload
Browse files- app.py +25 -0
- file_manager.py +40 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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])
|
| 21 |
+
gr.Markdown("""[Play the game!](https://pjavanrooyen.github.io/text-to-game/)""")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
if __name__ == "__main__":
|
| 25 |
+
demo.launch()
|
file_manager.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import base64
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# GitHub repository information
|
| 7 |
+
repository_owner = os.environ['GIT_USER_NAME']
|
| 8 |
+
repository_name = os.environ['GIT_REPO_NAME']
|
| 9 |
+
branch_name = 'main'
|
| 10 |
+
|
| 11 |
+
# GitHub API token (generate one in your GitHub account settings)
|
| 12 |
+
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}'
|
| 20 |
+
headers = {'Authorization': f'token {github_token}'}
|
| 21 |
+
response = requests.get(url, headers=headers)
|
| 22 |
+
response_json = response.json()
|
| 23 |
+
# Extract necessary information
|
| 24 |
+
current_sha = response_json['sha']
|
| 25 |
+
|
| 26 |
+
encoded_content = base64.b64encode(new_content.encode()).decode()
|
| 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
|
| 34 |
+
}
|
| 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
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
requests
|