{"id":19261997,"url":"https://github.com/hacksu/flask-api","last_synced_at":"2026-02-04T15:04:45.464Z","repository":{"id":149141382,"uuid":"71171672","full_name":"hacksu/Flask-API","owner":"hacksu","description":"A toy API built using flask to demonstrate using it for that purpose","archived":false,"fork":false,"pushed_at":"2016-10-19T00:40:11.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-06-26T09:04:54.552Z","etag":null,"topics":["advanced","lesson"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hacksu.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-17T19:05:14.000Z","updated_at":"2020-02-23T21:48:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"238ef07a-fe93-47e1-9a5b-9a11094dc1b8","html_url":"https://github.com/hacksu/Flask-API","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hacksu/Flask-API","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2FFlask-API","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2FFlask-API/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2FFlask-API/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2FFlask-API/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hacksu","download_url":"https://codeload.github.com/hacksu/Flask-API/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2FFlask-API/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29088449,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-04T03:31:03.593Z","status":"ssl_error","status_checked_at":"2026-02-04T03:29:50.742Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["advanced","lesson"],"created_at":"2024-11-09T19:29:19.129Z","updated_at":"2026-02-04T15:04:45.449Z","avatar_url":"https://github.com/hacksu.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flask\n\nFlask is a micro-framework for Python which allows you to progromatically control what gets returned from an http server.\nIt's meant to be easy to use and in my opinion it is.\n\n## Installation\n\nInstall python from https://www.python.org/ (the first thing that comes up when you Google for Python). The version shouldn't matter.\nCheck that you have it installed by running `python -V`\nInstall `flask` by typing `pip install flask`\n\nThen make a new directory and open it in your editor of choice.\n\n## Steps\n\n### Hello Internet\n\nMake a app.py file and put the following basically copied from Flask's main page in it\n\n        from flask import Flask\n        app = Flask(__name__)\n\n        @app.route(\"/\")\n        def hello():\n            return \"Hello Internet!\"\n\n        if __name__ == \"__main__\":\n            app.run(debug=True)\n\nLets go over this line by line and see what's happening\n\n`from flask import Flask` import the `Flask` thing from the `flask` package\n`app = Flask(__name__)` make a new instance of Flask and pass it the name of the current scope (not quite clear on what that is either)\n`@app.route(\"/\")` registers a route listening for anyone going to `/` on the webserver\n\n        def hello():\n            return \"Hello Internet!\"\n\nis the function that will handle the request, here it just returns \"Hello Internet!\" \n\nFinally \n\n        if __name__ == \"__main__\":\n            app.run(debug=True)\n\nstarts the webserver. Note this configuration is not something you want to use in production. Not only is it slow, but because we've set the debug flag\nit's insecure.\n\nTo run it type `python app.py`\n\nYou should see\n\n        * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n        * Restarting with stat\n        * Debugger is active!\n        * Debugger pin code: 270-413-265\n\nor similar.\n\n\nGo to http://127.0.0.1:5000/ to see our site.\n\n### Change the message\n\nChange \n\n        def hello():\n            return \"Hello Internet!\"\n\nto return something else. Save the file and reload in the browser. The message in your browser should say whatever you put there.\n\n### Add another route.\n\nHow do you think you'd add another route?\n\nIt turns out to be about what you'd guess\n\n        @app.route(\"/bye/\")\n        def bye():\n            return \"Bye folks\"\n\nGo to `http://localhost:5000/bye/` to see the message\n\nNote that there is a difference between\n\n@app.route(\"/bye/\") and @app.route(\"/bye\") the former will redirect people to the version with the trailing slash if they go to just /bye where as\nthe later will just match for /bye. It doesn't matter which you use, but you should know there is a difference.\n\n### JSON\n\nUp to now we've just returned a HTML, To be honest you may not have realized it was HTML it certainly wasn't properly formatted HTML,\nbut to your browser and critically anything else making http requests it was HTML. \n\nJSON is a bit different, it stands for JavaScript Object Notation and is a text based way of representing a JavaScript class or really any class. It servers a similar purpose to\nXML, but to me is much easier to read and write. \n\n\nAnyway, enough explenation, that all sounds good, how do we actually send it.\n\nAgain flask makes it easy. We just need to make two changes. First add a `from flask import Jsonify` to the top of the file then inside a route replace the return with\n`jsonify({\"message\": \"Hello Internet!\"})`\n\n### Practical Problem\n\nEverytime I talked about doing a survey, sami would say I really should make a world cloud of topic suggestions. My Googling skills weren't up to snuff though\nand I couldn't find a site to do it. We can make one though. Or at least the backend of one. We'll need a couple of routes.\n\nIn partical we need a way of voting for a particlar topic and then a way of listing the topics people have voted on. Listing the topics is pretty easy\nLets just add a route like\n\n        @app.route(\"/topics/\")\n        def topics():\n            return jsonify({\n                \"topics\": {\n                    \"json\": 4,\n                    \"jokes\": 1,\n                    \"Mobile\": 10\n                }\n            })\n\n### Make it Remember\n\nRight now it always returns the same words. Lets give it the potential to change by adding\n\n     topics = {}\n\nAs a global variable and changing to\n\n        @app.route(\"/topics/\")\n        def get_topics():\n            return jsonify({\n                \"topics\": topics\n            })\n\n\n### Modify the Topic\n\nRight now we don't have any way to modify the topics. Lets add a route\n\n        @app.route(\"/topics/\u003ctopic\u003e/\")\n        def suggest(topic):\n            if topic in topics:\n                topics[topic] += 1\n            else:\n                topics[topic] = 1\n            return jsonify({\n                \"topics\": topics\n            })\n\nThe `\u003c` surround a wild card that anything can be put in for. Here we put in a topic\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhacksu%2Fflask-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhacksu%2Fflask-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhacksu%2Fflask-api/lists"}