簡體   English   中英

將數據從燒瓶傳遞到 HTML

[英]Passing data from flask to HTML

我試圖將路徑列表傳遞給 HTML,但由於某種原因,輸出是一個空白的 HTML 頁面。 下面是我的 HTML 和 python 代碼。 我是網絡開發新手。 所以我在這里掙扎。

這是我的python代碼,我從中提取包含.md文件的目錄路徑,它將返回目錄路徑列表,我必須通過模板顯示。

from flask import Flask,render_template, request, redirect, url_for
from git import Repo
import os
from urllib.parse import urlparse
from pathlib import Path

app = Flask(__name__)

def parser(full_path):

path = full_path

files = []
for r, d, f in os.walk(path):
    for file in f:
        if '.md' in file:
            if 'README.md' not in file:

                root = r.replace(path,"")
                url_path = root + "/" + file.replace('.md', '')
                path = str(url_path)
                files.append(path)
return files

def create_dir(gh_url):
#    from ipdb import set_trace;set_trace()
git_url = gh_url

git_dir = git_url.strip('/').split('/')
git_dir = git_dir[-1]

if not os.path.exists(git_dir):
    os.mkdir(git_dir)

home = Path(Path.home())
new_dir = "new_dir"

if not os.path.exists(new_dir):
    os.mkdir(new_dir)

path = os.path.join(home, new_dir)

full_path = os.path.join(path, git_dir)

Repo.clone_from(git_url, full_path)

return parser(full_path)

@app.route("/", methods=["GET", "POST"])
def index():
files = []
if request.method == 'POST':
    gh_url  = request.form['gh_url']

    if len(gh_url) == 0:
        message = "Please enter the github repo"
    else:
        files = create_dir(gh_url)
return render_template('index.html', files=files)                

app.run(debug=True)

index.html 頁面是

{% extends 'base.html' %}

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>{% block title %}Enter the Github repository{% endblock %}</title>
</head>
<body>
<ul>
 {% for name in files %}
 <li>{{ name }}</li>
 {% endfor %}
</ul>

<ol>
 {% for name in files %}
 <li>{{ name }}</li>
 {% endfor %}
</ol>
</body>
</html>

base.html 是

{% block title %}Raw Form Example{% endblock %}

{% block content %}
<form method = "POST">
    <p>
        <label for="gh-label">GitHub URL</label>
        <input id="gh-url" name="gh_url" type="text">
    </p>

    <input type="submit" value="Show">
</form>
{% endblock %}

首先,您應該將{% extends 'base.html' %}index.htmlbase.html ,然后將 index.html 重命名為 base.html,將 base.html 重命名為 index.html。

請仔細閱讀有關模板繼承的文檔

那么你的 python 代碼的標識是錯誤的,它給重現你的例子帶來了困難。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM