0

I have a website, mainly built with HTML/CSS/JavaScript. There are a few places where I'd like to use a python library, or just write in python, and then create specific functions with Flask.

Basically, I have functions in Python/Flask that return JSON, which I'd like to call from JavaScript. How can I deploy Flask if I don't want my website to be an entirely Flask-based thing? If I want to write one or two files here and there with Python/Flask but the rest in JavaScript, how do I deploy it? What I've found online seems to be a solution to deploying Flask as a solution for the whole site, not just for a few individual scripts.

If it helps at all, I could have all my Flask apps in one directory.

1 Answer 1

1

There's nothing wrong with using Flask to power an API that is consumed by a static site - the simplest way to do what you are looking to do is to simply mount your WSGI application at some sub-path that Flask will handle (for example /api) and then have your JavaScript / form submissions / etc. just point at the appropriate endpoints under /api.

In general, you can use the same server to serve both the static content and your WSGI application, but there's no need for them to be the same. All of your static content might be served out of a CDN, with your API living on a single machine somewhere on AWS (for example).

Sign up to request clarification or add additional context in comments.

2 Comments

This sounds good. If I run flask on port 80 and my other stuff at port 80, there won't be any conflicts as long as all my flask apps are @app.route("/api/somepage")? Do you have links to any tutorials that will help me execute this?
You actually will run whatever is hosting Flask on port 80 and have the hosting httpd (Apache, nginx, etc.) serve the static files. Here's an example for using Apache to serve the static files while having Flask + mod_wsgi handle the majority of the site. You would just change the mount point for mod_wsgi.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.