Render different static folder based on url request - static

Using Flask, I can render different templates based on the url. For example :
http://example.com/site1/ ; will render templates/site1/{htmls}
http://example.com/site2/ ; will render templates/site2/{htmls}
This works really great with jinja_loader and a custom Loader, but now I'm stuck with the static files.
The static files depends on the template, so they are located in templates/static/site{0-9}, but of course, I cannot set a jinja_loader on the static_folder parameter because it's not related to Jinja but to Flask.
How can I render the correct static folder based on the current URL?
As an example, here's the loaded code:
Flask(app_name,
static_url_path = '/public',
static_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates/static')
)
and in templates/static, I have :
static/
site1/
css/
js/
images/
site2/
css/
js/
images/
etc...

You either have to use explicit paths in your static views:
url_for('static', filename='site1/css/...')
where site1 could be taken from request.path.split('/', 1)[0]:
url_for('static', filename='{}/css/...'.format(request.path.split('/', 1)[0]))
You could create a custom static view:
from flask import request, send_from_directory, current_app, safe_join
import os.path
#app.route('/<site>/static/<path:filename>')
def per_site_static(site, filename):
if site is None:
# pick site from the URL; only works if there is a `/site/` first element.
site = request.path.split('/')[0]
static_folder = safe_join(current_app.static_folder, site)
cache_timeout = current_app.get_send_file_max_age(filename)
return send_from_directory(static_folder, filename,
cache_timeout=cache_timeout)
Then use url_for() to generate urls:
{{ url_for('per_site_static', site=None, filename='css/...') }}

An other interesting alternative is the following :
class GenericStaticFlask(Flask):
def send_static_file(self, filename):
# g.site contains the name of the template path for siteX
return super(GenericStaticFlask, self).send_static_file("%s/%s" % (g.site, filename))
app = GenericStaticFlask(app_name,
static_url_path = '/public',
static_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates/static')
)

Related

how to fetch the frontmatter of any hugo markdown page (not just _index.md)

I'm trying to write a shortcode for my hugo site that fetches the title parameter of page.
I have a directory structure like this:
content
├── workshops
│   ├── foo
│   │   └── _index.md
│   ├── bar.md
This works perfectly:
{{ with .Site.GetPage "home" "workshops/foo"}}
{{ .Params.Title }}
{{ end }}
And this one consistently comes up blank (even though there is a title in the markdown).
{{ with .Site.GetPage "home" "workshops/bar"}}
{{ .Params.Title }}
{{ end }}
My question is: How so I get the title of a standalone page?
I've tried a bunch of different combinations of things and I'm just not coming right. I've tried reading the docs and they are horribly convoluted on this point.
I have a solution! I wrote a little Python3.7 script to make directories and move and rename markdown files and just ran it over my entire contents directory. This solved my problem but is a bit of a hack...
import logging
import os
from pathlib import Path
def fixup(path):
location = Path(path)
assert location.is_dir(), location
for child in location.iterdir():
if child.is_dir():
fixup(child)
else:
fix_file(child)
def fix_file(file_path):
name = file_path.name
if not name.endswith(".md"):
# we only care about markdown files.
return
check_metadata(file_path)
if name.startswith("_index."):
# looks good
return
# make a directory with the same name as the file (without the extension)
suffix = ''.join(file_path.suffixes)
prefix = name[: -len(suffix)]
new_dir = file_path.parent / prefix
new_dir.mkdir()
new_path = new_dir / f"_index{suffix}"
file_path.rename(new_path)
def check_metadata(file_path):
""" given the path to a markdown file, make sure that the frontmatter includes
the required metadata
"""
# TODO
# required = ['title']
# allowed = ['pre', 'weight', 'ready']
if __name__ == '__main__':
fixup('content')
Two differences:
Use the global site variable
Just pass the page name as the argument
{{ with site.GetPage "workshops/bar" }}
{{ .Title }}
{{ end }}

How to add static files link on index.html in Vue 2.5?

