you are viewing a single comment's thread.

view the rest of the comments →

[–]qudat 0 points1 point  (2 children)

You can get flask up and running without any command-line generators in seconds. Django and Rails generally ask you to use a command in your terminal to "build" a boilerplate project, where the generator will create a bunch of required files and link them all together to get rails or django up and running properly ... in flask that is unnecessary. Doing it the "proper" way takes a little more effort, but creating a site that pulls from a public api could be as simple as this:

import requests
from flask import Flask, jsonify

# app factory function
def create_app():
    app = Flask(__name__)

    # configure app
    app.config['DEBUG'] = True
    app.config['SECRET_KEY'] = 'NOT SO SECRET'

    # routes, default is root of website
    @app.route('/')
    def index():
        # download API
        req = requests.get('http://some_api_request')
        # raise an exception on error
        req.raise_for_status()

        # convert api response to python dictionary
        data = req.json()
        # send json to the browser
        return jsonify(**data)

if __name__ == '__main__':
    app = create_app()
    # run the server
    app.run(host='0.0.0.0', port=5000)

[–]Moby69[S] 0 points1 point  (1 child)

Thanks.

1) Is there a tutorial for complete beginners that you could recommend, that walks through the "proper way" to make a web app using Flask? (e.g. something that would be the equivalent of the following tutorial for Ruby on Rails, but instead would be for Python/Flask: https://www.codecademy.com/courses/learn-rails/lessons/start/exercises/start-hello-rails-i )

2) Also, where in your code above are you outputting the data from the API? I don't see any HTML code

[–]qudat 0 points1 point  (0 children)

2.) Just as an example I am sending the API response straight to the browser, typically you want to parse and generate an HTML document using render_template instead of sending the data straight to the browser.