{"id":17354694,"url":"https://github.com/openmohan/python-api-server","last_synced_at":"2025-03-27T13:49:05.240Z","repository":{"id":166769883,"uuid":"122725898","full_name":"openmohan/Python-API-Server","owner":"openmohan","description":"Simple Python API server using flask ","archived":false,"fork":false,"pushed_at":"2018-03-03T14:25:09.000Z","size":1273,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T18:16:12.886Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","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/openmohan.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":"2018-02-24T09:36:51.000Z","updated_at":"2018-02-24T10:01:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"c604020d-0d26-41ac-a9ee-eba20468a992","html_url":"https://github.com/openmohan/Python-API-Server","commit_stats":null,"previous_names":["openmohan/python-api-server"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openmohan%2FPython-API-Server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openmohan%2FPython-API-Server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openmohan%2FPython-API-Server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/openmohan%2FPython-API-Server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/openmohan","download_url":"https://codeload.github.com/openmohan/Python-API-Server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245858878,"owners_count":20684057,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-10-15T17:37:45.045Z","updated_at":"2025-03-27T13:49:05.224Z","avatar_url":"https://github.com/openmohan.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n### tl;dr \n\nA walkthrough on how to get set up with Heroku and its toolkit and then how to deploy a simple web application (for free) on the Heroku Cloud. \n\nTo see a more advanced Flask app that uses USGS data and Google APIs, checkout this repo: [datademofun/heroku-flask-quakes-lesssimple](https://github.com/datademofun/heroku-flask-quakes-lesssimple)\n\nThis is a long README of the steps, but all the actual code and configuration for this app is included in this repo. You can copy and deploy it through your own Heroku account.\n\n\n# Deploying a simple Flask app to the cloud via Heroku\n\nThis walkthrough will acquaint you with the [popular Heroku cloud application platform](https://devcenter.heroku.com/start). Previously, we have been able to create Python Flask apps (see lessons [here](http://www.compjour.org/lessons/flask-single-page/) and [here](http://www.compjour.org/lessons/flask-recalls/)) and run them locally on our own computers. With a cloud service, we are able to put our websites onto the public World Wide Web, with a publicly accessible URL.\n\nA live version of this lesson's very simple app can be found at this URL:\nhttps://warm-scrubland-16039.herokuapp.com/\n\n(it'll be slow because I'm using Heroku's free tier)\n\nTo reduce the number of new moving parts to learn about, [we'll only worry about creating the simplest of Flask apps](http://www.compjour.org/lessons/flask-single-page/) -- the lesser the complexity, the fewer the dependencies, and thus, the fewer the conflicts. So pay attention to the steps that involve touching the Heroku service and toolset.\n\nDeploying an app on the cloud just means that we're putting code on a computer that we have no direct control over. Writing the Python app code is the same as it was before, but we have to follow a few Heroku conventions before Heroku will execute our code on its own computers.\n\nReview the lessons on [creating a simple Flask app if you've forgotten how to put together a simple Flask app](http://www.compjour.org/lessons/flask-single-page/).\n\nSome of the instructions in this tutorial comes from these this official Heroku tutorial: [Getting Started on Heroku with Python](https://devcenter.heroku.com/articles/getting-started-with-python). However, be warned, the official tutorial includes a lot of extra steps that may not apply to your system. I've tried to filter them to a minimum.\n\n\n------------------\n\n\n\n# Sign up for Heroku\n\nHeroku, being a Software as a Service (SaaS)-type of service, requires you to create an account and login before you can start using its computers. Don't worry, creating an account and running a simple app is free and doesn't require a credit card.\n\nYou can create an account at this URL: [https://signup.heroku.com/dc](https://signup.heroku.com/dc)\n\n## Download the Heroku toolbelt\n\nHeroku has a command-line \"toolbelt\" that we must download and install in order commands that will simplify our communication with the Heroku servers. The toolbelt can be downloaded at: [https://toolbelt.heroku.com/](https://toolbelt.heroku.com/)\n\n![image download-heroku-toolbelt.png](readme_assets/images/download-heroku-toolbelt.png)\n\n## Authenticate with Heroku with `heroku login`\n\nInstalling the Heroku toolbelt will give you access to the `heroku` command which has several subcommands for interacting with the Heroku service. \n\nThe first command you need to run is `heroku login`, which will ask you to enter your login credentials so that every subsequent `heroku` command knows who you are:\n\n(You will only have to do this __once__)\n\n~~~sh\n$ heroku login\n~~~\n\n![readme_assets/images/heroku-login.gif](readme_assets/images/heroku-login.gif)\n\n\n\n# Let's create a Flask app\n\nCreate a basic Flask app of any kind, i.e. one that consists of just __app.py__. You can revisit the [lessons here](http://www.compjour.org/lessons/flask-single-page/hello-tiny-flask-app/) or the sample repo at: [https://github.com/datademofun/heroku-basic-flask](https://github.com/datademofun/heroku-basic-flask)\n\nHeck, try to write it out by memory if you can -- below, I've made the app output simple HTML that includes the current time and a placeholder image from the [loremflickr.com](http://loremflickr.com/) service:\n\n~~~py\nfrom flask import Flask\nfrom datetime import datetime\napp = Flask(__name__)\n\n@app.route('/')\ndef homepage():\n    the_time = datetime.now().strftime(\"%A, %d %b %Y %l:%M %p\")\n\n    return \"\"\"\n    \u003ch1\u003eHello heroku\u003c/h1\u003e\n    \u003cp\u003eIt is currently {time}.\u003c/p\u003e\n\n    \u003cimg src=\"http://loremflickr.com/600/400\"\u003e\n    \"\"\".format(time=the_time)\n\nif __name__ == '__main__':\n    app.run(debug=True, use_reloader=True)\n~~~\n\nYou should be able to run this app on your own system via the familiar invocation and visiting [http://localhost:5000](http://localhost:5000):\n\n~~~sh\n$ python app.py\n~~~\n\n\n# Specifying dependencies for deploying Heroku\n\n(the following comes from Heroku's guide to [Deploying Python and Django Apps on Heroku](https://devcenter.heroku.com/articles/deploying-python))\n\nOur simple Flask app has has a couple of __dependencies__: the Python interpreter and the Flask library (duh). Which means that Python and the Flask library must be installed on our computer.\n\nWhen we talk about deploying our app onto Heroku, or any cloud service, we are working with _someone else's computer_. And, for the most part, we can't count on \"someone else's computer\" to have the same software stack as we do.\n\nWith Heroku, we have to include some metadata with our application code, so that Heroku knows how to set up a compatible webserver and install the software that our application needs. The metadata can be as simple as including a few plaintext files, which I list below in the next section. \n\n## Installing the gunicorn web server\n\nWhenever we run `python app.py` from our command-line, we're running the default webserver that comes with Flask. However, Heroku seems to prefer a [web server called __gunicorn__](https://devcenter.heroku.com/articles/python-gunicorn). Just so that we can follow along with Heroku's documentation, let's install gunicorn on our own system. It's a Python library like any other and can be installed with __pip__:\n\n~~~sh\n$ pip install gunicorn\n~~~\n\n## Adding a requirements.txt\n\nBy convention, Python packages often include a plaintext file named __requirements.txt__, in which the dependencies for the package are listed on each line.\n\nCreate an empty file named `requirements.txt` (in the same root folder as __app.py__).\n\nSo, what our are dependencies? For starters, __Flask__. So, add it to __requirements.txt__ (it's case sensitive):\n\n~~~\nFlask\n~~~\n\nEven though it's not part of our __app.py__, we did just install __gunicorn__ -- because I said to -- so let's throw that into __requirements.txt__:\n\n~~~\nFlask\ngunicorn\n~~~\n\n\n## Specifying Python version with `runtime.txt`\n\nHeroku will know that we be running a Python app, but because there's a huge disparity between Python versions (notably, [Python 2 versus 3](https://wiki.python.org/moin/Python2orPython3)), we need to tell Heroku to use the Python version that we're using on our own computer to develop our app.\n\nWhich version of Python are we/you running? From your command line, run the Python interpreter with the `--version` flag:\n\n~~~sh\n$ python --version\nPython 3.5.1 :: Anaconda 2.5.0 (x86_64)\n~~~\n\nNevermind that `\"Anaconda\"` bit -- we just need the version number, e.g. __3.5.1__\n\n\nCreate __runtime.txt__ in your root app folder and add just the single line (note: replace my example version number with yours, if it is different):\n\n~~~sh\npython-3.5.1\n~~~\n\n## Create a `Procfile`\n\nOK, one more necessary plaintext file: [Heroku needs a file to tell it how to start up the web app](https://devcenter.heroku.com/articles/deploying-python#the-procfile). By convention, this file is just a plaintext file is called (and named): __Procfile__:\n\n\u003e A Procfile is a text file in the root directory of your application that defines process types and explicitly declares what command should be executed to start your app. \n\n\nAnd for now, __Procfile__ can contain just this line:\n\n~~~\nweb: gunicorn app:app --log-file=-\n~~~\n\n\n## Add a .gitignore file\n\nThis is not necessary for running an app, but we want to prevent unwanted files from being pushed up to Heroku Git repo (or any git repo) later on. Create a new file named `.gitignore` and copy the contents of the example `.gitignore` file here:\n\n[https://github.com/github/gitignore/blob/master/Python.gitignore](https://github.com/github/gitignore/blob/master/Python.gitignore)\n\n\n\n## Run `heroku local`\n\nSo now our simple Flask app folder contains this file structure:\n\n~~~\n  ├── .gitignore\n  ├── Procfile\n  ├── app.py\n  ├── requirements.txt\n  └── runtime.txt\n~~~\n\nBefore we deploy our app on to Heroku, we'll want to test it on our own system _again_ -- but this time, instead of using `python app.py`, [we'll use the Heroku toolbelt subcommand, __local__](https://devcenter.heroku.com/articles/deploying-python#build-your-app-and-run-it-locally):\n\n~~~sh\n$ heroku local web\n~~~\n\nIt will create an app at [http://localhost:5000](http://localhost:5000), which should work like before when you ran `python app.py.`\n\nWhat was the point of that? Go into your `Procfile`, delete the single line that we put into it, save it, then try `heroku local web` again. You'll get an error message because Heroku won't know how to build the application:\n\n~~~sh\n$ heroku local web\n[WARN] No ENV file found\n[WARN] Required Key 'web' Does Not Exist in Procfile Definition\n~~~\n\n\n# Setting up our app's Git repo\n\nThis part will be a little confusing. Heroku deploys using __git__ -- which is _not to be confused with_ __Github__. (Hopefully, you have __git__ installed at this point.)\n\nBasically, this means before we can deploy to Heroku, we need to create a git repo in our app, add the files, and commit them. But we _don't_ need to push them onto a Github repo if we don't want to.\n\nIn fact, for this basic app, don't bother making a Github repo. Just make a local git repo:\n\n~~~sh\n$ git init\n$ git add .\n$ git commit -m 'first'\n~~~\n\n\n# Creating a Heroku application\n\nOK, now Heroku has all it needs to provision a server for our application.\n\nNow we need to do two steps:\n\n1. Tell Heroku to initialize an application [via its __create__ command](https://devcenter.heroku.com/articles/creating-apps).\n2. Tell Heroku to deploy our application by pushing our code onto the Git repo hosted on Heroku.\n\n\n## Initializing a Heroku application\n\nFirst, make sure you've successfully created a Git repo in your app folder. Running `git status` should, at the very least, not give you an error message telling you that you've yet to create a Git repo.\n\nThen, run this command:\n\n~~~sh\n$ heroku create\n~~~\n\nThe __create__ subcommand sets up a URL that your application will live at, and a Git repo from which you'll be pushing your code to on deployment.\n\nThe `heroku create` command results in output that looks like this:\n\n~~~stdout\nCreating app... ⬢ warm-scrubland-16039\nhttps://warm-scrubland-16039.herokuapp.com/ | https://git.heroku.com/warm-scrubland-16039.git\n~~~\n\nThat output tells us two things:\n\n1. Our application can be visited at: `https://boiling-journey-47934.herokuapp.com/`\n2. Heroku has git repo at the url `https://git.heroku.com/boiling-journey-47934.git`...In fact, the `create` command has helpfully set up a _remote_ named _heroku_ for us to __push__ to.\n\nIf you visit your application's app, e.g. `https://some-funnyword-9999.herokuapp.com/`. you'll get a placeholder page:\n\n![image welcome-heroku.png](readme_assets/images/welcome-heroku.png)\n\nThat's not what we programmed our app to do -- so that's just a page that comes from Heroku -- we haven't really deployed our app yet. But Heroku is ready for us. You can further confirm this by visiting [https://dashboard.heroku.com/](https://dashboard.heroku.com/) and seeing your application's URL at the bottom:\n\n![image heroku-dashboard.png](/readme_assets/images/heroku-dashboard.png)\n\nClicking on that application entry will reveal a page that is empty of \"processes\":\n\n![image heroku-dashboard-initial-app.png](readme_assets/images/heroku-dashboard-initial-app.png)\n\n\nAs for that Git repo that Heroku created for us...run this command to see the endpoints:\n\n~~~sh\n$ git remote show heroku\n~~~\n\nThe output:\n\n~~~sh\n* remote heroku\n  Fetch URL: https://git.heroku.com/warm-scrubland-16039.git\n  Push  URL: https://git.heroku.com/warm-scrubland-16039.git\n  HEAD branch: (unknown)\n~~~\n\n\n\n## Deploying our application code\n\nOK, let's finally __deploy our app__. We tell Heroku that we want to deploy our currently committed code by doing a `git push` to `heroku master`:\n\n~~~sh\n$ git push heroku master\n~~~\n\nThis should seem familiar to when you've pushed code to your __Github account__, but targeting `origin master`:\n\n~~~sh\n$ git push origin master\n~~~\n\n...but of course, we haven't actually created a __Github__ git repo for our simple app...we've only created a __local repo__. And, by running `heroku create`, we also created a repo on Heroku...which we will now push to:\n\n~~~sh\n$ git push heroku master\n~~~\n\nAnd with that simple command, Heroku will go through the steps of taking our application code, installing the dependencies we specified in `requirements.txt` and `runtime.txt`, and then starting a webserver as specified in `Procfile`:\n\n(this process takes a lot longer than simply pushing code onto Github to save)\n\nAfter about 30 seconds, you'll get output telling you how to find your application on the web:\n\n\n![readme_assets/images/heroku-git-push.gif](readme_assets/images/heroku-git-push.gif)\n\n~~~sh\nremote:        https://warm-scrubland-16039.herokuapp.com/ deployed to Heroku\nremote: \nremote: Verifying deploy.... done.\nTo https://git.heroku.com/warm-scrubland-16039.git\n   1c6e386..b0e9510  master -\u003e master\n~~~\n\nMy app happens to be given the name __warm-scrubland-16039__, which means that it is now available at the following URL for the whole world:\n\n[https://warm-scrubland-16039.herokuapp.com/](https://warm-scrubland-16039.herokuapp.com/ )\n\nAnd that's how you make your application available to the world.\n\n## Scaling the app with dynos\n\nHeroku [has this concept of __dynos__](https://devcenter.heroku.com/articles/dynos), an abstraction of the servers used to host your app and do its computational work. The free account lets you run apps on a single dyno...and by default, your new apps should have a single dyno upon creation. But just incase it doesn't, run this heroku command:\n\n\n~~~sh\n$ heroku ps:scale web=1\n~~~\n\n\n# Saving your application to Github\n\nFor homework purposes -- though not necessarily _this_ app -- you'll want to push your application code to your Github account as well.\n\nYou need to create an entirely new repo on Github, e.g. \n\n      https://github.com/your_username/fun_flask_app\n\n\nWhich will result in these instructions:\n\n\u003e ### …or push an existing repository from the command line\n\u003e\n\u003e `git remote add origin git@github.com:your_username/fun_flask_app.git`\n\u003e \n\n\nReplace `your_username` and `fun_flask_app` with the appropriate names. Then add run the given command:\n\n~~~sh\ngit remote add origin git@github.com:your_username/fun_flask_app.git\n~~~\n\nThe __git add__ and __git commit__ commands stay the same no matter how many repos you push to. But you have to push to each repo specifically and separately:\n\nTo get your code on Github:\n\n~~~\ngit push origin master\n~~~\n\nAnd, again, to get it deployed on Heroku:\n\n~~~\ngit push heroku master\n~~~\n\n\n# Changing our application code\n\nAltering the codebase of a Heroku-deployed app is not much different than how we've re-edited and saved code before, except that we have to run __git push heroku master__ in order to update the application on the Heroku server -- Heroku's server doesn't have a mind-meld with our computer's hard drive, we have to notify it of our changes via a `git push`.\n\nHowever, `git push` doesn't push anything until we've actually changed code -- and added and committed those changes via `git add` and `git commit`.\n\nGive it a try. Change __app.py__. Then add/commit/push:\n\n~~~sh\ngit add --all\ngit commit -m 'changes'\ngit push heroku master\n~~~\n\nDepending on how much you've altered the code base, the push/deploy process may take just as long as the initial install. But that's a reasonable price to pay for an easy process for updating an application that the entire world can access.\n\n\n# Managing your Heroku apps\n\nIf you plan on using Heroku to deploy your apps but _not while not paying a monthly bill_, you'll only be able to deploy one live app at a time.\n\nTo __destroy__ an app, which will destroy the deployed version and the reserved URL -- but _not_ your local code -- you can select your app via the [Heroku web dashboard](https://dashboard.heroku.com/apps), then delete it via its configuration/settings menu.\n\nOr, if you'd rather do it from the command-line with the Heroku toolbelt, use the __apps:destroy__ subcommand:\n\n~~~sh\n$ heroku apps:destroy whatever-yourappnameis-99999\n~~~\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenmohan%2Fpython-api-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenmohan%2Fpython-api-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenmohan%2Fpython-api-server/lists"}