I tried with following explanation in vue 2.5 it does not work. But in version vue 2.4.2 working fine.
"Real" Static Assets
In comparison, files in static/ are not processed by Webpack at all: they are directly copied to their final destination as-is, with the same filename. You must reference these files using absolute paths, which is determined by joining build.assetsPublicPath and build.assetsSubDirectory in config.js.
As an example, with the following default values:
// config/index.js
module.exports = {
// ...
build: {
assetsPublicPath: '/',
assetsSubDirectory: 'static'
}
}
Any file placed in static/ should be referenced using the absolute URL /static/[filename]. If you change assetSubDirectory to assets, then these URLs will need to be changed to /assets/[filename].
Any suggestion for vue 2.5.2 ?

Unittest Jinja2 and Webapp2 : template not found

I use Jinja2 with Webapp2 on a GAE project.
I have a base RequestHandler as describe in webapp2_extras.jinja2:
import webapp2
from webapp2_extras import jinja2
def jinja2_factory(app):
"""Set configuration environment for Jinja."""
config = {my config...}
j = jinja2.Jinja2(app, config=config)
return j
class BaseHandler(webapp2.RequestHandler):
#webapp2.cached_property
def jinja2(self):
# Returns a Jinja2 renderer cached in the app registry.
return jinja2.get_jinja2(factory=jinja2_factory, app=self.app)
def render_response(self, _template, **context):
# Renders a template and writes the result to the response.
rv = self.jinja2.render_template(_template, **context)
self.response.write(rv)
And a view handler as:
class MyHandler(BaseHandler):
def get(self):
context = {'message': 'Hello, world!'}
self.render_response('my_template.html', **context)
My templates are in the default location (templates).
The app works well on dev server, and the template is correctly rendered.
But when I try to unittest MyHandler with
import unittest
import webapp2
import webstest
class MyHandlerTest(unittest.TestCase):
def setUp(self):
application = webapp2.WSGIApplication([('/', MyHandler)])
self.testapp = webtest.TestApp(application)
def test_response(self):
response = application.get_response('/')
...
application.get_response('/my-view') raise an exception: TemplateNotFound: my_template.html.
Is there something I missed? Like a jinja2 environment or template loader configuration?
Problem origin:
Jinja2 default loader searches files in a relative ./templates/ directory. When you run your GAE application on the development server this path is relative to the root of your application. But when you run your unittests this path is relative to your unittest files.
Solution:
Not really an ideal solution, but here a trick I did to solve my problem.
I updated the jinja2 factory to add a dynamic template path, set in app config:
def jinja2_factory(app):
"""Set configuration environment for Jinja."""
config = {'template_path': app.config.get('templates_path', 'templates'),}
j = jinja2.Jinja2(app, config=config)
return j
And I set an absolute path to the templates in the setUp of my unittests:
class MyHandlerTest(unittest.TestCase):
def setUp(self):
# Set template path for loader
start = os.path.dirname(__file__)
rel_path = os.path.join(start, '../../templates') # Path to my template
abs_path = os.path.realpath(rel_path)
application.config.update({'templates_path': abs_path})

What's the best practice to use named Route in AppEngine?

In the app.yaml file, I have put 2 lines to specify the url mapping:
url: /blog/.*
script: blog.app
url: /
script: home.app
the problem is I can't use the "uri_for" function to generate a url for blog module in home.py, case there's no Route added in home moudle:
here is the code in home module:
app = webapp2.WSGIApplication([
webapp2.Route(r'/', handler=HomeHandler, name='home')
], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)
and code in blog.py:
app = webapp2.WSGIApplication([
webapp2.Route(r'/blog/<blog_id:\d+>', handler=BlogHandler, name="blog")
], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)
so, if I have code like this: {{ uri_for('blog', blog_id=blabla) }} in home.html, it can't work.
You should consolidate those routes into one app.
app = webapp2.WSGIApplication([
webapp2.Route(r'/', handler=HomeHandler, name='home'),
webapp2.Route(r'/blog/<blog_id:\d+>', handler=BlogHandler, name="blog")
], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)
and actually those are only the view blog post routes.
If you wanted to do a full CRUD app, you might need to add some more.
app = webapp2.WSGIApplication([
webapp2.Route(r'/admin/blog', handler='admin.AdminBlogHandler:list, name="admin.blog.list"),
webapp2.Route(r'/admin/blog/new', handler='admin.AdminBlogHandler:new', name='admin.blog.edit'),
webapp2.Route(r'/admin/blog/<id:[^/]+>/edit', handler='admin.AdminBlogHandler:edit', name='admin.blog.edit'),
webapp2.Route(r'/admin/blog/<id:[^/]+>', handler='admin.AdminBlogHandler:view', name='admin.blog.view')
], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)
Note for these examples:
1) you prefix a name to load the handlers from a different file (admin.AdminBlogHandler will look in 'admin.py' for 'class AdminBlogHandler'
2) you specify the method to run after the handler name, after the colon.
3) in each method I am creating functionality for get and post, so there are not discrete RESTful URLs for edit and update.

