from flask import Flask, url_for, render_template, Markup, redirect, request, flash from flask import session as login_session from config import Config import json import os from time import gmtime, strftime from pprint import pprint app = Flask(__name__, static_url_path='', static_folder="static", template_folder="templates") app.jinja_env.add_extension('jinja2.ext.loopcontrols') app.config.from_object(Config) # setting variables for holding paths, folder names and the one file for description path = "static/files/" jsonfiles = [] #json files fullpathjsonfiles = [] #fullpath for some situations listingfiles= [] #fullpaths listingdirectories = [] #paths jsonfiles = [] #json files fullpathjsonfiles = [] #fullpath for some situations thefile = None #selected file for description now = strftime("%Y-%m-%d_%H:%M:%S", gmtime()) #description time positioninarray = 8 #counter listofdicts=[] #to be able to import and use json content datafromjson = [] #listing paths and files, not in order but well... for path, subdirs, files in os.walk(path): for name in files: #excluding json files from listing :-) if not name.endswith(".json") and not name.endswith(".DS_Store"): fullpath = os.path.join(path, name) listingdirectories.append(path) listingfiles.append(fullpath[7:]) #fullpaths minus static/ if name.endswith(".json"): fullpath = os.path.join(path, name) jsonfiles.append(fullpath[7:]) # print(name) # for line in open(fullpath, "r"): # listofdicts.append(json.loads(line)) #================================================= #listing the json paths for path, subdirs, files in os.walk(path): for name in files: if name.endswith(".json"): fullpath = os.path.join(path, name) jsonfiles.append(fullpath[7:]) fullpathjsonfiles.append(fullpath) print(jsonfiles) print("-------------------------") dict = {} #dict for the form entries with open("static/"+jsonfiles[4], 'r') as f: data_dict = json.load(f) datafromjson = data_dict["files"] print(datafromjson) @app.route("/") def home(): return render_template('home.html') @app.route('/about/') def about(): return render_template('about.html') @app.route('/all/') def all(): thefile = listingfiles[positioninarray] print(listingfiles) # print(dict) counter2=0 return render_template('all.html', file=thefile, listingfiles=listingfiles, jsonfiles=jsonfiles, listofdicts=listofdicts, counter2=counter2) @app.route('/description') def description(): #open json file, list filepaths in array and loop with thefile with open("static/"+jsonfiles[4], 'r') as f: data_dict = json.load(f) datafromjson = data_dict["files"] itemid = data_dict["id"] for file in datafromjson: # # assigning html tag on the basis of extension, to be finished when all the files have been sent # if file.lower().endswith(('.png', '.jpg', '.jpeg')): # file = Markup('
') # #if sound # elif file.lower().endswith(('.mp3')): # file = Markup('''''') #if txtfile if file.lower().endswith(('.txt')): # print(thefile) with open("static/"+file,"r") as f: textfile = f.read() # #ifvid to be added # elif file.lower().endswith(('.mp4')): # file = Markup(''' ''') return render_template('description.html', datafromjson=datafromjson, itemid=itemid, textfile=textfile) if __name__ == '__main__': app.run(debug=True)