1

I am trying to pass a list of paths to HTML, but for some reason the output is a blank HTML page. below are my HTML and python code. I am new to web dev. so i am struggling here.

here is my python code from where i am extracting the path of dir containing .md files and it will return a list of dir paths and that i have to show through the template.

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)

The index.html page is

{% 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 is

{% 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 %}
3
  • You pass files to render_template('index.html', files=files) which can be None, if len(gh_url) != 0, it's not so good for your application (UnboundLocalError: local variable 'files' referenced before assignment). Are you going to fix it? Commented Dec 21, 2019 at 15:51
  • Hi, yes I'm fixing it Commented Dec 24, 2019 at 12:44
  • If you fix this and your templates - it must get better. Also pay attention to what is returned by webserver (inspect page source or use curl). Malformed page will look like blank too. Commented Dec 25, 2019 at 9:24

1 Answer 1

1

Firstrly you should move {% extends 'base.html' %} from index.html to base.html and then rename index.html to base.html and base.html to index.html.

Please, read documentation about template inheritance carefully.

Then your the identation of your python code is wrong and it creates difficulties to reproduce your example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.