Prva verzija mini web serverja (flask). Hvala, Github Copilot.
This commit is contained in:
5
web/__init__.py
Normal file
5
web/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""Package for web-related modules."""
|
||||
|
||||
from .server import app, set_projector_app, start_server_thread, run_server
|
||||
|
||||
__all__ = ["app", "set_projector_app", "start_server_thread", "run_server"]
|
||||
173
web/server.py
Normal file
173
web/server.py
Normal file
@@ -0,0 +1,173 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""Flask web server for the song projector application.
|
||||
|
||||
HTML, CSS and JavaScript are now stored in separate template/static files rather
|
||||
than being generated inside Python code.
|
||||
"""
|
||||
|
||||
import threading
|
||||
import os
|
||||
|
||||
from flask import Flask, render_template, request, jsonify
|
||||
|
||||
# create Flask app with proper folders relative to this file
|
||||
app = Flask(__name__, static_folder="static", template_folder="templates")
|
||||
|
||||
# Globalna referenca na SongProjector aplikaciju
|
||||
_projector_app = None
|
||||
|
||||
|
||||
def set_projector_app(projector_app):
|
||||
"""Postavi referenco na SongProjector aplikacijo"""
|
||||
global _projector_app
|
||||
_projector_app = projector_app
|
||||
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Flask rute
|
||||
# ----------------------------------------------------------
|
||||
|
||||
@app.route("/", methods=["GET"])
|
||||
def index():
|
||||
"""Prikaže glavno HTML stranko"""
|
||||
# predaja nadzora Jinja2, ki poišče web/templates/index.html
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
@app.route('/api/state', methods=['GET'])
|
||||
def get_state():
|
||||
"""Vrati trenutno stanje aplikacije"""
|
||||
if _projector_app is None:
|
||||
return jsonify({
|
||||
'current_text': 'Napaka: Aplikacija ni inicijalizirana',
|
||||
'page_info': '',
|
||||
'caps_mode': False,
|
||||
'can_prev': False,
|
||||
'can_next': False
|
||||
})
|
||||
|
||||
current_text = ""
|
||||
page_info = ""
|
||||
can_prev = False
|
||||
can_next = False
|
||||
|
||||
if _projector_app.pages:
|
||||
current_text = _projector_app.pages[_projector_app.current_page_index]
|
||||
if _projector_app.all_caps_mode:
|
||||
current_text = current_text.upper()
|
||||
|
||||
current_page = _projector_app.current_page_index + 1
|
||||
total_pages = len(_projector_app.pages)
|
||||
page_info = f"{_projector_app.song_number_last} {current_page}/{total_pages}"
|
||||
|
||||
can_prev = _projector_app.current_page_index > 0
|
||||
can_next = _projector_app.current_page_index + 1 < len(_projector_app.pages)
|
||||
else:
|
||||
current_text = "Pripravljeno. Vpiši številko pesmi."
|
||||
|
||||
return jsonify({
|
||||
'current_text': current_text,
|
||||
'page_info': page_info,
|
||||
'caps_mode': _projector_app.all_caps_mode,
|
||||
'can_prev': can_prev,
|
||||
'can_next': can_next
|
||||
})
|
||||
|
||||
|
||||
@app.route('/api/load_song', methods=['POST'])
|
||||
def load_song():
|
||||
"""Naloži pesem po številki"""
|
||||
if _projector_app is None:
|
||||
return jsonify({'status': 'error', 'message': 'Aplikacija ni inicijalizirana'})
|
||||
|
||||
data = request.get_json()
|
||||
song_number = data.get('song_number', '').strip()
|
||||
|
||||
if song_number:
|
||||
_projector_app.song_number = song_number
|
||||
_projector_app.load_song()
|
||||
if hasattr(_projector_app, 'show_page'):
|
||||
_projector_app.show_page()
|
||||
|
||||
return jsonify({'status': 'ok'})
|
||||
|
||||
|
||||
@app.route('/api/next_page', methods=['POST'])
|
||||
def next_page():
|
||||
"""Naslednja stran"""
|
||||
if _projector_app is not None:
|
||||
_projector_app.next_page()
|
||||
|
||||
return jsonify({'status': 'ok'})
|
||||
|
||||
|
||||
@app.route('/api/prev_page', methods=['POST'])
|
||||
def prev_page():
|
||||
"""Prejšnja stran"""
|
||||
if _projector_app is not None:
|
||||
_projector_app.prev_page()
|
||||
|
||||
return jsonify({'status': 'ok'})
|
||||
|
||||
|
||||
@app.route('/api/clear_screen', methods=['POST'])
|
||||
def clear_screen():
|
||||
"""Zatemniti ekran"""
|
||||
if _projector_app is not None:
|
||||
_projector_app.clear_screen()
|
||||
|
||||
return jsonify({'status': 'ok'})
|
||||
|
||||
|
||||
@app.route('/api/toggle_caps', methods=['POST'])
|
||||
def toggle_caps():
|
||||
"""Preklop med velikimi in malimi črkami"""
|
||||
if _projector_app is not None:
|
||||
_projector_app.all_caps_mode = not _projector_app.all_caps_mode
|
||||
_projector_app.show_page()
|
||||
|
||||
return jsonify({'status': 'ok'})
|
||||
|
||||
|
||||
@app.route('/api/search_songs', methods=['POST'])
|
||||
def search_songs():
|
||||
"""Iskanje pesmi po naslovu"""
|
||||
if _projector_app is None:
|
||||
return jsonify({'results': []})
|
||||
|
||||
data = request.get_json()
|
||||
query = data.get('query', '').strip()
|
||||
|
||||
if not query:
|
||||
return jsonify({'results': []})
|
||||
|
||||
try:
|
||||
_projector_app.cursor.execute(
|
||||
"SELECT id, title FROM songs WHERE title LIKE ? COLLATE NOCASE",
|
||||
(f"%{query}%",)
|
||||
)
|
||||
results = _projector_app.cursor.fetchall()
|
||||
return jsonify({'results': results})
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)})
|
||||
|
||||
|
||||
def run_server(host='127.0.0.1', port=5000):
|
||||
"""Zaženi Flask server"""
|
||||
app.run(host=host, port=port, debug=False, use_reloader=False, threaded=True)
|
||||
|
||||
|
||||
def start_server_thread(projector_app, host='127.0.0.1', port=5000):
|
||||
"""Zaženi server v zvjenoj niti"""
|
||||
set_projector_app(projector_app)
|
||||
|
||||
server_thread = threading.Thread(
|
||||
target=run_server,
|
||||
args=(host, port),
|
||||
daemon=True
|
||||
)
|
||||
server_thread.start()
|
||||
|
||||
return server_thread
|
||||
157
web/static/script.js
Normal file
157
web/static/script.js
Normal file
@@ -0,0 +1,157 @@
|
||||
console.log('JavaScript se izvaja...');
|
||||
|
||||
// DOM elementi
|
||||
const songNumberInput = document.getElementById('song-number');
|
||||
const loadBtn = document.getElementById('load-btn');
|
||||
const searchQueryInput = document.getElementById('search-query');
|
||||
const searchBtn = document.getElementById('search-btn');
|
||||
const capsBtn = document.getElementById('caps-btn');
|
||||
const displayArea = document.getElementById('display-area');
|
||||
const prevBtn = document.getElementById('prev-btn');
|
||||
const nextBtn = document.getElementById('next-btn');
|
||||
const darkBtn = document.getElementById('dark-btn');
|
||||
const pageInfo = document.getElementById('page-info');
|
||||
|
||||
console.log('DOM elementi najdeni:', { songNumberInput: !!songNumberInput, loadBtn: !!loadBtn, searchQueryInput: !!searchQueryInput, searchBtn: !!searchBtn, capsBtn: !!capsBtn, displayArea: !!displayArea, prevBtn: !!prevBtn, nextBtn: !!nextBtn, darkBtn: !!darkBtn, pageInfo: !!pageInfo });
|
||||
|
||||
let capsMode = false;
|
||||
|
||||
// Naloži trenutne podatke
|
||||
async function updateState() {
|
||||
console.log('updateState() se kliče...');
|
||||
try {
|
||||
console.log('Pošiljam fetch za /api/state...');
|
||||
const response = await fetch('/api/state');
|
||||
console.log('Response status:', response.status);
|
||||
const data = await response.json();
|
||||
console.log('Response data:', data);
|
||||
|
||||
displayArea.textContent = data.current_text || 'Pripravljeno. Vpiši številko pesmi.';
|
||||
pageInfo.textContent = data.page_info || '';
|
||||
capsMode = data.caps_mode || false;
|
||||
|
||||
if (capsMode) {
|
||||
capsBtn.classList.add('active');
|
||||
} else {
|
||||
capsBtn.classList.remove('active');
|
||||
}
|
||||
|
||||
prevBtn.disabled = !data.can_prev;
|
||||
nextBtn.disabled = !data.can_next;
|
||||
console.log('updateState() uspešno zaključeno');
|
||||
} catch (error) {
|
||||
console.error('Napaka pri posodabljanju stanja:', error);
|
||||
displayArea.innerHTML = '<span class="status-message">Napaka: ni povezave do strežnika</span>';
|
||||
prevBtn.disabled = true; nextBtn.disabled = true; loadBtn.disabled = true; searchBtn.disabled = true; capsBtn.disabled = true; darkBtn.disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Naloži pesem
|
||||
async function loadSong() {
|
||||
console.log('loadSong() se kliče');
|
||||
const songNumber = songNumberInput.value.trim();
|
||||
console.log('Song number:', songNumber);
|
||||
if (!songNumber) return;
|
||||
|
||||
try {
|
||||
console.log('Pošiljam POST za /api/load_song...');
|
||||
const response = await fetch('/api/load_song', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({song_number: songNumber}) });
|
||||
console.log('Response status:', response.status);
|
||||
const data = await response.json();
|
||||
console.log('Response data:', data);
|
||||
|
||||
songNumberInput.value = '';
|
||||
updateState();
|
||||
} catch (error) {
|
||||
console.error('Napaka pri nalaganju pesmi:', error);
|
||||
displayArea.innerHTML = '<span class="status-message">Napaka: ni povezave do strežnika</span>';
|
||||
}
|
||||
}
|
||||
|
||||
// Naslednja stran
|
||||
async function nextPage() {
|
||||
try {
|
||||
await fetch('/api/next_page', {method: 'POST'});
|
||||
updateState();
|
||||
} catch (error) {
|
||||
console.error('Napaka pri navigaciji:', error);
|
||||
displayArea.innerHTML = '<span class="status-message">Napaka: ni povezave do strežnika</span>';
|
||||
}
|
||||
}
|
||||
|
||||
// Prejšnja stran
|
||||
async function prevPage() {
|
||||
try {
|
||||
await fetch('/api/prev_page', {method: 'POST'});
|
||||
updateState();
|
||||
} catch (error) {
|
||||
console.error('Napaka pri navigaciji:', error);
|
||||
displayArea.innerHTML = '<span class="status-message">Napaka: ni povezave do strežnika</span>';
|
||||
}
|
||||
}
|
||||
|
||||
// Zatemnitev ekrana
|
||||
async function clearScreen() {
|
||||
try {
|
||||
await fetch('/api/clear_screen', {method: 'POST'});
|
||||
updateState();
|
||||
} catch (error) {
|
||||
console.error('Napaka pri zatamnitvi ekrana:', error);
|
||||
displayArea.innerHTML = '<span class="status-message">Napaka: ni povezave do strežnika</span>';
|
||||
}
|
||||
}
|
||||
|
||||
// Preklop VELIKIH ČRK
|
||||
async function toggleCaps() {
|
||||
try {
|
||||
const response = await fetch('/api/toggle_caps', {method: 'POST'});
|
||||
updateState();
|
||||
} catch (error) {
|
||||
console.error('Napaka pri preklopa velikih črk:', error); displayArea.innerHTML = '<span class="status-message">Napaka: ni povezave do strežnika</span>'; }
|
||||
}
|
||||
|
||||
// Iskanje pesmi
|
||||
async function searchSongs() {
|
||||
const query = searchQueryInput.value.trim();
|
||||
if (!query) return;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/search_songs', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({query: query}) });
|
||||
const data = await response.json();
|
||||
|
||||
if (data.results && data.results.length > 0) {
|
||||
const resultList = data.results.map(item => `${item[0]}: ${item[1]}`).join('\n');
|
||||
displayArea.innerHTML = '<span class="status-message">' + resultList.replace(/</g, '<').replace(/>/g, '>').replace(/\n/g, '<br>') + '</span>';
|
||||
} else {
|
||||
displayArea.innerHTML = '<span class="status-message">Ni zadetkov.</span>';
|
||||
}
|
||||
searchQueryInput.value = '';
|
||||
} catch (error) {
|
||||
console.error('Napaka pri iskanju:', error);
|
||||
displayArea.innerHTML = '<span class="status-message">Napaka: ni povezave do strežnika</span>';
|
||||
}
|
||||
}
|
||||
|
||||
// Dodaj poslušalce
|
||||
console.log('Dodajam event listenerje...');
|
||||
loadBtn.addEventListener('click', loadSong);
|
||||
console.log('loadBtn listener dodan');
|
||||
songNumberInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') loadSong(); });
|
||||
console.log('songNumberInput listener dodan');
|
||||
|
||||
searchBtn.addEventListener('click', searchSongs);
|
||||
searchQueryInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') searchSongs(); });
|
||||
|
||||
capsBtn.addEventListener('click', toggleCaps);
|
||||
prevBtn.addEventListener('click', prevPage);
|
||||
nextBtn.addEventListener('click', nextPage);
|
||||
darkBtn.addEventListener('click', clearScreen);
|
||||
console.log('Vsi event listenerji dodani');
|
||||
|
||||
// Začetna inicijalizacija
|
||||
console.log('Začenjam začetno inicijalizacijo...');
|
||||
updateState();
|
||||
|
||||
// Osveži stanje vsako sekundo (za sinhronizacijo s tipkovnico)
|
||||
// setInterval(updateState, 1000);
|
||||
console.log('JavaScript inicializacija zaključena');
|
||||
153
web/static/styles.css
Normal file
153
web/static/styles.css
Normal file
@@ -0,0 +1,153 @@
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: 'Times New Roman', serif;
|
||||
background-color: #0a0a0a;
|
||||
color: #ffffff;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
.container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
/* Gornja vrstica - Vnos pesmi */
|
||||
.top-bar {
|
||||
background-color: #1a1a1a;
|
||||
padding: 20px;
|
||||
border-bottom: 2px solid #333;
|
||||
display: flex;
|
||||
gap: 15px;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.top-bar label {
|
||||
font-size: 16px;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.top-bar input {
|
||||
padding: 10px 15px;
|
||||
font-size: 16px;
|
||||
background-color: #2a2a2a;
|
||||
border: 1px solid #444;
|
||||
color: #ffffff;
|
||||
border-radius: 4px;
|
||||
width: 100px;
|
||||
}
|
||||
|
||||
.top-bar button {
|
||||
padding: 10px 20px;
|
||||
font-size: 16px;
|
||||
background-color: #3a7ca5;
|
||||
border: none;
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
border-radius: 4px;
|
||||
transition: background-color 0.3s;
|
||||
}
|
||||
|
||||
.top-bar button:hover {
|
||||
background-color: #2d6183;
|
||||
}
|
||||
|
||||
.caps-toggle {
|
||||
padding: 10px 15px;
|
||||
font-size: 14px;
|
||||
background-color: #4a4a4a;
|
||||
border: 1px solid #666;
|
||||
color: #ffffff;
|
||||
cursor: pointer;
|
||||
border-radius: 4px;
|
||||
transition: background-color 0.3s;
|
||||
}
|
||||
|
||||
.caps-toggle.active {
|
||||
background-color: #3a7ca5;
|
||||
}
|
||||
|
||||
/* Sredenska vrstica - Prikaz besedila */
|
||||
.content {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 40px;
|
||||
overflow-y: auto;
|
||||
background-color: #000000;
|
||||
}
|
||||
|
||||
.lyrics-display {
|
||||
text-align: center;
|
||||
font-size: 32px;
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
max-width: 90%;
|
||||
}
|
||||
|
||||
.status-message {
|
||||
color: #aaa;
|
||||
font-size: 24px;
|
||||
}
|
||||
|
||||
/* Donja vrstica - Navigacijski gumbi */
|
||||
.bottom-bar {
|
||||
background-color: #1a1a1a;
|
||||
padding: 20px;
|
||||
border-top: 2px solid #333;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 20px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.bottom-bar button {
|
||||
padding: 15px 40px;
|
||||
font-size: 18px;
|
||||
border: none;
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
border-radius: 4px;
|
||||
transition: background-color 0.3s;
|
||||
min-width: 150px;
|
||||
}
|
||||
|
||||
.btn-nav {
|
||||
background-color: #556b79;
|
||||
}
|
||||
|
||||
.btn-nav:hover {
|
||||
background-color: #3d4a52;
|
||||
}
|
||||
|
||||
.btn-nav:disabled {
|
||||
background-color: #2a2a2a;
|
||||
cursor: not-allowed;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.btn-dark {
|
||||
background-color: #8b4513;
|
||||
min-width: 150px;
|
||||
}
|
||||
|
||||
.btn-dark:hover {
|
||||
background-color: #6b3410;
|
||||
}
|
||||
|
||||
.page-info {
|
||||
color: #999;
|
||||
font-size: 16px;
|
||||
min-width: 60px;
|
||||
}
|
||||
43
web/templates/index.html
Normal file
43
web/templates/index.html
Normal file
@@ -0,0 +1,43 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="sl">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Projektor pesmi - Web sučelje</title>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<!-- Gornja vrstica -->
|
||||
<div class="top-bar">
|
||||
<label for="song-number">Pesem:</label>
|
||||
<input type="number" id="song-number" placeholder="Vpiši številko" min="0">
|
||||
<button id="load-btn">Naloži</button>
|
||||
|
||||
<!-- <label for="search-query">Iskanje:</label>
|
||||
<input type="text" id="search-query" placeholder="Naslovna beseda...">
|
||||
<button id="search-btn">Išči</button> -->
|
||||
|
||||
<button id="caps-btn" class="btn-dark">VELIKE črke</button>
|
||||
</div>
|
||||
|
||||
<!-- Sredenska vrstica - Prikaz -->
|
||||
<div class="content">
|
||||
<div id="display-area" class="lyrics-display">
|
||||
<span class="status-message">Pripravljeno. Vpiši številko pesmi.</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Spodnja vrstica - Navigacija -->
|
||||
<div class="bottom-bar">
|
||||
<button id="prev-btn" class="btn-nav">◀ Nazaj</button>
|
||||
<span id="page-info" class="page-info"></span>
|
||||
<button id="dark-btn" class="btn-dark">Zatemni ekran</button>
|
||||
<span id="page-info-right" class="page-info"></span>
|
||||
<button id="next-btn" class="btn-nav">Naprej ▶</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="{{ url_for('static', filename='script.js') }}"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user