Upload 4 files
Browse files
20250525_searchdata_webapp_v2/app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, send_from_directory, jsonify, request
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
df = pd.DataFrame({
|
| 6 |
+
'name': ['Alice', 'Bob', 'Charlie', 'David'],
|
| 7 |
+
'age': [25, 30, 35, 40],
|
| 8 |
+
'city': ['New York', 'Los Angeles', 'Chicago', 'Houston']})
|
| 9 |
+
|
| 10 |
+
@app.route('/')
|
| 11 |
+
def serve_index():
|
| 12 |
+
return send_from_directory('.', 'index.html')
|
| 13 |
+
|
| 14 |
+
@app.route('/static/<path:path>')
|
| 15 |
+
def serve_static(path):
|
| 16 |
+
return send_from_directory('static', path)
|
| 17 |
+
|
| 18 |
+
@app.route('search', methods=['POST'])
|
| 19 |
+
def search():
|
| 20 |
+
keyword = request.json.get('keyword', '').lower()
|
| 21 |
+
if keyword:
|
| 22 |
+
result = df[df['name'].str.lower().str.contains(keyword)]
|
| 23 |
+
else:
|
| 24 |
+
result = df.head(5)
|
| 25 |
+
return jsonify(result.to_dict(orient='records'))
|
| 26 |
+
|
| 27 |
+
if __name__ == '__main__':
|
| 28 |
+
app.run(debug=True)
|
| 29 |
+
|
| 30 |
+
|
20250525_searchdata_webapp_v2/index.html
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>データ検索</title>
|
| 7 |
+
</head>
|
| 8 |
+
<body>
|
| 9 |
+
<h1>検索ページ</h1>
|
| 10 |
+
<input type="text" id="searchInput" placeholder="名前で検索">
|
| 11 |
+
<button onclick="performSearch()">検索</button>
|
| 12 |
+
<table id="resultsTable" border="1"></table>
|
| 13 |
+
|
| 14 |
+
<!-- ここにJSで結果を挿入する -->
|
| 15 |
+
|
| 16 |
+
<script src="./static/main.js"></script>
|
| 17 |
+
</body>
|
| 18 |
+
</html>
|
20250525_searchdata_webapp_v2/static/main.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
async function performSearch() {
|
| 2 |
+
const keywords = document.querySelector('#searchInput').value;
|
| 3 |
+
|
| 4 |
+
try{
|
| 5 |
+
const response = await fetch('/search', {
|
| 6 |
+
method: 'POST',
|
| 7 |
+
headers: {'content-type': 'application/json'},
|
| 8 |
+
body: JSON.stringify({keywords: keywords})
|
| 9 |
+
});
|
| 10 |
+
|
| 11 |
+
const data = await response.json();
|
| 12 |
+
displayResults(data);
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
} catch (error) {
|
| 18 |
+
console.log(`Error: ${error}`);
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
function displayResults(data) {
|
| 23 |
+
const table = document.querySelector('#resultsTable')
|
| 24 |
+
table.innerHTML = '';
|
| 25 |
+
|
| 26 |
+
if (data.length === 0) {
|
| 27 |
+
const tr = document.createElement('tr');
|
| 28 |
+
const td = document.createElement('td');
|
| 29 |
+
td.textContent = '該当なし'
|
| 30 |
+
tr.appendChilt(td);
|
| 31 |
+
table.appentChild(tr)
|
| 32 |
+
return;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// ヘッダー作成
|
| 36 |
+
const headerRow = document.createElement('tr');
|
| 37 |
+
Object.keys(data[0]).forEach(key => {
|
| 38 |
+
const th = document.createElement('th');
|
| 39 |
+
th.textContent = key;
|
| 40 |
+
headerRow.appendChild(th);
|
| 41 |
+
});
|
| 42 |
+
table.appendChild(headerRow)
|
| 43 |
+
|
| 44 |
+
// データ行の作成
|
| 45 |
+
data.forEach(row => {
|
| 46 |
+
const tr = document.createElement('tr');
|
| 47 |
+
Object.values(row).forEach(cell =>{
|
| 48 |
+
const td = document.createElement('td');
|
| 49 |
+
td.textContent = cell;
|
| 50 |
+
tr.appendChild(td);
|
| 51 |
+
});
|
| 52 |
+
table.appendChild(tr);
|
| 53 |
+
});
|
| 54 |
+
}
|
20250525_searchdata_webapp_v2/static/style.css
ADDED
|
File without changes
|