mirror of
https://github.com/kxgx/2.13-Ink-screen-clock.git
synced 2026-03-16 07:13:17 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
863e9b2f5d | ||
|
|
d523a31006 | ||
|
|
9d4895fca4 | ||
|
|
e7f71c699e | ||
|
|
419f44be7e |
76
app/app.py
76
app/app.py
@@ -1,9 +1,11 @@
|
||||
from flask import Flask, render_template, request, send_from_directory
|
||||
from flask import Flask, render_template, request, send_from_directory, redirect, url_for, jsonify
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
app = Flask(__name__, template_folder='webui', static_url_path='', static_folder='webui')
|
||||
FONT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'pic') # 字体文件夹路径
|
||||
MAIN_PY_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'bin', 'main.py')
|
||||
|
||||
def list_font_files(font_dir):
|
||||
try:
|
||||
@@ -12,36 +14,36 @@ def list_font_files(font_dir):
|
||||
print(f"Error listing font files: {e}")
|
||||
return [] # 返回空列表以避免迭代错误
|
||||
|
||||
def update_main_py_font_names(font_names):
|
||||
main_py_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'main.py')
|
||||
@app.route('/execute-shell')
|
||||
def execute_shell():
|
||||
# 替换以下命令为您想要执行的Shell命令
|
||||
command = "sudo reboot now"
|
||||
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
if result.returncode == 0:
|
||||
return jsonify({'status': 'success', 'output': result.stdout})
|
||||
else:
|
||||
return jsonify({'status': 'error', 'output': result.stderr})
|
||||
|
||||
@app.route('/edit_main_py')
|
||||
def edit_main_py():
|
||||
try:
|
||||
with open(main_py_path, 'r') as file:
|
||||
with open(MAIN_PY_PATH, 'r') as file:
|
||||
content = file.read()
|
||||
|
||||
# 为每个字体变量定义正则表达式模式
|
||||
patterns = {
|
||||
'font01': re.compile(r"(?<=font01\s*=\s*ImageFont\.truetype\(\s*os\.path\.join\(picdir,\s*)'[^']+'(?=\s*,\s*20\s*\))"),
|
||||
'font02': re.compile(r"(?<=font02\s*=\s*ImageFont\.truetype\(\s*os\.path\.join\(picdir,\s*)'[^']+'(?=\s*,\s*15\s*\))"),
|
||||
'font03': re.compile(r"(?<=font03\s*=\s*ImageFont\.truetype\(\s*os\.path\.join\(picdir,\s*)'[^']+'(?=\s*,\s*38\s*\))"),
|
||||
'font04': re.compile(r"(?<=font04\s*=\s*ImageFont\.truetype\(\s*os\.path\.join\(picdir,\s*)'[^']+'(?=\s*,\s*10\s*\))"),
|
||||
'font05': re.compile(r"(?<=font05\s*=\s*ImageFont\.truetype\(\s*os\.path\.join\(picdir,\s*)'[^']+'(?=\s*,\s*12\s*\))"),
|
||||
'font06': re.compile(r"(?<=font06\s*=\s*ImageFont\.truetype\(\s*os\.path\.join\(picdir,\s*)'[^']+'(?=\s*,\s*13\s*\))"),
|
||||
}
|
||||
|
||||
# 使用正则表达式替换字体文件名
|
||||
for font_var, font_name in font_names.items():
|
||||
pattern = patterns.get(font_var)
|
||||
if pattern:
|
||||
# 确保字体文件名被正确地转义
|
||||
safe_font_name = re.escape(font_name)
|
||||
content = pattern.sub(f"'{safe_font_name}'", content)
|
||||
|
||||
with open(main_py_path, 'w') as file:
|
||||
file.write(content)
|
||||
except Exception as e:
|
||||
print(f"Error updating main.py: {e}")
|
||||
return False
|
||||
return True
|
||||
return f"Error reading main.py: {e}", 500
|
||||
return render_template('edit_main_py.html', content=content)
|
||||
|
||||
@app.route('/save_main_py', methods=['POST'])
|
||||
def save_main_py():
|
||||
new_content = request.form.get('content')
|
||||
if new_content is None:
|
||||
return "No content provided", 400
|
||||
try:
|
||||
with open(MAIN_PY_PATH, 'w') as file:
|
||||
file.write(new_content)
|
||||
except Exception as e:
|
||||
return f"Error saving main.py: {e}", 500
|
||||
return redirect(url_for('index'))
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
@@ -65,24 +67,6 @@ def update_font_names():
|
||||
font_files = list_font_files(FONT_DIR)
|
||||
return render_template('update_font_names.html', font_files=font_files)
|
||||
|
||||
@app.route('/save_font_names', methods=['POST'])
|
||||
def save_font_names():
|
||||
# 获取表单数据
|
||||
font_names = {
|
||||
'font01': request.form.get('font01'),
|
||||
'font02': request.form.get('font02'),
|
||||
'font03': request.form.get('font03'),
|
||||
'font04': request.form.get('font04'),
|
||||
'font05': request.form.get('font05'),
|
||||
'font06': request.form.get('font06'),
|
||||
}
|
||||
|
||||
# 更新 main.py 中的字体文件名
|
||||
if update_main_py_font_names(font_names):
|
||||
return '字体文件名已保存'
|
||||
else:
|
||||
return '保存字体文件名时发生错误', 500
|
||||
|
||||
@app.route('/fonts/<filename>')
|
||||
def fonts(filename):
|
||||
return send_from_directory(FONT_DIR, filename)
|
||||
|
||||
9
app/webui/default.min.css
vendored
Normal file
9
app/webui/default.min.css
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
/*!
|
||||
Theme: Default
|
||||
Description: Original highlight.js style
|
||||
Author: (c) Ivan Sagalaev <maniac@softwaremaniacs.org>
|
||||
Maintainer: @highlightjs/core-team
|
||||
Website: https://highlightjs.org/
|
||||
License: see project LICENSE
|
||||
Touched: 2021
|
||||
*/pre code.hljs{display:block;overflow-x:auto;padding:1em}code.hljs{padding:3px 5px}.hljs{background:#f3f3f3;color:#444}.hljs-comment{color:#697070}.hljs-punctuation,.hljs-tag{color:#444a}.hljs-tag .hljs-attr,.hljs-tag .hljs-name{color:#444}.hljs-attribute,.hljs-doctag,.hljs-keyword,.hljs-meta .hljs-keyword,.hljs-name,.hljs-selector-tag{font-weight:700}.hljs-deletion,.hljs-number,.hljs-quote,.hljs-selector-class,.hljs-selector-id,.hljs-string,.hljs-template-tag,.hljs-type{color:#800}.hljs-section,.hljs-title{color:#800;font-weight:700}.hljs-link,.hljs-operator,.hljs-regexp,.hljs-selector-attr,.hljs-selector-pseudo,.hljs-symbol,.hljs-template-variable,.hljs-variable{color:#ab5656}.hljs-literal{color:#695}.hljs-addition,.hljs-built_in,.hljs-bullet,.hljs-code{color:#397300}.hljs-meta{color:#1f7199}.hljs-meta .hljs-string{color:#38a}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}
|
||||
59
app/webui/edit_main_py.html
Normal file
59
app/webui/edit_main_py.html
Normal file
@@ -0,0 +1,59 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>编辑 main.py</title>
|
||||
<!-- 引用本地的highlight.js CSS -->
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='default.min.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
<h1>编辑 main.py</h1>
|
||||
<!-- 添加一个表单来提交编辑后的代码 -->
|
||||
<form method="post" action="{{ url_for('save_main_py') }}">
|
||||
<pre style="background-color: black !important;"><code class="python" id="code">{{ content }}</code></pre>
|
||||
<input type="hidden" name="content" id="hidden-content">
|
||||
<input type="submit" value="保存">
|
||||
</form>
|
||||
|
||||
<!-- 引用本地的highlight.js JavaScript -->
|
||||
<script src="{{ url_for('static', filename='highlight.min.js') }}"></script>
|
||||
<script>
|
||||
// 初始化highlight.js
|
||||
document.addEventListener('DOMContentLoaded', (event) => {
|
||||
hljs.highlightAll();
|
||||
});
|
||||
</script>
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
var preElements = document.querySelectorAll('pre.code-editor');
|
||||
preElements.forEach(function(pre) {
|
||||
pre.style.backgroundColor = 'black';
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<h2>重启</h2>
|
||||
</head>
|
||||
<body>
|
||||
<button id="executeShellBtn">重启</button>
|
||||
|
||||
<script>
|
||||
document.getElementById('executeShellBtn').addEventListener('click', function() {
|
||||
fetch('/execute-shell')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.status === 'success') {
|
||||
alert('Command executed successfully:\n' + data.output);
|
||||
} else {
|
||||
alert('Error executing command:\n' + data.output);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
alert('Error: ' + error);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
1213
app/webui/highlight.min.js
vendored
Normal file
1213
app/webui/highlight.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@@ -19,5 +19,9 @@
|
||||
<li>没有找到字体文件。</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
<!-- 添加跳转到edit_main_py的按钮 -->
|
||||
<a href="/edit_main_py"><button>编辑main.py</button></a>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -252,4 +252,4 @@ epd.init()
|
||||
epd.Clear(0xFF) # 清除屏幕内容
|
||||
epd.sleep() # 使屏幕进入休眠状态
|
||||
epd2in13_V4.epdconfig.module_exit() # 清理资源
|
||||
exit()
|
||||
exit()
|
||||
@@ -96,4 +96,4 @@ except Exception as e:
|
||||
exit()
|
||||
|
||||
# 脚本正常结束后的清理操作
|
||||
exit()
|
||||
exit()
|
||||
Reference in New Issue
Block a user