Catch All Script in App Engine Python (APP.YAML) does not work in Static Files

I have tried everything but it seems that you cannot get a catch all url...
- url: /.*
script: not_found.py
...to work on urls that are based on static directory paths. eg. I can type in www.foobar.com/asdas/asd/asd/asd/ad/sa/das/d and I can get a nice custom 404 page. But if I alter a static path url like www.foobar.com/mydir/mydir/mypage.html, I just get the horrible generic 404....
Error: Not Found
The requested URL /mydir/mydir/mypage.html was not found on this server.
... I would like to alter whatever catches the url in directory paths and writes the 404. This appears the only way to get a consistent custom 404 page in GAE Python.
Can anyone help? I have written my website from scratch and have a very limited knowledge of Python. Achieving a consistent custom 404 is the only thing I cannot seem to overcome.
EDIT/ADD : OK I've added the kind suggestion of #Lipis , and gone through getting started which which thankfully has given me a much better understanding of classes (I sadly can't vote it up yet). But! I am using a .py script found on the net and I think the NotFound class is interfering with the class that gives my index page, because now my index page is the 404 page specified by the Jinja! I have very little understanding of MainHandler so I may have to give up for now.
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import jinja2
class MainHandler(webapp.RequestHandler):
def get (self, q):
if q is None:
q = 'index.html'
path = os.path.join (os.path.dirname (__file__), q)
self.response.headers ['Content-Type'] = 'text/html'
self.response.out.write (template.render (path, {}))
class NotFound(webapp.RequestHandler):
def post(self):
# you need to create the not_found.html file
# check Using Templates from Getting Started for more
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
template = jinja_environment.get_template('404.html')
self.response.out.write(template.render(template_values))
def main ():
application = webapp.WSGIApplication ([('/(.*html)?', MainHandler),('/.*', NotFound)],
debug=True)
util.run_wsgi_app (application)
if __name__ == '__main__':
main ()
For better understanding I'll make some modifications on the Getting Started example which I assume that you have done it and you made some experiments with it.
It's not a good idea to have the static file for all the not found pages in the app.yaml since most likely you would like to show something more dynamic and usually the - url: /.* should be handled within your app.
In this example we are going to add a new RequestHandler for all your not found pages
import jinja2
import os
# more imports
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
class NotFound(webapp.RequestHandler):
def get(self):
# you need to create the not_found.html file
# check Using Templates from Getting Started for more
template = jinja_environment.get_template('not_found.html')
self.response.out.write(template.render(template_values))
application = webapp.WSGIApplication(
[('/', MainPage),
('/.*', NotFound)], # <-- This line is important
debug=True)
But in order to make the jinja2 templates work, follow carefully the modifications that you need to do in Using Templates section from the Getting Started.
The order in the URL mapping is very important so this catch all regular expression (/.*) should be always the last one, because otherwise all the other rules will be skipped.
If you want to catch all URLs, you will have to modify your main request handler in your file "not_found.py" by adding '/.*'.
For example, you can set the file "not_found.py" to:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.out.write("Hello, MAIN!")
application = webapp.WSGIApplication(
[('/.*', MainHandler)], # <--- Add '/.*' here
debug=True)
def main():
run_wsgi_app(application)
If you navigate to www.foobar.com/asd/ad/sa/das/d or any other URL, you will see the message "Hello, MAIN!.
Hope it helps. Ask question if needed

Resources