How can I serve NPM packages using Flask?

Go to your static folder and there initialize your npm project. cd flask_app/static $ npm init after installing and saving npm packages you can serve them like this: <script src=”https://stackoverflow.com/questions/24514936/{{ url_for(“static’, filename=”node_modules/toastr/toastr.js”)}}”></script> credits to: https://codeburst.io/creating-a-full-stack-web-application-with-python-npm-webpack-and-react-8925800503d9

Flask confusion with app

I think the main confusion is in the line: from app import app You have a python package (a folder with __init__.py file) named “app”. From this folder, you are now importing the variable “app” that you defined below in __init__.py file: app = Flask(__name__) Rename the folder from app to say “myproject”. Then you …

Read more

How to upload a file using an ajax call in flask

To answer your question… HTML: <form id=”upload-file” method=”post” enctype=”multipart/form-data”> <fieldset> <label for=”file”>Select a file</label> <input name=”file” type=”file”> </fieldset> <fieldset> <button id=”upload-file-btn” type=”button”>Upload</button> </fieldset> </form> JavaScript: $(function() { $(‘#upload-file-btn’).click(function() { var form_data = new FormData($(‘#upload-file’)[0]); $.ajax({ type: ‘POST’, url: ‘/uploadajax’, data: form_data, contentType: false, cache: false, processData: false, success: function(data) { console.log(‘Success!’); }, }); }); }); …

Read more

Flask request.remote_addr is wrong on webfaction and not showing real user IP

If there is a proxy in front of Flask, then something like this will get the real IP in Flask: if request.headers.getlist(“X-Forwarded-For”): ip = request.headers.getlist(“X-Forwarded-For”)[0] else: ip = request.remote_addr Update: Very good point mentioned by Eli in his comment. There could be some security issues if you just simply use this. Read Eli’s post to …

Read more