flask website for la petite ecole
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

109 lines
3.4 KiB

from flask import Flask
from flask import render_template, request , send_from_directory
from flask_flatpages import FlatPages
import markdown2, os
import random
from pathlib import Path
app = Flask(__name__)
application = app
FLATPAGES_EXTENSION = '.md'
FLATPAGES_MARKDOWN_EXTENSIONS = ['extra']
FLATPAGES_AUTO_RELOAD = True
app.config['FLATPAGES_ROOT'] = 'pages'
app.config['APPLICATION_ROOT'] = '/lpe'
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
app.config.from_object(__name__)
pages = FlatPages(app)
pages.get('foo')
def Liste_cat():
articles = (p for p in pages if 'date' in p.meta and 'type' in p.meta and p.meta['type']=='blog')
catList = []
for a in articles:
catList.append(a.meta['category'])
catList = list(dict.fromkeys(catList))
return catList
def get_article_images(article_path):
"""Helper function to get images for a given article path."""
article_folder = os.path.join(app.config['FLATPAGES_ROOT'], article_path)
images = []
if os.path.isdir(article_folder):
images = [img for img in os.listdir(article_folder) if img.endswith(('.jpg', '.jpeg', '.png', '.gif'))]
return images
@app.route('/')
def index():
pictoPath = os.listdir('./static/img/shape/')
pictoR = random.choice(pictoPath)
page = pages.get_or_404('home')
return render_template('home.html' , page=page , picto=pictoR)
@app.route('/home/')
def home():
pictoPath = os.listdir('./static/img/shape/')
pictoR = random.choice(pictoPath)
page = pages.get_or_404('home')
return render_template('home.html' , page=page , picto=pictoR)
@app.route('/<path:path>/')
def page_static(path):
pictoPath = os.listdir('./static/img/shape/')
pictoR = random.choice(pictoPath)
page = pages.get_or_404(path)
return render_template('page.html' , page=page , picto=pictoR)
@app.route('/vitrine/')
def blog():
pictoPath = os.listdir('./static/img/shape/')
pictoR = random.choice(pictoPath)
articles = (p for p in pages if 'date' in p.meta and 'category' in p.meta and p.meta['category']=='vitrine')
latest = sorted(articles, reverse=True,key=lambda p: p.meta['date'])
# catList = Liste_cat()
return render_template('flux_No_img.html', articles=latest , picto=pictoR)
@app.route('/actu/')
def actu():
pictoPath = os.listdir('./static/img/shape/')
pictoR = random.choice(pictoPath)
articles = (p for p in pages if 'date' in p.meta and 'category' in p.meta and p.meta['category']=='actu')
articles_gal= []
for article in articles:
images = get_article_images(article.path)
articles_gal.append({
'article': article,
'images': images
})
latest = sorted(articles_gal, reverse=True, key=lambda p: p['article'].meta['date'])
# catList = Liste_cat()
print(articles_gal)
return render_template('flux_img.html', articles=latest , picto=pictoR )
@app.route('/images/<path:filepath>')
def image(filepath):
"""Serve images from the pages folder dynamically."""
try:
# Use send_from_directory to serve the image
print(f"Serving image from: {filepath}")
return send_from_directory(app.config['FLATPAGES_ROOT'], filepath)
except FileNotFoundError:
# Return a 404 error if the file is not found
return 'File not found', 404
if __name__ == '__main__':
app.run(host='0.0.0.0')