-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_form.html
108 lines (99 loc) · 2.81 KB
/
test_form.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Form</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js" type="text/javascript" charset="utf-8"></script>
<script>hljs.highlightAll();</script>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
display: flex;
}
#left {
flex: 0 0 30%;
padding: 20px;
display: flex;
flex-direction: column;
}
#right {
flex: 1;
padding: 20px;
display: flex;
flex-direction: column;
}
.ace_editor {
flex: 1;
width: 100%;
}
input {
width: 100%;
flex: none;
}
button {
padding: 10px 20px;
margin-top: 10px;
flex: none;
}
</style>
</head>
<body>
<div id="left">
<h2>Frontend input</h2>
<form id="jsonForm">
<input id="inputPrompt"/>
<button type="submit">Submit</button>
<button type="button" id="clearButton">Clear</button>
</form>
</div>
<div id="right">
<h2>Server output</h2>
<div id="jsonOutput" class="ace_editor"></div>
</div>
<script>
var editor = ace.edit("jsonOutput");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/json");
var input = document.getElementById('inputPrompt');
document.getElementById('jsonForm').addEventListener('submit', function(event) {
event.preventDefault();
var inputPrompt = input.value;
document.getElementById("inputPrompt").disabled = true;
fetch('/prompt', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ content: inputPrompt })
})
.then(response => {
if (response.headers.get('Content-Type').includes('application/json')) {
return response.json();
} else {
return response.text();
}
})
.then(data => {
if (typeof data === 'object') {
editor.setValue(JSON.stringify(data, null, 2))
} else {
editor.setValue(data)
}
document.getElementById("inputPrompt").disabled = false;
})
.catch(error => {
console.error('Error:', error);
editor.setValue(error.toString())
});
});
document.getElementById('clearButton').addEventListener('click', function() {
editor.setValue('');
input.value=""
});
</script>
</body>
</html>