-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
138 lines (124 loc) · 3.19 KB
/
Copy pathapp.go
File metadata and controls
138 lines (124 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"syscall"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
func (a *App) CheckPythonAvailable() (bool, string) {
cmd := exec.Command("python", "-c", "print('Hello, Python!')")
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
err := cmd.Run()
if err != nil {
return false, err.Error()
}
return true, "Python is available"
}
func (a *App) RunPythonCode(code string) (string, bool) {
// 要运行结果
cmd := exec.Command("python", "-c", code)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
out, err := cmd.CombinedOutput()
if err != nil {
return string(out), false
}
fmt.Print("USR IN|\t\t\n", code, "\n==================================\n")
fmt.Print("GO OUT|\t\t\n", string(out), "\n===============================\n")
return string(out), true
}
/*
{
"content": "Hi! 欢迎来到 Pix! 尝试输入一串 Python 代码, 以在控制台输出 \"Hello, World!\" 字符串吧!",
"id": 1,
"io": {
"preset": "#===分割线,勿删===\n#在这里输入代码, 请不要改动上方的代码\n",
"tests": [
{
"input": "",
"outputs": [
"Hello, World!\n"
]
}
]
},
"name": "开始!"
},
*/
type ProbJSON struct {
Content string `json:"content"`
ID int `json:"id"`
IO struct {
Preset string `json:"preset"`
Tests []struct {
Input string `json:"input"`
Outputs []string `json:"outputs"`
} `json:"tests"`
} `json:"io"`
Name string `json:"name"`
}
func (a *App) FetchPorbsFromServer() ([]ProbJSON, bool) {
var serverUrl string = "https://xnors.pythonanywhere.com/pixprobs"
resp, err := http.Get(serverUrl)
if err != nil {
return []ProbJSON{}, false
}
defer resp.Body.Close()
var prob []ProbJSON
err = json.NewDecoder(resp.Body).Decode(&prob)
if err != nil {
return []ProbJSON{}, false
}
return prob, true
}
type UserData struct {
MaxProbs int `json:"max_probs"`
}
func (a *App) SaveUserData(userData UserData) bool {
// 保存json到文件
jsonData, err := json.Marshal(userData)
if err != nil {
fmt.Print("ERROR when SaveUserData", err)
return false
}
err = os.WriteFile("userData.json", jsonData, 0644)
if err != nil {
fmt.Print("ERROR when SaveUserData", err)
return false
}
return true
}
func (a *App) LoadUserData() (UserData, bool) {
// 从文件读取json
jsonData, err := os.ReadFile("userData.json")
if err != nil {
fmt.Print("ERROR when LoadUserData", err)
return UserData{}, false
}
var userData UserData
err = json.Unmarshal(jsonData, &userData)
if err != nil {
fmt.Print("ERROR when LoadUserData", err)
return UserData{}, false
}
return userData, true
}