did some code
This commit is contained in:
62
main.py
Normal file
62
main.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import flask
|
||||||
|
from flask import render_template, jsonify, request, redirect
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import markdown
|
||||||
|
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def home():
|
||||||
|
return render_template('index.html')
|
||||||
|
|
||||||
|
@app.route('/read')
|
||||||
|
def readBook():
|
||||||
|
filepath = request.args.get('filepath')+"/book.md"
|
||||||
|
with open(filepath) as f:
|
||||||
|
mdtext = f.read()
|
||||||
|
renderedMd = markdown.markdown(mdtext)
|
||||||
|
finalHTML = "<a href='http://localhost:5000/'>Home</a>" + renderedMd
|
||||||
|
return finalHTML
|
||||||
|
|
||||||
|
@app.route('/notes')
|
||||||
|
def notesForBook():
|
||||||
|
notespath = request.args.get('filepath')+"/notes.txt"
|
||||||
|
with open(notespath) as f:
|
||||||
|
notestext = f.read()
|
||||||
|
return render_template('notes.html', path=notespath, notes=notestext)
|
||||||
|
|
||||||
|
@app.route('/savenotes', methods=['POST'])
|
||||||
|
def saveNotes():
|
||||||
|
path = request.form.get("path")
|
||||||
|
notes = request.form.get("notes")
|
||||||
|
with open(path, "w") as f:
|
||||||
|
f.write(notes)
|
||||||
|
return redirect("http://localhost:5000/", code=302)
|
||||||
|
|
||||||
|
@app.route('/api/textbooks')
|
||||||
|
def textbooks():
|
||||||
|
textbook_list = []
|
||||||
|
|
||||||
|
dir = './textbooks'
|
||||||
|
|
||||||
|
for dirpath, dirnames, filenames in os.walk(dir):
|
||||||
|
if 'manifest.json' in filenames:
|
||||||
|
manifest_path = os.path.join(dirpath, 'manifest.json')
|
||||||
|
try:
|
||||||
|
with open(manifest_path, 'r', encoding='utf-8') as f:
|
||||||
|
manifest_data = json.load(f)
|
||||||
|
data = {
|
||||||
|
"path": dirpath,
|
||||||
|
"name": manifest_data.get("name"),
|
||||||
|
"author": manifest_data.get("author")
|
||||||
|
}
|
||||||
|
textbook_list.append(data)
|
||||||
|
print(json.dumps(data, indent=2))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return jsonify(textbook_list)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(debug=True)
|
||||||
BIN
static/background.jpg
Normal file
BIN
static/background.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.7 MiB |
87
templates/index.html
Normal file
87
templates/index.html
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Textbooks</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
body{
|
||||||
|
background-image: url("static/background.jpg");
|
||||||
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||||
|
color: white;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
a{
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
div{
|
||||||
|
max-width: 50vw;
|
||||||
|
margin: auto;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<h1>Textbook Software</h1>
|
||||||
|
<div id="main-container">
|
||||||
|
<p>textbooks loading</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
async function renderData() {
|
||||||
|
const container = document.getElementById('main-container');
|
||||||
|
container.innerHTML = '<p>Fetching data from API...</p>';
|
||||||
|
|
||||||
|
const API_URL = 'http://localhost:5000/api/textbooks';
|
||||||
|
let items = [];
|
||||||
|
|
||||||
|
const response = await fetch(API_URL);
|
||||||
|
items = await response.json();
|
||||||
|
|
||||||
|
container.innerHTML = '';
|
||||||
|
|
||||||
|
if (items.length === 0) {
|
||||||
|
container.innerHTML = '<p>No texbooks</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
items.forEach((item) => {
|
||||||
|
const itemDiv = document.createElement('div');
|
||||||
|
|
||||||
|
itemDiv.appendChild(document.createElement('hr'));
|
||||||
|
|
||||||
|
const contentDiv = document.createElement('div');
|
||||||
|
|
||||||
|
contentDiv.innerHTML = `
|
||||||
|
<p>${item.name}</p>
|
||||||
|
<p>By ${item.author}</p>
|
||||||
|
`;
|
||||||
|
|
||||||
|
const linkButton = document.createElement('a');
|
||||||
|
linkButton.textContent = `[Read ${item.name}]`;
|
||||||
|
linkButton.href = `read?filepath=${encodeURIComponent(item.path)}`;
|
||||||
|
linkButton.title = `go to ${item.name}`;
|
||||||
|
contentDiv.appendChild(linkButton);
|
||||||
|
|
||||||
|
const notesButton = document.createElement('a');
|
||||||
|
notesButton.textContent = `[Notes for ${item.name}]`;
|
||||||
|
notesButton.href = `notes?filepath=${encodeURIComponent(item.path)}`;
|
||||||
|
notesButton.title = `view notes for ${item.name}`;
|
||||||
|
contentDiv.appendChild(notesButton);
|
||||||
|
|
||||||
|
|
||||||
|
itemDiv.appendChild(contentDiv);
|
||||||
|
|
||||||
|
container.appendChild(itemDiv);
|
||||||
|
});
|
||||||
|
|
||||||
|
container.appendChild(document.createElement('hr'));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', renderData);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
16
templates/notes.html
Normal file
16
templates/notes.html
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Notes</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<a href="http://localhost:5000/">Home (No save)</a>
|
||||||
|
<form action="http://localhost:5000/savenotes" method="post">
|
||||||
|
<input for="path" name="path" type="hidden" value="{{ path }}" style="display: hidden;"></input>
|
||||||
|
<textarea for="notes" name="notes">{{ notes }}</textarea>
|
||||||
|
<input type="submit" value="save"></input>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
3
textbooks/textbook1/book.md
Normal file
3
textbooks/textbook1/book.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Textbook 1
|
||||||
|
|
||||||
|
This is a demo textbook
|
||||||
4
textbooks/textbook1/manifest.json
Normal file
4
textbooks/textbook1/manifest.json
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"name":"Textbook1",
|
||||||
|
"author":"author"
|
||||||
|
}
|
||||||
1
textbooks/textbook1/notes.txt
Normal file
1
textbooks/textbook1/notes.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
my notes 12345
|
||||||
Reference in New Issue
Block a user