Flask-JSGlue helps hook up your Flask application nicely with the front end.
Tested on Python 2.7, 3.3, 3.4, 3.5, 3.6, and PyPy2/3.
Flask-JSGlue helps hook up your Flask application nicely with the front end.
Flask-JSGlue helps hook up your Flask application nicely with the front end.
Tested on Python 2.7, 3.3, 3.4, 3.5, 3.6, and PyPy2/3.
When using ProxyPass on an apache sever to redirect to an internal server and using ReverseProxied, JSGlue doesn't acknowledge the use of a different URL. How do I fix this? for example
actual url is example.com/myapp/resource
JSGLUE url is example.com/resource
I need it to understand that its behind the /myapp
I'm trying to do this within my imported javascript and it does read it upon page load
var x = {{x.title}}
So javascript must be embedded inside the jinja tempalte itself?
If I'm building a web application using a Flask back-end but a Vue front-end instead of one based off the Jinja2 templating system, but still want to utilize Flask's url_for
function in the front-end, what's the best way to do that?
Flask's url_for
function encodes input to be URL safe. For example, url_for('root', someparam='5 & 6 & more')
gives back /?someparam=5+%26+6+%26+more
.
JSGlue doesn't escape the input at all. Flask.url_for("root", {"someparam": "5 & 6 & more"})
gives back /?someparam=5 & 6 & more
.
Here's a fully working demo script:
import flask
from flask_jsglue import JSGlue
app = flask.Flask(__name__)
jsglue = JSGlue(app)
app.debug = True
home_template = u'''
<head>
{{ JSGlue.include() }}
</head>
<body>
Flask url: <code>{{ url_for('root', someparam='5 & 6 & more') }}</code>
<br/>
JSGlue url: <code id="jsglueurl"></code>
<script>
var jsglueUrl = Flask.url_for("root", {"someparam": "5 & 6 & more"});
document.getElementById("jsglueurl").innerHTML = jsglueUrl;
</script>
</body>
'''
@app.route("/")
def root():
return flask.render_template_string(home_template)
if __name__ == "__main__":
app.run()
You'll see the results on the page.
My Flask application is deployed at a BASE_URL so that links to endpoints look like:
http://my_server/BASE_URL/endpoint/...
All URLs for Flask endpoints need to start with that BASE_URL. I find that links generated on the server side using {{ url_for(endpoint) }} work correctly, but links generated in Javascript on the client using Flask.url_for(endpoint) come out as
http://my_server/endpoint/...
with the application BASE_URL portion missing.
When viewed in a debugger both the flask server-side code and the jsglue client code seem to produce the same thing: "/endpoint/...", but the final URLs come out differently.
This may be user error, in which case I'd really appreciate some advice about how to solve it, or it may be a problem with URLs produced by jsglue.
Thanks for all your work with js_glue, it has been very helpful.
I am currently trying to get an absolute url for one of my routes. I understand that this can be done by passing _external=True to the url_for function in Flask. Is this possible to do with js_glue?
I have tried the follow:
Flask.url_for('fn_name', { '_external': 'True' })
-- string of Python true.
Flask.url_for('fn_name', { '_external': true })
-- javascript true
Neither of them have managed to get the absolute url for the route.
Would you be able to advise if this is possible with js_glue, and if so, how do I get the absolute url?
Thanks in advance.
Maybe put it into a template that can be overwritten.
serving flask app on uwsgi and nginx for static files does not enable jsglue to work properly...
any reason why jsglue isn't being found?
is jsglue only good for development and not for production use?
An optional integration with flask-cache could do some wonders here.
To make flask-jsglue SPA friendly, it would be nice to be able to generate the js file to disk with a command. Otherwise, it is quite difficult to import glue.js if the file is served from another domain.
New release please?
I installed Flask-JSGlue2 through pip and I get the following error:
GET https://domain.com/jsglue.js net::ERR_ABORTED 500 (INTERNAL SERVER ERROR)
When I click it it takes me to the script tag which is the following:
<script src="/jsglue.js" type="text/javascript"></script>
I tried changing the JSGLUE_JS_PATH constant but it didn't help.
When external is provided, JsGlue.url_for should use SERVER_NAME to build the path, not location, just like Flask.url_for. This behavior is quite useful for SPA websites.
I have a project that is using Quart as backend instead of Flask. Although you can patch Flask in Quart for external libraries, there is an issue when make_response directive is used.
I adapted your flask_jsglue.py to use Quart instead but it would be nice to have that option in your library.
Thank you.
Hello,
With new version of Jinja2 the Python modules Markup and Escape should be imported from MarkupSafe (https://jinja.palletsprojects.com/en/3.1.x/changes/#version-3-1-1)
Actually it raise an exception :
File "/var/www/site_mbt/flask-app-v2/src/__init__.py", line 11, in <module>
from flask_jsglue import JSGlue
File "/var/www/site_mbt/flask-app-v2/venv/lib/python3.8/site-packages/flask_jsglue.py", line 2, in <module>
from jinja2 import Markup
ImportError: cannot import name 'Markup' from 'jinja2' (/var/www/site_mbt/flask-app-v2/venv/lib/python3.8/site-packages/jinja2/__init__.py)
Thank you for your great job.
I'm trying to do pagination with jsglue. how do i pass the parameters to a get request in my flask.url_for statement? inside the template?
I found the module need a javascript file called jsglue.js
, but where should I get that file?
Let's say I have a flask app running and the root URL is http://www.example.com/app
JSGlue.include()
will still point to http://www.example.com/jsglue.js and thus will not work
Code on PyPi claims to be version 0.3.1, however the application root code is not implemented in that version.
Something to consider:
This functionality is built into werkzeug, however it is depreciated:
https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/contrib/jsrouting.py
See:
http://werkzeug.pocoo.org/docs/0.10/transition/#stop-using-deprecated-things
I was looking into using this but ended up writing a separate url helper to get around using back/front url_for
for consistency because it was a simpler option.
Regardless, it seems like the lack of an inclusion/exclusion criteria config for url rules here is a potential (minor) security hazard, because it currently outputs the entire ruleset into jsglue.js
. It's minor in the sense that we assume your URLs are already secure from unauthorized attacks, but not so minor in that you reveal information that should be obscure by design :-)
Have you considered e.g. reading a config parameter like a regex that specifies inc/exc rules? These would be either a single regex string, or a list/tuple of strings (for exact matches) or regex strings.
Briefly, if an inclusion rule is given, all rules are excluded by default, except for those matching the rule. If exclusion given, all are included by default except for those matching. If both given, all are excluded, except those matching inclusion, with exclusion rules would act as subfilters.
I don't know if I'll come back to this library but if I do, and this is still open, I will add this feature. Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.