Please allow me to ask my question using an example where we have a database with flight data and I want to make a web app where users can search, select, and book a flight.

I wrote some classes such as SelectedFlight, Ticket, and Passenger. For example, the SelectedFlight is supposed to represent the flight that the user selects from the list of available flights. The SelectedFlight has a get_flight_data() method which queries data from the database to create the SelectedFlight object instance.

I now need to make a web app using Python Flask (which is a framework that basically lets you map URLs to Python functions).

I (think I) have two options.

(1) I create a script with some Flask functions. The user types in a URL, and the function mapped to that URL is called. The function instantiates my backend objects and returns HTML.

(2) I create a script (i.e. API) with Flask functions but instead of returning HTML, the functions will return JSON. Then I create another script with Flask functions that perform requests on the JSON URLs and render HTML for the web app.

Which of the options would you use in the example I mentioned and why?

  • 1
    The right question is which one you need? Our preferences might or might not fulfil your needs. Right now, the question is mainly opinion based, hence off-topic – Laiv Nov 18 '18 at 15:44
  • Popular drive seems to be to use phone apps to do things you do on a desktop or laptop via the browser. Hence, I'll bet a donut that the next person you see about this IRL will ask "what about a phone app". And that means two apps at least really. So for your use case, I'd do a REST-ish API set up and send JSON back to requesting client. – ivanivan Nov 18 '18 at 19:19

The use of an API a majority of times is when two disparate applications need to communicate (it's short for Application Program Interface). So this will depend entirely on your application's design pattern.

First consideration is will you ever vary your front end platform, which @ivanivan mentions in his comment. If you ever want to do a mobile application, or any separate application that needs to consume this data, you'll want an API for sure. If you only intend on making a web app, an API is still a possibility.

Second, if the first didn't answer your question, consider whether you want to do one application (the server does all the work and just send HTML to the client), or if you want two (or more) applications (the front end is it's own application and the server is providing data to that application). This is a common difference between synchronous (each change on the web page is a whole new request/page) and asynchronous (each change on the website might be a small API call to just get/update data). With your specific use case, a static web application can work just fine. However, your feature set or design ideas might evolve beyond that. It is a bit more work however, as your logic gets split across the API and the front end.

Whichever is best for you is about what features you want and how much work you want to put in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.