git clone https://github.com/harryparkdotio/flask-singleview.git
in app.py
from flask_singleview import singleviewsingleview =
from flask import Flask, render_template, jsonify, send_from_directory# only necessary if you are using socketiofrom flask_socketio import SocketIOfrom flask_singleview import singleviewimport requests, json, base64app =app.threaded = True# if you want to use socketiosocketio =singleview =# if you just want to use AJAXsingleview =# you can specify the base template by: index.html is the default templatesingleview =# routes#######################################################returnreturn#######################################################...#######################################################if __name__ == "__main__":# if you want to use socketio# if you just want to use AJAX
note, this is almost identical to that of the
app.py
file
values =return
Adding a variable in the url still works.
return
But declaring the variable type doesn't.
BREAKS THINGS
# nope --> `int:`return
And multiple routes on the same function doesn't work either. You'll just get errors
return
But standard options for routes do work!
return
disabling socketio (preventing unnecessary calls)
template_vars =return
disabling content preload (preventing double render)
template_vars =return
The default template which everything is inserted into is index.html
.
This is how flask singleview works as a basic overview):
if accessed_by == 'socketio':returnelif accessed_by == 'flask_route':preload_content =return
this is how the index template is setup
<!-- links; not required -->home123<!-- content; leave it EXACTLY like this -->{% if preload_content is defined %}{% autoescape false %}{{ preload_content }}{% endautoescape %}{% endif %}<script>{% if content_method == 'ajax' %}<script>{% elif content_method == 'socketio' %}<script><script>{% endif %}
note it doesn't matter how your custom views are rendering, it'll just pop it into the
div
with the idsingleview-content
So, by now you must be wondering how to actually reference and link between pages.
<!-- goes to index -->home
url_for()
also works
<!-- goes to the url of route_1 -->1<!-- note the two leading hashtags -->
Just reference all internal links with ##
before. This way, js can capture it.
You can decide whether or not you want to omit the leading /
from your href
attributes, singleview_flask takes it into account either way.
##hello
→ example.com/hello
##/hello
→ example.com/hello
##hello/name
→ example.com/hello/name
##
?Good question, it's still fairly standard when it comes to a URL, meaning that it isn't going to chuck a tantrum if the js doesn't load properly, it may not work, but it won't scream at you.
Firstly, you'll need to create a folder named static
, where you can pop all the necessary files, like css, js, and images. Think of it like your assets folder.
You'll need to download ajax.js
and socketio.js
from the data/scripts
folder of this repo (here is a link), and put it into the static folder in the root directory.
You'll also need a templates
folder in your root directory, containing the templates your project will use. It's a good idea to check out the template in the data/templates
folder of this repo (here is a link) to get a basis from, or to use.
Otherwise, here is what you'll need to add to the bottom of your webpage to load the required javascript correctly.
{% if content_method == 'ajax' %}<script>{% elif content_method == 'socketio' %}<script><script>{% endif %}
If you are confused, please check out the example, it might make a bit more sense.
Double render, think of it like render_template()
inception, because thats exactly what it is.
render_template(render_template())
← general gist of it
to prevent this (it shouldn't occur, unless you're doing weird shit. Let me know if you are though, I'd be interested in hearing about it)
add route_exclude=True
as a param of the @app.route()
decorator.
return
If you are choosing to go the AJAX route (pun intended), then you really, really, really, don't want to route anything to the route /page
. This is due to the fact that flask_singleview is using it to send pages to the client.
this won't allow for preloading, it will still allow the user to access it directly, it just won't show anything but the standard index.html
page.
return
Basically the same thing as above, but opposite, only shows content if accessed directly, not through AJAX or socketio (clicked an internal link).
return
Now this one is a tad odd, and useful if you just want to return the function as normal. This prevents double render, a quirk.
return