How to use Plotly Python SDK in AWS Elastic Beanstalk - python

I've been trying to install Plot.ly Python SDK, I have it included in the requirements.txt but still fails and I get a Page Not Found error when calling a page served by Flask.

The problem with Plot.ly is that it requires the credentials to be installed:
import plotly
plotly.tools.set_credentials_file(username='SomeDemoAccount', api_key='SomeAPIKey')
And this won't run in as a code, not from ssh in the console because the instance doesn't has access to the ~/.plotly/.credentials file, i.e. it can't create it nor access it, so any call to the API will always fail. In AWS logs you'll get the following error:
Looks like you don't have 'read-write' permission to your 'home' ('~') directory or to our '~/.plotly' directory. That means plotly's python api can't setup local configuration files. No problem though! You'll just have to sign-in using 'plotly.plotly.sign_in()'. For help with that: 'help(plotly.plotly.sign_in)'.
So the solution is to call the plotly.plotly.sign_in() method that it's not even mentioned in their getting started guide nor the API reference, and it must be called with following arguments:
plotly.plotly.sign_in("Your Plotly Username","Your Plotly API Key")
That I implemented by having those values as EB Environment Properties:
plotly.plotly.sign_in(os.environ['YOUR_PLOTLY_USERNAME_ENV_PROPERTY_NAME'],os.environ['YOUR_PLOTLY_API_KEY_ENV_PROPERTY_NAME'])

Related

How do I check why a Kubernetes webpage keeps loading?

I followed this Google cloud Kubernetes tutorial for python. I basically changed what's in their hello world function to plot with matplotlib (with some other functions beforehand to get data to plot). It all worked (with some changes to the dockerfile, to pip install modules, and use just python 3.7 instead of the slim version) until where it says to view a deployed app. I copy the external IP and try it in the browser, but it just loads. I'm not sure what to check to see why it won't finish loading.
So I'm wondering how I check where the problem is. The python code works fine elsewhere, outputting a plot with flask on a local computer
You can try proxying from your localhost directly to the pod to see if there's a problem with the load balancer.
kubectl port-forward your-pod-xxxxxxxxxx-xxxxx <local-port>:<pod-port>
Then you can just hit http://172.0.0.1:<local-port> on your browser.
You can also take a look at the pod logs:
kubectl logs your-pod-xxxxxxxxxx-xxxxx

Trying to deploy a python script to staging bucket

For use with dialogflow I've been deploying index.js javascript files to google cloud sdk staging bucket using:
gcloud beta functions deploy projName --staging-bucket projName.appspot.com
--trigger-http
Today I tried to switch from javascript to python because I know it a little better.
I hoped it would be as simple as replacing index.js with index.py but the deployment still searches for index.js.
The error message I get is message=Function load error: File index.js or junction.js that is expected to define function does not exist in the root directory.
I got the impression it would be a simple switch because I'm using the webhook script provided by dialogflow themselves on here:
https://github.com/dialogflow/fulfillment-webhook-weather-python
Any ideas how to solve this issue?
You are trying to deploy a cloud function, but as far as I know, you can't write Google cloud functions in Python. Javascript is the only supported language at this time.
See, for example, https://cloud.google.com/functions/docs/writing/
If you want to deploy Python code, you are going to need some other method; maybe AppEngine, which is what it looks like the code you linked to was using.

Bluemix ignores route information from manifest file

I am trying to to push an app to IBM Bluemix Cloud Foundry. The manifest file contains route information like here:
applications:
# The Python app starts here
- name: yourname-myapp
memory: 256M
command: python myapp.py
routes:
- route: myapp.mybluemix.net/app-path
I tried it both with a Python and Node.js app. Bluemix seems to ignore the route information and only creates a route yourname-myapp.mybluemix.net, not the expected myapp.mybluemix.net/app-path. Why?
You must be using a cf CLI version older than 6.21.0.
The cf CLI ignores attributes it does not know so it created and mapped a route based on the app name for you.
You can install the latest cf CLI from https://github.com/cloudfoundry/cli/releases.
Make sure that your installed cf CLI is up-to-date. Pushing an app with route information in the manifest.yml requires at least CLI version 6.21. See the changelog for version 6.21.
Here is a sample app with routes including troubleshooting section, based on my tests.
In general, to find out about deployment or runtime errors of your Bluemix app, you can take a look into the error log. The most recent log entries can be obtained like this:
cf logs <application-name> --recent
You can also do the following to get a verbose readout of the push in your terminal:
CF_TRACE=true cf push yourname-myapp

Azure failing to run python script

I built a simple web app using Flask. What this does is basically take data from a form and sends a POST - which is then passed as a command line argument to the script using
os.popen("python3 script.py " + postArgument).read()
The command is stored in a variable which is then passed to an element in a new page with the results.
About the script: It runs the string in the POST through an API, gets some data, processes it, sends it to another API and finally prints the results (which are finally stored in the variable)
It works fine on a local server. But Azure fails to return anything. The string is empty.
How do I get some terminal logs?
Is there a solution?
Per my experience, it seems that the issue was caused by the Python 3 (even for Python 2) interpreter called python on Azure, not python3.
So if you had configured the Python 3 runtime environment for the Application settings on Azure portal as the figure below, please using python script.py instead of python3 script.py in your code.
Or you also can use the absolute path of Python 3 on Azure WebApp D:\Python34\python instead of python3 in your code, as below.
However, I also doubt the another possible issue for you besides the above case. You may use some python packages which be not install using pip on Azure. If so, you need to refer to the section Troubleshooting - Package Installation of the Azure offical document for Python to resolve the possible issue.
Hope it helps. Any concern & update, please feel free to let me know.

Access Google cloud endpoints on a non-existent version label

I have two apps, my_app and my_endpoint_app. I can access my_endpoint_app with any version label in the URL I want and it will automatically route to the default version if it does not match an existing version.
Example:
https://josh-dot-my_endpoint_app.appspot.com/ will respond with the default version since there is no josh version deployed.
If I try to do the same with a Google Cloud Endpoint service call, I get a Not Found error.
Example:
The unsuccessful https://josh-dot-my_endpoint_app.appspot.com/_ah/api/myendpoint vs the working https://my_endpoint_app.appspot.com/_ah/api/myendpoint
I have a couple of Google AppEngine applications that communicate with each other via Cloud Endpoints.
Under normal usage this is OK because I know the version beforehand and avoid these errors. In our development environment, this falls apart. In order to support feature branches and testing in isolation, we push our code up to appspot using the -V switch of appcfg.py.
Example:
appcfg.py -A my_app -V josh update .
Now I can access my feature branch at https://josh-dot-my_app.appspot.com. In order to support some version label hackery, I dynamically calculate the right endpoint app to call with something like s/my_app/my_endpont_app/g and then make my service calls there. This fails because of the dynamic version label not existing. If I push a version label with that name it completes as expected.
Is there any way to get Cloud Endpoints to answer on non-existent version label hostnames?
Scenarios that I want to support
https://my_endpoint_app.appspot.com/_ah/api/myendpoint
Main application URL, routes to default version
https://josh-dot-my_endpoint_app.appspot.com/_ah/api/myendpoint
Version does not exist, should route to default version
https://new-feature-dot-my_endpoint_app.appspot.com/_ah/api/myendpoint
Version new-feature exists, should route to new-feature version so that we can test new code in isolation before merging into the main code branch. This would be internal apis that the current endpoints might make use of without changing what the endpoint accomplishes. (performance improvements, etc)
You can reroute any Url to any module/version via the dispatch file.

Categories