Changing a file on Google App Engine - python

I have a Google App Engine Python project that I haven't touched since 2014, when you worked with a desktop app to upload and deploy it. Google App engine changed a lot in the meantime. According to the information the deploying guide I copy
the file(s) like this:
gsutil cp main.py gs://myproject.appspot.com/
I converted my app.yaml file to a json file using their utility. When I initially deployed the app using this page it failed because there was no deployment information, so I added this:
"deployment": {
"files": {
"main": {
"sourceUrl": "https://storage.googleapis.com/myproject/main.py"
},
}
},
However when I deployed again I got an error because it couldn't find the file with that URL. The gsutil has to use a bucket with gs://something and the file has to exist at https:// something. How can I upload the file to the project and specify the location in the app.json file?

You were probably using what is now called App Engine Standard environment. Deploys are handled with the gcloud app deploy command.
Example:
gcloud app deploy --version [YOUR_VERSION_ID] --no-promote --project [YOUR_PROJECT_ID]
Here is more info for Deploying a Python App
The deploy command uses app.yaml file for configuration info.

Related

How to load a static file in Flask app when deploying to Heroku?

I have a Flask JSON API application that would read a file in the code repository and display the data in that file. My application file is as follows:
from flask import Flask, jsonify
from utils import load_data
data = load_data(os.environ.get('DATA_FILE_PATH')) # This would be in data/data.txt for example
app = Flask(__name__)
#app.route('/', methods=['GET'])
def home():
return jsonify(data=data)
When I deploy to Heroku, how can I set the environment variable to point to that particular data file? Locally, I would do
$ export DATA_FILE_PATH=/path/to/repo/data/data.txt
I know I can use the Heroku CLI heroku config:set DATA_FILE_PATH=<path> but I'm not sure how to place that path variable since I don't really know the directory structure on the Heroku deployment.
You can add them in the settings tab config vars
Okay, turns out you can go into the Heroku Dyno's bash using the following command (assuming you've already pushed to Heroku):
$ heroku run bash
and figure out what the file system structure looks like. In my particular case, my repo was served under the /app directory. So all I needed to pass into the value of the DATA_FILE_PATH environment variable is /app/data/data.txt.

How to deploy Flask app as package to App Engine?

I'm following various tutorials from Google Cloud to try and deploy my first Python Flask application to App Engine. I organized my app as a package instead of a module, after watching the tutorial from Corey Schafer on YouTube titled: "Python Flask Tutorial: Full-Featured Web App Part 5 - Package Structure"
So now, in my working directory, I have a structure like this:
1. Project folder
1.1 myapp folder
1.1.1 __pycache__
1.1.2 static folder
1.1.3 __init__.py
1.1.4 app.yaml
1.1.5 requirements.txt
1.1.6 something_else.py
1.2 run.py
My run.py file has the following code:
from myapp import app
if __name__ == '__main__':
app.run(debug=True)
My app.yaml file looks like this:
runtime: python37
handlers:
# This configures Google App Engine to serve the files in the app's static
# directory.
- url: /static
static_dir: static
# This handler routes all requests not caught above to your main app. It is
# required when static routes are defined, but can be omitted (along with
# the entire handlers section) when there are no static files defined.
- url: /.*
script: auto
Now I am not sure how to set up my app.yaml file to specify the entry point, and also make my app run when I deploy it to App Engine. I am currently running the gcloud app deploy command via the Google Cloud SDK after I cd into myapp folder first?
The deployment phase in the console goes well, but when I check the app's browsing link, I am faced with a 502 Bad Gateway error (as I'm also expecting).
Everything works fine locally, but the deployment isn't too straightforward for me now that my app is structured like a package. Any help is greatly appreciated.
In your app.yaml, you can specify a custom entrypoint like so:
runtime: python37
entrypoint: gunicorn -b :$PORT myapp:app
Where myapp:app corresponds to the module to import the WSGI app from (from myapp import app)
You should also move your app.yaml file to be in the project folder instead, and run gcloud app deploy from there instead.
More details here: https://cloud.google.com/appengine/docs/standard/python3/runtime

Proxying API request from React frontend to Flask backend on two separate Heroku apps

