Tuesday, January 25, 2022

Flask CORS based API hosted on Heroku



(base) C:\Users\Ashish Jain\OneDrive\Desktop\murmuring-ridge-75281>type *

Procfile

web: python TestService.py

requirements.txt

flask flask_restful flask-cors

TestService.py

from flask import Flask, request from flask_restful import Resource, Api from flask_cors import CORS, cross_origin import os app = Flask(__name__) cors = CORS(app) app.config['CORS_HEADERS'] = 'Content-Type' api = Api(app) class Test(Resource): def get(self): result = "Hello World" return result api.add_resource(Test, '/test') # URL Route if __name__ == '__main__': port = int(os.environ.get('PORT', 5000)) app.run(host='0.0.0.0', port=port) -----------------------------------

Test.html

<script crossorigin="anonymous" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script> function myclick() { $.get("https://murmuring-ridge-75281.herokuapp.com/test", function (data, status) { alert("Data: " + data + "\nStatus: " + status); }); } </script> <button class="customBtn" onclick="myclick()">GET</button> -----------------------------------

Error 1

(base) C:\Users\Ashish Jain\OneDrive\Desktop\gentle-garden-53110>git commit -m "#" [master a4dd273] # 1 file changed, 1 insertion(+) create mode 100644 Procfile (base) C:\Users\Ashish Jain\OneDrive\Desktop\gentle-garden-53110>git push Enumerating objects: 6, done. Counting objects: 100% (6/6), done. Delta compression using up to 4 threads Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 722 bytes | 361.00 KiB/s, done. Total 6 (delta 0), reused 0 (delta 0), pack-reused 0 remote: Compressing source files... done. remote: Building source: remote: remote: -----> Building on the Heroku-20 stack remote: -----> Determining which buildpack to use for this app remote: ! No default language could be detected for this app. remote: HINT: This occurs when Heroku cannot detect the buildpack to use for this application automatically. remote: See https://devcenter.heroku.com/articles/buildpacks remote: remote: ! Push failed remote: Verifying deploy... remote: remote: ! Push rejected to gentle-garden-53110. remote: To https://git.heroku.com/gentle-garden-53110.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'https://git.heroku.com/gentle-garden-53110.git' (base) C:\Users\Ashish Jain\OneDrive\Desktop\gentle-garden-53110> (base) C:\Users\Ashish Jain\OneDrive\Desktop\gentle-garden-53110>heroku buildpacks:set heroku/python Buildpack set. Next release on gentle-garden-53110 will use heroku/python. Run git push heroku main to create a new release using this buildpack.

Error 2

(base) C:\Users\Ashish Jain\OneDrive\Desktop\gentle-garden-53110>git push Enumerating objects: 6, done. Counting objects: 100% (6/6), done. Delta compression using up to 4 threads Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 722 bytes | 361.00 KiB/s, done. Total 6 (delta 0), reused 0 (delta 0), pack-reused 0 remote: Compressing source files... done. remote: Building source: remote: remote: -----> Building on the Heroku-20 stack remote: -----> Using buildpack: heroku/python remote: -----> App not compatible with buildpack: https://buildpack-registry.s3.amazonaws.com/buildpacks/heroku/python.tgz remote: More info: https://devcenter.heroku.com/articles/buildpacks#detection-failure remote: remote: ! Push failed remote: ! remote: ! ## Warning - The same version of this code has already been built: a4dd27398a25f894326ca01b9651d867ee1a2c5f remote: ! remote: ! We have detected that you have triggered a build from source code with version a4dd27398a25f894326ca01b9651d867ee1a2c5f remote: ! at least twice. One common cause of this behavior is attempting to deploy code from a different branch. remote: ! remote: ! If you are developing on a branch and deploying via git you must run: remote: ! remote: ! git push heroku <branchname>:main remote: ! remote: ! This article goes into details on the behavior: remote: ! https://devcenter.heroku.com/articles/duplicate-build-version remote: remote: Verifying deploy... remote: remote: ! Push rejected to gentle-garden-53110. remote: To https://git.heroku.com/gentle-garden-53110.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'https://git.heroku.com/gentle-garden-53110.git'

Error 3

(base) C:\Users\Ashish Jain\OneDrive\Desktop>heroku create --buildpack heroku/python Creating app... ! ! You've reached the app limit of 5 apps for unverified accounts. Delete some apps or add a credit card to verify your account.

How to set 'buildpack' at the time of creating project:

(base) C:\Users\Ashish Jain\OneDrive\Desktop>heroku create --buildpack heroku/python Creating app... done, ⬢ murmuring-ridge-75281 Setting buildpack to heroku/python... done https://murmuring-ridge-75281.herokuapp.com/ | https://git.heroku.com/murmuring-ridge-75281.git -----------------------------------

README

![python](https://cloud.githubusercontent.com/assets/51578/13712821/b68a42ce-e793-11e5-96b0-d8eb978137ba.png) # Heroku Buildpack: Python [![CircleCI](https://circleci.com/gh/heroku/heroku-buildpack-python.svg?style=svg)](https://circleci.com/gh/heroku/heroku-buildpack-python) This is the official [Heroku buildpack](https://devcenter.heroku.com/articles/buildpacks) for Python apps. Recommended web frameworks include **Django** and **Flask**, among others. The recommended webserver is **Gunicorn**. There are no restrictions around what software can be used (as long as it's pip-installable). Web processes must bind to `$PORT`, and only the HTTP protocol is permitted for incoming connections. See it in Action ---------------- ``` $ ls my-application requirements.txt runtime.txt $ git push heroku main Counting objects: 4, done. Delta compression using up to 8 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (4/4), 276 bytes | 276.00 KiB/s, done. Total 4 (delta 0), reused 0 (delta 0) remote: Compressing source files... done. remote: Building source: remote: remote: -----> Python app detected remote: -----> Installing python remote: -----> Installing pip remote: -----> Installing SQLite3 remote: -----> Installing requirements with pip remote: Collecting flask (from -r /tmp/build_c2c067ef79ff14c9bf1aed6796f9ed1f/requirements.txt (line 1)) remote: Downloading ... remote: Installing collected packages: Werkzeug, click, MarkupSafe, Jinja2, itsdangerous, flask remote: Successfully installed Jinja2-2.10 MarkupSafe-1.1.0 Werkzeug-0.14.1 click-7.0 flask-1.0.2 itsdangerous-1.1.0 remote: remote: -----> Discovering process types remote: Procfile declares types -> (none) remote: ``` A `requirements.txt` must be present at the root of your application's repository to deploy. To specify your python version, you also need a `runtime.txt` file - unless you are using the default Python runtime version. Current default Python Runtime: Python 3.9.10 Alternatively, you can provide a `setup.py` file, or a `Pipfile`. Using `pipenv` will generate `runtime.txt` at build time if one of the field `python_version` or `python_full_version` is specified in the `requires` section of your `Pipfile`. Specify a Buildpack Version --------------------------- You can specify the latest production release of this buildpack for upcoming builds of an existing application: $ heroku buildpacks:set heroku/python Specify a Python Runtime ------------------------ Supported runtime options include: - `python-3.10.2` - `python-3.9.10` - `python-3.8.12` - `python-3.7.12`

No comments:

Post a Comment