, но теперь я хочу отобрать HTML из Flask вместо непосредственно давать Содержание
Предыдущий код < /p>
import { get } from "../server/credentials/export.js";
var address;
document.addEventListener("DOMContentLoaded", async () => {
address = await get();
await fetch_data('space');
});
window.fetch_data = fetch_data;
window.back = back;
window.preview_file = preview_file
var current_path
async function fetch_data(path) {
if (path == 'space') {
document.querySelector('#back').style.display = 'none';
} else {
document.querySelector('#back').style.display = 'block';
}
var response = await fetch(`${address}/${path}`);
var data = await response.json();
current_path = path;
document.querySelector('#list').innerHTML = "";
document.querySelector('#status').innerHTML = `CURRENT PATH : ${path.toUpperCase()}`;
if ("folder" in data) {
const allPromises = data["folder"].map(async (item) => {
let newPath = `${path}/${item}`;
let item_response = await fetch(`${address}/${newPath}`);
let item_data = await item_response.json();
if ("folder" in item_data) {
document.querySelector('#list').innerHTML += `[*]
} else {
document.querySelector('#list').innerHTML += `[*]
}
});
await Promise.all(allPromises);
} else {
console.log("something happened bro...");
}
}
async function preview_file(path) {
var response = await fetch(`${address}/${path}`);
var data = await response.json();
console.log(data)
var newWindow = window.open('', '_blank');
if (newWindow) {
newWindow.document.open();
newWindow.document.write(`${data['file']}`);
newWindow.document.close();
} else {
console.error("POP UP ERROR");
}
}
async function back(){
current_path = current_path.split('/').slice(0, -1).join('/');
console.log(current_path.split('/').slice(0, -1))
await fetch_data(current_path)
}
< /code>
И это был мой предыдущий код Flask < /p>
# ---- PATH FUNCTION ----
@app.route('/', methods=['GET'])
def space(spacename):
space_path = os.path.join(DATABASE_DIR, spacename)
if not os.path.exists(space_path):
return Response(json.dumps({"error": "Not Found" }), status=404, mimetype='application/json')
if os.path.isdir(space_path):
space_contents = os.listdir(space_path)
return Response(json.dumps({"folder": space_contents}), status=200, mimetype='application/json')
# return Response(json.dumps(space_contents), status=200, mimetype='application/json')
if os.path.isfile(space_path):
with open(f"{space_path}","r") as file:
file_content = file.read()
return Response(json.dumps({"file" : file_content}), status=200, mimetype='text/plain')
< /code>
Я хочу использовать < /p>
return render_template("index.html", file_content=file_content)
< /code>
вместо < /p>
return Response(json.dumps({"file" : file_content}), status=200, mimetype='text/plain')
Подробнее здесь: https://stackoverflow.com/questions/794 ... r-and-json
Мобильная версия