Python Forum

Full Version: Get return text from a failed script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm new to Python and I'm trying to run a their script.  It works a treat, however, if I use it too much, I get an error saying I've breached the API limit.

Is there a way I can somehow capture that text response?  I'll include the last bit of my script.  The error gets generated at the very last line.

layout = go.Layout(
title='BigScenario - Average',
xaxis=dict(
title='Application Version',
titlefont=dict(
family='Times New Roman, monospace',
size=18
)
),
yaxis=dict(
title='Seconds',
titlefont=dict(
family='Times New Roman, monospace',
size=18
)
),
hovermode='closest'
)

fig = go.Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='09052017230558')
So when I run it, I get the following returned in the console:
   
Error:
plot_url = py.plot(fig, filename='09052017230558')   File "C:\Users\John\AppData\Local\Programs\Python\Python36\lib\site-packages\plotly\plotly\plotly.py", line 227, in plot     response = v1.clientresp(data, **plot_options)   File "C:\Users\John\AppData\Local\Programs\Python\Python36\lib\site-packages\plotly\api\v1\clientresp.py", line 35, in clientresp     response = request('post', url, data=payload)   File "C:\Users\John\AppData\Local\Programs\Python\Python36\lib\site-packages\plotly\api\v1\utils.py", line 86, in request     validate_response(response)   File "C:\Users\John\AppData\Local\Programs\Python\Python36\lib\site-packages\plotly\api\v1\utils.py", line 38, in validate_response     raise exceptions.PlotlyRequestError(message, status_code, content) plotly.exceptions.PlotlyRequestError: Hey there! You've hit one of our API request limits.  To get unlimited API calls(10,000/day), please upgrade to a paid plan.  Thanks for using Plotly! Happy Plotting!
I'd like to capture and check for a substring in that error if it occurs.  Is this possible?

Many thanks,
J
from plotly.exceptions import PlotlyRequestError

try:
   plot_url = py.plot(fig, filename='09052017230558')
except PlotlyRequestError:
   # here you can do what you want when this error is raised
   # to handle it graciously e.g.
   print("Hey there! You've hit one of our API request limits.")
you need to use try/except block. Note that you need to import the specific error (at the top of the script). i.e. you don't check for the string - that's how exceptions are handled in python.
Also in the above example I just print the message, but if you reached the limit, maybe it's better to quit or whatever else you deem necessary.
Hi mate,

Many thanks for that!  It has worked a treat.

I'll read up some more on that but a good start.

Cheers,
John