I am working on a web application with a create-react-app frontend and a Flask API backend with Flask-RESTful. I built the app locally and it works on my computer, but now I am unsuccessfully trying to deploy it to Heroku.
I deployed the Flask backend and the React frontend as two different Heroku dynos, so each one has their own URL. When I go to the URL for the frontend, it appears to be working fine, and the backend URL seems to work too. However, the app relies on API calls to the backend, and I am having trouble successfully sending requests to my backend.
When I was working locally, I would run the flask backend with the gunicorn --bind 0.0.0.0:8000 app:app and the frontend with the npm start command. The app worked fine with relative calls in my Axios function calls as long as I put "proxy": "https://localhost:8000" in my package.json file in my frontend app. In general, I followed this tutorial: https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/.
I thought I could simply edit the proxy field in the package.json file to point to the URL of my backend Heroku app, and then I wouldn't have to change anything else. However, when I deploy using this, I find that the requests to my endpoint are being made to https://my-frontend-app.herokuapp.com/<endpoint> instead of https://my-backend-app.herokuapp.com/<endpoint>.
Here is my package.json:
{
"name": "heroku-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"bootstrap": "^4.1.2",
"plotly.js": "^1.39.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-plotly.js": "^2.2.0",
"react-scripts": "1.1.4",
"reactstrap": "^6.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"proxy": "https://star-employees-api.herokuapp.com"
}
My requests look like this:
axios.get('/analyze', data)
.then((response) => {
// Handle request response here
};
Can anyone tell me what I'm doing wrong here? This is my first time deploying an app like this and using Heroku, so I'm not sure if I'm way off base here. If there is a better way to deploy this kind of app, please let me know as well.
I can provide any other code snippets or file structure if it's helpful, just not sure what would be helpful at this point. Thanks in advance.
I managed to figure it out. As Tholle mentioned, the proxy field in the package.json file is only for the dev server. If you're using create-react-app with Heroku and the buildpack, there are some other steps. When deploying, you need to follow the instructions here and add a static.json file in your topmost directory.

Getting internal server error on Flask App hosted on Azure

I've hosted a flask app on Azure, but there seems to be some problem with linking the WSGIHandler. It is a very simple bug. I can't seem to identify it.
The following is the error I'm getting in my Logs
Error occurred while reading WSGI handler:
Traceback (most recent call last):
File "D:\Python27\Scripts\wfastcgi.py", line 711, in main
env, handler = read_wsgi_handler(response.physical_path)
File "D:\Python27\Scripts\wfastcgi.py", line 568, in read_wsgi_handler
return env, get_wsgi_handler(handler_name)
File "D:\Python27\Scripts\wfastcgi.py", line 551, in get_wsgi_handler
raise ValueError('"%s" could not be imported' % handler_name)
ValueError: "App" could not be imported
StdOut:
StdErr:
ErrorCode Access is denied.
(0x5)
Here is my folder structure
myapplication
-- App
-- __init__.py
The contents of __init__.py is
from flask import Flask
# initialize the flask app
app = Flask(__name__)
print "init"
#app.route('/')
def hello():
return "hello world";
if __name__ == "__main__":
app.run()
I've configured the following App Settings in Azure Web App
PYTHONPATH = D:\home\site\wwwroot
WSGI_HANDLER = App.app
Per my understanding, your deployment is incompleteness, as Azure uses IIS to host python web sites on Web Apps Services, which needs a web.config to configure hander mapping and URL rewrite rules and some other settings.
To create and deploy a flask project on Azure Web Apps , we can generally follow the steps below:
1, On Azure manage portal, click NEW => COMPUTE => WEB APP =>FROM GALLERY at the bottom navigation, on the ADD WEB APP dialog, select Flask, name the site on the next dialog page. Now we have created a flask web site project.
We can type the endpoint on the browser to check the website, http://<your_site_name>.azurewebsite.net
2,On the web apps list, click the name we created above to get into the configuration page, click DASHBOARD, at the quick glance column, click Set up deployment from source control ,select Local Git repository. Now there is an additional tab named DEPLOYMENT beside the DASHBOARD tab. And in the DEPLOYMENT page there are steps of how to deploy your site by git. We can clone the project to local by the GIT URL provided on this page.
We can get more on this official article

GIT/Heroku sensitive info

I have a django project deployed in Heroku. It uses python-instragram.
I have a 'client secret' from an instragram client that I have.
I use git/github for version control.
This client_secret is imported from an untracked file because I don't want to have it on my public github repo. I do something like this:
from core_keys import core_client_secret
CONFIG = {
'client_id': '83d1b794dfc24f5588378f88be67c586',
'client_secret': core_client_secret,
'redirect_uri': 'http://localhost:8515/oauth_callback'
}
api = client.InstagramAPI(**CONFIG)
I have core_keys.py added to .gitignore:
*/core_keys.py
When I deploy to heroku the app doesn't work obviously because the file that contains the client_secret was not pushed to heroku since it's in .gitignore.
How can I have this file on heroku without the need for a private repo, what approach should I use?
You should store the secrets as config vars in the environment.
Just as a reference, ended up doing this:
On the terminal at my development machine:
heroku config:set INSTAGRAMSECRET=00000FFFFF
On the file where I need the environment var inside of Heroku:
import os
insta_secret = os.environ['INSTAGRAMSECRET']

Categories