-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
201 lines (185 loc) · 9.84 KB
/
index.html
File metadata and controls
201 lines (185 loc) · 9.84 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SQL Natural Language Interface</title>
<script src="https://cdn.jsdelivr.net/npm/react@18.2.0/umd/react.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/react-dom@18.2.0/umd/react-dom.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@babel/standalone@7.23.2/babel.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const { useState, useEffect } = React;
const App = () => {
const [schema, setSchema] = useState('');
const [query, setQuery] = useState('');
const [results, setResults] = useState(null);
const [sqlQuery, setSqlQuery] = useState('');
const [csvFile, setCsvFile] = useState(null);
const [error, setError] = useState('');
const [loading, setLoading] = useState(false);
const [summary, setSummary] = useState('');
// Fetch schema on mount
useEffect(() => {
fetchSchema();
}, []);
const fetchSchema = async () => {
try {
const response = await fetch('http://localhost:8001/fetch-schema');
const data = await response.json();
setSchema(data.schema);
} catch (err) {
setError('Failed to fetch schema');
}
};
const handleQuerySubmit = async (e) => {
e.preventDefault();
setError('');
setResults(null);
setSqlQuery('');
setCsvFile(null);
setSummary('');
setLoading(true);
try {
const response = await fetch('http://localhost:8001/generate-query', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ user_query: query, db_schema: schema })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
setSqlQuery(data.sql_query);
setResults(data.results);
if (data.csv_base64) {
const downloadLink = `data:text/csv;base64,${data.csv_base64}`;
setCsvFile({ name: data.csv_filename, link: downloadLink });
}
if (data.summary) setSummary(data.summary);
} catch (err) {
setError(`Error: ${err.message}`);
} finally {
setLoading(false);
}
};
return (
<div className="min-h-screen bg-gray-100 p-6">
<div className="max-w-4xl mx-auto">
<h1 className="text-3xl font-bold text-gray-800 mb-6">SQL Natural Language Interface</h1>
{/* Schema Display */}
<div className="bg-white p-4 rounded-lg shadow mb-6">
<h2 className="text-xl font-semibold mb-2">Database Schema</h2>
<pre className="bg-gray-50 p-4 rounded text-sm overflow-auto max-h-60">
{schema || 'Loading schema...'}
</pre>
</div>
{/* Query Input */}
<div className="bg-white p-4 rounded-lg shadow mb-6">
<h2 className="text-xl font-semibold mb-2">Enter Your Query</h2>
<form onSubmit={handleQuerySubmit}>
<textarea
className="w-full p-3 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
rows="4"
placeholder="e.g., List all customers who made purchases in the last month"
value={query}
onChange={(e) => setQuery(e.target.value)}
disabled={loading}
/>
<button
type="submit"
className={`mt-3 px-4 py-2 rounded-lg text-white ${loading ? 'bg-gray-400 cursor-not-allowed' : 'bg-blue-500 hover:bg-blue-600'}`}
disabled={loading}
>
{loading ? 'Processing...' : 'Generate SQL'}
</button>
</form>
</div>
{/* Error Message */}
{error && (
<div className="bg-red-100 p-4 rounded-lg text-red-700 mb-6">
{error}
</div>
)}
{/* Generated SQL */}
{sqlQuery && (
<div className="bg-white p-4 rounded-lg shadow mb-6">
<h2 className="text-xl font-semibold mb-2">Generated SQL Query</h2>
<pre className="bg-gray-50 p-4 rounded text-sm overflow-auto">
{sqlQuery}
</pre>
</div>
)}
{/* Results Table */}
{results && results.length > 0 && (
<div className="bg-white p-4 rounded-lg shadow mb-6">
<h2 className="text-xl font-semibold mb-2">Query Results</h2>
<div className="overflow-x-auto">
<table className="min-w-full divide-y divide-gray-200">
<thead className="bg-gray-50">
<tr>
{Object.keys(results[0]).map((key) => (
<th
key={key}
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
>
{key}
</th>
))}
</tr>
</thead>
<tbody className="bg-white divide-y divide-gray-200">
{results.map((row, index) => (
<tr key={index}>
{Object.values(row).map((value, i) => (
<td
key={i}
className="px-6 py-4 whitespace-nowrap text-sm text-gray-500"
>
{value !== null ? (
typeof value === 'object'
? <pre className="whitespace-pre-wrap">{JSON.stringify(value, null, 2)}</pre>
: value.toString()
) : (
'NULL'
)}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
</div>
)}
{/* CSV Download */}
{csvFile && (
<div className="bg-white p-4 rounded-lg shadow">
<h2 className="text-xl font-semibold mb-2">Download Results</h2>
<a
href={csvFile.link}
download={csvFile.name}
className="inline-block px-4 py-2 bg-green-500 text-white rounded-lg hover:bg-green-600"
>
Download CSV
</a>
</div>
)}
{/* Summary */}
{summary && (
<div className="mt-6 bg-white p-4 rounded-lg shadow mb-6">
<h2 className="text-xl font-semibold mb-2">Summary</h2>
<p className="text-gray-700">{summary}</p>
</div>
)}
</div>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('root'));
</script>
</body>
</html>