Python: Flask cache unable to work in specific time range - python

the Flask app I create only able to work if it outside the time range but return error if it is within the time range (the if path)
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.cache import Cache
from datetime import datetime, time
app.config['CACHE_TYPE'] = 'simple'
app.cache = Cache(app)
#app.route('/thtop', methods=['GET'])
#app.cache.cached(timeout=60)
def thtop():
now = datetime.now()
now_time = now.time()
if now_time >= time(3,30) and now_time <= time(16,30):
rv = app.cache.get('last_response')
else:
rv = 'abcc'
app.cache.set('last_response', rv, timeout=3600)
return rv
If the time in the if path, it unable to show the string abcc but shown Internal Server Error.
In WSGI error log, it also shown Exception on /thtop [GET]#012Traceback (most recent call last):#012 File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app#012 response = self.full_dispatch_request()#012 File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1361, in full_dispatch_request#012 response = self.make_response(rv)#012 File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1439, in make_response#012 raise ValueError('View function did not return a response')#012ValueError: View function did not return a response
What is wrong when I am caching?
UPDATE
Use flask_caching module but still same failures
from flask.ext.sqlalchemy import SQLAlchemy
from flask_caching import Cache
from datetime import datetime, time
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
#app.route('/thtop', methods=['GET'])
#cache.cached(timeout=60)
def thtop():
now = datetime.now()
now_time = now.time()
if now_time >= time(3,30) and now_time <= time(14,30):
rv = cache.get('last_response')
else:
rv = 'abcc'
cache.set('last_response', rv, timeout=3600)
return rv
The difference I observed in both different module when I run in console, starting from def thtop(), app.cache.get('last_response') return nothing. However, cache.get('last_response') return abcc.
The problem is when run in web app, it will cause error as shown above.

You're getting the error whenever now_time >= time(3,30) and now_time <= time(14,30) is True and rv = cache.get('last_response') is None. When that happens, you're trying to return None from the view which causes the ValueError.
You need to add some additional logic to check that the cache actually returns data:
from flask import Flask
from flask_caching import Cache
from datetime import datetime, time
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
#app.route('/thtop', methods=['GET'])
#cache.cached(timeout=60)
def thtop():
now = datetime.now()
now_time = now.time()
rv = None
if now_time >= time(3, 30) and now_time <= time(14, 30):
rv = cache.get('last_response')
if not rv:
rv = 'abcc'
cache.set('last_response', rv, timeout=3600)
return rv
if __name__ == '__main__':
app.run()
with this logic your route will always return something so you won't get the ValueError.

Seems like this statement is True: if now_time >= time(3,30) and now_time <= time(16,30)
That is why you are trying to get the last_response value from rv = app.cache.get('last_response') which is equal to None I think.
The Internal Server Error is thrown because you return a NoneType object that is not valid. You should return a function() or 'a string' instead.
Try fixing this Error by changing app.cache.get('last_response') to app.cache.get('last_response', 'FIXED')

Related

Python error when trying to execute a script to shutdown GCP VM

The functionality of this script to shutdown GCP VM's based on some logic. Currently we are trying shut it down at night, but the scripts are failing before it could shutdown the VM. We shutdown based on time. At evenings and night the VM are shutdown using these scripts
Script to turn off instances overnight
Environment: Runs in Google Cloud Functions (Python3.7)
import datetime
import json
from pprint import pformat
import pytz
import re
import modules.common.cfcommon as cfcommon
import modules.utilities.dateutilities as dateutilities
from modules.compute.instances import InstanceList, Instance
from modules.compute.compute_service import ComputeServiceContext
from modules.utilities.printutilities import print_message, debug_message
from modules.pubsub.topic import PublishMessage
from modules.common.labels import VMAUTOSHUTDOWN_LABEL, VMAUTOSHUTDOWN_DEFER_LABEL, ShutdownDeferLabelValueIsValid, ShutdownLabelValueIsValid
from templates.renderer import render_template
# Takes a list in the following format and checks if the 'Instance' object is within it
# list must contain dictionaries in the following format:
# {"name": "instancename", "zone": "zonename"}
# Example: {"name": "test-01", "zone": "us-east4-c"}
#
# Parameters:
# inputList - list of dictionary objects
# instance - Instance object
def isInstanceInList(inputList, instance):
if not isinstance(inputList, list):
raise TypeError("Provided inputList is not a list")
if not isinstance(instance, Instance):
raise TypeError("Provided instance is not of type 'Instance'")
# Iterate over every item in inputList and check if the name and zone match
for cItem in inputList:
if cItem["name"].lower() == instance.properties["name"].lower() and cItem["zone"].lower() == instance.GetShortZoneName().lower():
return True
# No match found
return False
# Takes a list of Instance objects and sees if their shutdown timezone is within the graceperiod of the shutdownHour
#
# Example: is shutdown hour is 23 and the gracePeriodMin is 15 then if the function is called at 23:12, the instance will be included in the shutdown list
#
# Parameters:
# instanceList - List of Instance objects
# shutdownHour - number (0-23) 0 = Midnight, 23 = 11PM
# gracePeriodMin - number
def getInstancesToStop(instanceList, gracePeriodMin):
instancesToStop = []
debug_message("Entering getInstancesToStop")
for cInstance in instanceList:
debug_message("Instance: %s (ID: %s, Zone: %s, Project: %s)" % (cInstance.GetName(), cInstance.GetId(), cInstance.GetShortZoneName(), cInstance.project))
labels = cInstance.GetLabels()
if VMAUTOSHUTDOWN_LABEL in labels.keys():
labelValue = labels.get(VMAUTOSHUTDOWN_LABEL, '')
pattern = '\d\d-\d\d-\d\d'
match = re.match(pattern, labelValue)
if not match or not ShutdownLabelValueIsValid(labelValue):
debug_message(f'Label {labelValue} does not match the correct format')
instancesToStop.append(cInstance)
continue
else:
debug_message(f'Label {VMAUTOSHUTDOWN_LABEL} not found. Adding to shutdown list')
instancesToStop.append(cInstance)
continue
shutdown_deferred_utc_datetime = None
if VMAUTOSHUTDOWN_DEFER_LABEL in labels.keys():
labelValue = labels.get(VMAUTOSHUTDOWN_DEFER_LABEL, '')
pattern = '\d\d\d\d-\d\d-\d\dt\d\d-\d\d-\d\d'
match = re.match(pattern, labelValue)
if match and ShutdownDeferLabelValueIsValid(labelValue):
shutdown_deferred_utc_date, shutdown_deferred_utc_time = labelValue.split('t')
year, month, day = shutdown_deferred_utc_date.split('-')
hour, minute, second = shutdown_deferred_utc_time.split('-')
shutdown_deferred_utc_datetime = datetime.datetime.now(pytz.timezone('GMT')).replace(
year=int(year), month=int(month), day=int(day), hour=int(hour), minute=int(minute), second=int(second)
)
else:
debug_message(f'Label {labels[VMAUTOSHUTDOWN_DEFER_LABEL]} does not match the correct format')
instancesToStop.append(cInstance)
continue
current_utc_time = dateutilities.get_current_datetime()
# If defer date is in the future, and not in grace window time, skip shutting down
if shutdown_deferred_utc_datetime is not None and shutdown_deferred_utc_datetime > current_utc_time:
debug_message(f'Instance {cInstance.GetName()} shutdown deferred until after {labels[VMAUTOSHUTDOWN_DEFER_LABEL]}')
continue
# If defer time is in past, continue with the vm hour shutdown
shutdown_utc_hour = labels[VMAUTOSHUTDOWN_LABEL].split('-')[0]
# Convert shutdown UTC hour into datetime object
shutdown_utc_time = datetime.datetime.now(pytz.timezone('GMT')).replace(hour=int(shutdown_utc_hour), minute=0, second=0)
shutdown_utc_grace_time = shutdown_utc_time + datetime.timedelta(minutes=gracePeriodMin)
debug_message(f"Shutdown UTC time {shutdown_utc_time}")
debug_message(f"Shutdown UTC grace time {shutdown_utc_grace_time}")
# Check if shutdown is within time window
if current_utc_time >= shutdown_utc_time and current_utc_time <= shutdown_utc_grace_time:
debug_message("We're in the time window")
instancesToStop.append(cInstance)
else:
debug_message("We're outside the time window. Not adding to stop list")
return instancesToStop
# This is the main entry point that cloud functions calls
def AutoStopVMInstances(config, policy=None, payload=None, generate_local_report=False):
FUNCTION_NAME = "AutoStopVMInstances"
# Populated by config later...
QUERY_PROJECT_IDS = None # List of project IDs
INSTANCE_WHITELIST = None # List of dictionaries in format {"name": "instancename", "zone": "zonename", "project": "projectid"}
PREVIEW_MODE = True
SHUTDOWN_GRACEPERIOD_MIN = 30
# Start
startTime = datetime.datetime.now()
print_message("Started %s within Cloud Function %s [%s]" % (FUNCTION_NAME, cfcommon.CLOUD_FUNCTION_NAME, startTime))
debug_message("")
# For ease of access, assign from config values
debug_message("Processing Configuration...")
QUERY_PROJECT_IDS = config.get("QueryProjectIDs", []) # Required field
INSTANCE_WHITELIST = config.get("InstanceWhiteList", []) # Optional Field
PREVIEW_MODE = config.get("PreviewMode", True) # Required field
SHUTDOWN_GRACEPERIOD_MIN = config.get("ShutdownGracePeriodMin", None) # Required field
SKIP_INSTANCE_GROUPS = config.get("SkipInstanceGroups", False) # Optional
EMAIL_PUB_SUB_PROJECT = config.get("EmailPubSubProject", None) # Optional
EMAIL_PUB_SUB_TOPIC = config.get("EmailPubSubTopic", None) # Optional
EMAIL_TO = config.get("EmailTo", []) # Optional
EMAIL_CC = config.get("EmailCC", None) # Optional
EMAIL_BCC = config.get("EmailBCC", None) # Optional
EMAIL_FROM = config.get("EmailFrom", "noreply-ei-cs-cloudops-resource-administration#ei-cs-cloudops.local") # Optional
EMAIL_SUBJECT = config.get("EmailSubject", "Nightly VM Instance Shutdown Summary") # Optional
cfLogger = cfcommon.CloudFunctionLog()
# Validate whitelist
if INSTANCE_WHITELIST is None:
raise Exception("Unable to get whitelist")
debug_message("Whitelist loaded:")
debug_message(pformat((INSTANCE_WHITELIST)))
# Re-init Compute service - execution environment in cloud functions can be shared among each other. Let's re-init our connection every execution.
ComputeServiceContext.InitComputeService()
# Build the service object.
allRunningInstances = []
for cProjectId in QUERY_PROJECT_IDS:
debug_message("Checking Project: %s" % (cProjectId))
# Main Loop - Let's get and analyze all instances from our project
# Paginated within the 'request' object
runningInstances = []
allInstances = []
debug_message("Building Instance List...", end="")
instances = InstanceList(cProjectId)
instances.PopulateInstances()
debug_message("Done")
for cInstance in instances.GetAllInstances():
debug_message("Found Instance %s in %s [%s - %s]" % (cInstance.GetName(), cInstance.GetZone(), cInstance.GetId(), cInstance.GetStatus()))
# Check if whitelisted. If it is, skip it
if isInstanceInList(INSTANCE_WHITELIST, cInstance):
debug_message(" Instance is whitelisted. Skipping.")
continue
# Check if we should skip instance groups
if SKIP_INSTANCE_GROUPS and cInstance.IsWithinInstanceGroup():
debug_message(" Instance is within an instance group. Skipping.")
continue
debug_message(" Is Running: %s" % (cInstance.IsRunning()))
owner = cInstance.GetOwner()
if owner in ("devops", "ei devops", "eicsdevopseng"):
debug_message("Skipping instance owned by devops")
continue
# # TODO: FOR USE WHEN TESTING
# if VMAUTOSHUTDOWN_LABEL not in labels.keys():
# continue
# Keep track of this instance
allInstances.append(cInstance)
# If it's running, it's a candidate to stop
if cInstance.IsRunning():
runningInstances.append(cInstance)
# Handle no instances found
if len(allInstances) == 0:
debug_message("INFO: No Instances found.")
# Summarize for user
debug_message("")
if len(runningInstances) > 0:
debug_message("Found %s/%s non-whitelisted instances are running (project: %s)" % (len(runningInstances), len(allInstances), cProjectId))
else:
debug_message("All %s non-whitelisted instances are good (project: %s)" % (len(allInstances), cProjectId))
# Main loop to stop
debug_message("")
allRunningInstances = allRunningInstances + runningInstances
instancesToBeStopped = getInstancesToStop(allRunningInstances, SHUTDOWN_GRACEPERIOD_MIN)
stoppedCount = 0
instanceSummary = []
if len(instancesToBeStopped) == 0:
print_message("No instances are due to be stopped")
else:
for cInstance in instancesToBeStopped:
summaryEntry = {
"Name": cInstance.GetName(),
"ID": cInstance.GetId(),
"Zone": cInstance.GetShortZoneName(),
"Project": cInstance.GetProject(),
"Preview": PREVIEW_MODE,
"Stopped": False,
"InstanceLink": cInstance.GetSelfLinkToConsole()
}
logMessage = "Stopping Instance: {name} (ID: {id}, Zone: {zone}, Project: {project})".format(
name=summaryEntry.get("Name"),
id=summaryEntry.get("ID"),
zone=summaryEntry.get("Zone"),
project=summaryEntry.get("Project")
)
if PREVIEW_MODE:
print_message("(PREVIEW) " + logMessage )
else:
print_message(logMessage)
cInstance.Stop()
summaryEntry["Stopped"] = True
stoppedCount += 1
instanceSummary.append(summaryEntry)
if EMAIL_PUB_SUB_PROJECT is not None and EMAIL_PUB_SUB_TOPIC is not None:
debug_message("It looks like we have an email config. Attempting to send email")
emailBody = render_template(
'shutdown_report',
instance_summary=instanceSummary,
config=json.dumps(config, indent=4, sort_keys=True),
preview_mode=PREVIEW_MODE,
generation_time=datetime.datetime.now().astimezone(pytz.utc)
)
emailPayload = {
"To": EMAIL_TO,
"From": EMAIL_FROM,
"Subject": EMAIL_SUBJECT,
"BodyHtml": emailBody
}
if EMAIL_CC is not None:
emailPayload["CC"] = EMAIL_CC
if EMAIL_BCC is not None:
emailPayload["BCC"] = EMAIL_BCC
if not generate_local_report:
print_message("Sending email...", end="")
PublishMessage(EMAIL_PUB_SUB_PROJECT, EMAIL_PUB_SUB_TOPIC, json.dumps(emailPayload))
else:
print_message('Generating local HTML report')
with open('./html_reports/shutdown_report.html', 'w') as r:
r.write(emailBody)
print_message("Done")
# We want to log a nice structured json line to stackdriver for easy reporting.
cfLogger.log({
"StartTime": startTime.isoformat(),
"InstancesStoppedCount": stoppedCount,
"PreviewMode": PREVIEW_MODE,
"Instances": instanceSummary,
"Whitelist": INSTANCE_WHITELIST,
"EndTime": datetime.datetime.now().isoformat(),
"LogLine": "Summary"
})
print_message("DONE [%s]" % (datetime.datetime.now()))
The error I'm getting when trying to run a VM shutdown script:
Caught exception while running VMNightlyShutdown. Exception Text: Traceback (most recent call last):
File "/workspace/main.py", line 248, in StartCloudFunction
policy=policy_config)
File "/workspace/vm_nightly_shutdown.py", line 256, in AutoStopVMInstances
cInstance.Stop()
File "/workspace/modules/compute/instances.py", line 729, in Stop
stop_instance(self.project, self.GetShortZoneName(), self.GetName(), waitForCompletion=waitForCompletion)
File "/workspace/modules/compute/instances.py", line 45, in stop_instance
return wait_for_operation(project, zone, response["name"])
File "/workspace/modules/compute/instances.py", line 27, in wait_for_operation
operation=operation
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/googleapiclient/http.py", line 932, in execute
headers=self.headers,
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/googleapiclient/http.py", line 222, in _retry_request
raise exception
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/googleapiclient/http.py", line 191, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/google_auth_httplib2.py", line 225, in request
**kwargs
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/httplib2/__init__.py", line 1721, in request
conn, authority, uri, request_uri, method, body, headers, redirections, cachekey,
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/httplib2/__init__.py", line 1440, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/httplib2/__init__.py", line 1392, in _conn_request
response = conn.getresponse()
File "/layers/google.python.runtime/python/lib/python3.7/http/client.py", line 1373, in getresponse
response.begin()
File "/layers/google.python.runtime/python/lib/python3.7/http/client.py", line 319, in begin
version, status, reason = self._read_status()
File "/layers/google.python.runtime/python/lib/python3.7/http/client.py", line 280, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/layers/google.python.runtime/python/lib/python3.7/socket.py", line 589, in readinto
return self._sock.recv_into(b)
File "/layers/google.python.runtime/python/lib/python3.7/ssl.py", line 1071, in recv_into
return self.read(nbytes, buffer)
File "/layers/google.python.runtime/python/lib/python3.7/ssl.py", line 929, in read
return self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:2570)",
}

Unable to redirect variable in python with flask [duplicate]

This question already has answers here:
Flask view return error "View function did not return a response"
(3 answers)
Closed 11 months ago.
yesterday I started to learn Flask and got into a pitfall for redirection with a variable. I tried without session as well but can't get ahead.
Code is as below -
from flask import Flask, render_template, request, redirect, url_for, session
from flask_wtf import Form
from wtforms import StringField
app = Flask(__name__)
app.config['SECRET_KEY'] = 'our very hard to guess secretfir'
#app.route('/')
def index():
return render_template('index.html')
#app.route('/thank-you')
def thank_you():
if request.method == 'POST':
messages = request.args['translated_text'] # counterpart for url_for()
messages = session['translated_text'] # counterpart for session
return render_template('thank-you.html',messages=messages)
def translate_text(target, text):
import six
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
if isinstance(text, six.binary_type):
text = text.decode("utf-8")
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(text, target_language=target)
output_text = format(result["translatedText"])
return output_text
# approach using WTForms
class RegistrationForm(Form):
input_lang = StringField('Input Language in length(2) ==> ')
output_lang = StringField('Output Language in length(2) ==> ')
input_text = StringField('Input Text ==> ')
#app.route('/translate', methods=['GET', 'POST'])
def translate():
error = ""
form = RegistrationForm(request.form)
if request.method == 'POST':
input_lang = form.input_lang.data
output_lang = form.output_lang.data
input_text = form.input_text.data
if len(input_lang) != 2 or len(output_lang) != 2 or len(input_text) == 0:
error = "Please supply proper inputs! "
else:
translated_text = translate_text(output_lang, input_text)
session['translated_text'] = translated_text
return redirect(url_for('thank_you',transalted_text=translated_text))
return render_template('translate.html', form=form, message=error)
# Run the application
app.run(debug=True)
Whenever, I submit \translate.html, I get an error as :
127.0.0.1 - - [03/Apr/2022 13:35:04] "GET /thank-you?transalted_text=salut HTTP/1.1" 500 -
Traceback (most recent call last):
File "C:\Dev\Python\Python310\lib\site-packages\flask\app.py", line 2095, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Dev\Python\Python310\lib\site-packages\flask\app.py", line 2080, in wsgi_app
response = self.handle_exception(e)
File "C:\Dev\Python\Python310\lib\site-packages\flask\app.py", line 2077, in wsgi_app
response = self.full_dispatch_request()
File "C:\Dev\Python\Python310\lib\site-packages\flask\app.py", line 1526, in full_dispatch_request
return self.finalize_request(rv)
File "C:\Dev\Python\Python310\lib\site-packages\flask\app.py", line 1545, in finalize_request
response = self.make_response(rv)
File "C:\Dev\Python\Python310\lib\site-packages\flask\app.py", line 1701, in make_response
raise TypeError(
TypeError: The view function for 'thank_you' did not return a valid response. The function either returned None or ended without a return statement.
127.0.0.1 - - [03/Apr/2022 13:35:04] "GET /thank-you?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 304 -
127.0.0.1 - - [03/Apr/2022 13:35:04] "GET /thank-you?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 304 -
127.0.0.1 - - [03/Apr/2022 13:35:04] "GET /thank-you?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 304 -
TypeError: The view function for 'thank_you' did not return a valid response. The function either returned None or ended without a return statement.
If you look at the thank_you func, it only knows how to handle a POST request, but in case of GET is returning None
#app.route('/thank-you')
def thank_you():
if request.method == 'POST':
messages = request.args['translated_text'] # counterpart for url_for()
messages = session['translated_text'] # counterpart for session
return render_template('thank-you.html',messages=messages)
# move the logic for GET request here
return {'msg': 'example'} # I asume that you are working with flask 2.0
And now you are returning for a GET request.
And if you are on flask 2.0, you could also specify the http method in the app decorator. For more clarity:
#app.get('/thank-you')
def thank_you():
return 'Thank you'

Calling a flask application from djangocms

I have the following flask app that takes a 36 slider answers, from a form and outputs a simple multiplication result
flask:
from flask import Flask, render_template, request
import pandas as pd
import jinja2
app = Flask(__name__)
#app.route('/quest')
def index():
# Render the form. Nothing special needs to happen here.
return render_template('/aboutImgFree.html')
#app.route('/results', methods=['POST'])
def results():
if request.method == 'POST':
likertValues = {'C1': [request.values['vol1'], request.values['vol2'],request.values['vol3'], request.values['vol4'],
request.values['vol5'],request.values['vol6']],
'C2': [request.values['vol7'], request.values['vol8'], request.values['vol9'], request.values['vol10'],
request.values['vol11'], request.values['vol12']],
'C3': [request.values['vol13'], request.values['vol14'], request.values['vol15'], request.values['vol16']
, request.values['vol17'], request.values['vol18']],
'C4': [request.values['vol19'], request.values['vol20'], request.values['vol21'], request.values['vol22']
, request.values['vol23'], request.values['vol24']],
'C5': [request.values['vol25'], request.values['vol26'], request.values['vol27'], request.values['vol28']
, request.values['vol29'], request.values['vol30']],
'C6': [request.values['vol31'], request.values['vol32'], request.values['vol33'], request.values['vol34']
, request.values['vol35'], request.values['vol36']]}
io = pd.DataFrame(data=likertValues)
io_t = io.astype('int64')
factor={'factor':[1,2,3,4,5,6]}
f=pd.DataFrame(data=factor)
#return render_template('/results.html',io=io.to_html(),f=type(f.iloc[-1,-1]))
'''
io_table=pd.read_csv(request.files['io_table'])
io_t=io_table
factor=pd.read_csv(request.files['factor'])
f=factor
'''
mdl1=pd.DataFrame(io_t.values+f.values, columns=io_t.columns, index=io_t.columns)
mdl2=pd.DataFrame(io_t.values*f.values, columns=io_t.columns, index=io_t.columns)
c_sum1 = mdl1.sum(axis=0)
r_sum1 = mdl1.sum(axis=1)
c_sum2 = mdl2.sum(axis=0)
r_sum2 = mdl2.sum(axis=1)
mdl1_t=mdl1
mdl2_t=mdl2
io_t.rename(index={0:'C1',1:'C2',2:'C3',3:'C4',4:'C5',5:'C6'},inplace=True)
io_t.rename(columns={0:'C1',1:'C2',2:'C3',3:'C4',4:'C5',5:'C6'},inplace=True)
mdl1_t=mdl1_t.append(c_sum1, ignore_index=True)
mdl1_t.rename(index={0:'C1',1:'C2',2:'C3',3:'C4',4:'C5',5:'C6',6:'Sum Column'},inplace=True)
mdl1_t=pd.concat([mdl1_t, r_sum1], axis=1, ignore_index=True)
mdl1_t.iloc[-1,-1]=mdl1.values.sum()
mdl1_t.rename(columns={0:'C1',1:'C2',2:'C3',3:'C4',4:'C5',5:'C6',6:'Sum Row'},inplace=True)
mdl2_t=mdl2_t.append(c_sum2, ignore_index=True)
mdl2_t.rename(index={0:'C1',1:'C2',2:'C3',3:'C4',4:'C5',5:'C6',6:'Sum Column'},inplace=True)
mdl2_t=pd.concat([mdl2_t, r_sum2], axis=1, ignore_index=True)
mdl2_t.iloc[-1,-1]=mdl2.values.sum()
mdl2_t.rename(columns={0:'C1',1:'C2',2:'C3',3:'C4',4:'C5',5:'C6',6:'Sum Row'},inplace=True)
option = request.form.getlist('model')
if option == ['mdl1']:
return render_template('/results.html', io=io_t.to_html(), mdl1_t=mdl1_t.to_html())
elif option == ['mdl2']:
return render_template('/results.html', io=io_t.to_html(), mdl2_t=mdl2_t.to_html())
elif option == ['mdl1','mdl2']:
return render_template('/results.html', io=io_t.to_html(), mdl1_t=mdl1_t.to_html(), mdl2_t=mdl2_t.to_html())
else:
return render_template('/results.html', io=io_t.to_html())
#results = new_io_table(io_table,factor) # Get some results
if __name__ == "__main__":
app.run(debug=True)
What I want to do is take that code and call it from my djangocms application. Thing is that I can't find anywhere the answer and I don't know what exactly to look for. Is there anyone here that has done something similar?
I'm using django 3.x and flask 1.1.2
UPDATE
I found out about dispatching, which is really awesome, but I can't make it work correctly.
from flask import Flask
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.exceptions import NotFound
from app1.website2 import app as app1
from app2 import app as app2
app = Flask(__name__)
app.wsgi_app = DispatcherMiddleware(NotFound(), {
"/app1": app1,
"/quest": app2
})
if __name__ == "__main__":
app.run()
And I get the following error
Traceback (most recent call last):
File "C:\Users\user\Desktop\CMS\Lib\site-packages\werkzeug\serving.py", line 323, in run_wsgi
execute(self.server.app)
File "C:\Users\user\Desktop\CMS\Lib\site-packages\werkzeug\serving.py", line 312, in execute
application_iter = app(environ, start_response)
File "C:\Users\user\Desktop\CMS\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\user\Desktop\CMS\Lib\site-packages\werkzeug\middleware\dispatcher.py", line 66, in __call__
return app(environ, start_response)
TypeError: 'module' object is not callable

Cloud firestore exceptions not caught by except block

import datetime
from threading import Timer
import firebase_admin
from firebase_admin import firestore
import calendar
db = firestore.Client()
col_ref = db.collection(u'tblAssgin').get()
current_serving = [doc.id for doc in col_ref]
#print(current_serving)
def sit_time():
for i in current_serving:
try:
doc_ref = db.collection(u'tblAssgin').document(i)
except:
current_serving.remove(doc_ref)
else:
doc = doc_ref.get()
a = doc.get('assginTime')
assign_time = datetime.datetime.fromtimestamp(calendar.timegm(a.timetuple()))
now = datetime.datetime.now()
sitting_time = now - assign_time
hours,remainder = divmod(sitting_time.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
print('minutes:',minutes)
updates = {u'sitting_time':minutes}
doc_ref.update(updates)
t = None
def refresh():
global t
sit_time()
t = Timer(60, refresh)
t.daemon = True
t.start()
refresh()
So basically the above code does that it first fetches all the document id's of collection name 'tblAssgin' and store it in 'current_serving' list. Then, loops over each document and calculate time and runs again after every 60 sec. Now suppose I delete one document then that document will not be found. So I want to do that when the document is not found exception is raised and that document id gets's removed from 'current_serving' list. But the exception is not caught.
Please help
Thanks in advance..!!
You're assuming CollectionReference.document() will throw an exception if the document does not exist. It doesn't.
>>> client.collection('non-existing').document('also-non-existing')
<google.cloud.firestore_v1beta1.document.DocumentReference object at 0x10feac208>
But DocumentReference.get() will throw an exception if the document doesn't exist.
>>> client.collection('non-existing').document('also-non-existing').get()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "~/gae/lib/python3.6/site-packages/google/cloud/firestore_v1beta1/document.py", line 432, in get
raise exceptions.NotFound(self._document_path)
google.api_core.exceptions.NotFound: 404 ~/databases/(default)/documents/non-existing/also-non-existing

Exception in making a REST call

I am currently working on a REST call using Flask Framework. and I have run into some errors along the way, which I am unable to figure out as to why they occur (still trying though). The error is as shown below:
[2016-09-20 18:53:26,486] ERROR in app: Exception on /Recommend [GET]
Traceback (most recent call last):
File "/anaconda/lib/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/anaconda/lib/python2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/anaconda/lib/python2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/anaconda/lib/python2.7/site-packages/flask_api/app.py", line 97, in handle_user_exception
for typecheck, handler in chain(blueprint_handlers.items(), app_handlers.items()):
AttributeError: 'tuple' object has no attribute 'items'
127.0.0.1 - - [20/Sep/2016 18:53:26] "GET /Recommend HTTP/1.1" 500 -
here is the code that I have built:
from flask import request, jsonify
from flask_api import FlaskAPI
from flask_cors import CORS
from sets import Set
from collections import defaultdict
import itertools
import copy
app = FlaskAPI(__name__)
CORS(app)
content = None
class Apriori:
def __init__(self):
self.no_of_transactions = None
self.min_support = 0.5
self.min_confidence = 0.75
self.transactions = {}
self.set_of_items = set()
self.frequencies = {}
self.frequent_itemsets_of_order_n = {}
self.association_rules = {}
def createPowerSet(self,s):
powerset = set()
for i in xrange(2**len(s)):
subset = tuple([x for j,x in enumerate(s) if (i >> j) & 1])
if len(subset) == 0:
pass
elif len(subset) == 1:
powerset.add(subset[0])
else:
powerset.add(subset)
return powerset
def createFrequentItemSets(self,set_of_items,len):
frequent_itemsets = set(itertools.combinations(set_of_items, len))
for i in list(frequent_itemsets):
tempset = set(i)
self.frequencies[i] = 0
for k, v in self.transactions.iteritems():
if tempset.issubset(set(v)):
self.frequencies[i] += 1
if float(self.frequencies[i])/self.no_of_transactions < self.min_support:
frequent_itemsets.discard(i)
return frequent_itemsets
def mineAssociationRules(self,frequent_itemset):
s = set(frequent_itemset)
subs = list(self.createPowerSet(s))
for each in subs:
if sorted(tuple(set(each))) == sorted(tuple(s)):
continue
if len(set(each))==1:
antecedent = list(set(each))[0]
elif len(set(each))>1:
antecedent = tuple(set(each))
if len(s.difference(set(each)))==1:
consequent = list(s.difference(set(each)))[0]
elif len(s.difference(set(each)))>1:
consequent = tuple(s.difference(set(each)))
AuC = tuple(s)
if float(self.frequencies[AuC])/self.frequencies[antecedent] >= self.min_confidence:
if antecedent in self.association_rules:
pass
else:
if type(antecedent) is tuple:
antecedent = (",").join(antecedent)
if type(consequent) is tuple:
consequent = (",").join(consequent)
self.association_rules[antecedent] = consequent
def implement(self,transactions):
#for i in range(0,self.no_of_transactions):
for i in range(0,len(transactions)):
self.transactions["T"+str(i)] = defaultdict(list)
self.transactions["T"+str(i)] = transactions[i].split(',')
self.set_of_items = self.set_of_items.union(Set(self.transactions["T"+str(i)]))
for i in list(self.set_of_items):
self.frequencies[i] = 0
for k, v in self.transactions.iteritems():
if i in v:
self.frequencies[i] = self.frequencies[i] + 1
if float(self.frequencies[i])/self.no_of_transactions < self.min_support:
self.set_of_items.discard(i)
self.frequent_itemsets_of_order_n[1] = self.set_of_items
l = 1
reps = copy.deepcopy(self.set_of_items)
while True:
l += 1
result = self.createFrequentItemSets(self.set_of_items, l)
if len(result) == 0:
break
self.frequent_itemsets_of_order_n[l] = result
reps = copy.deepcopy(self.frequent_itemsets_of_order_n[l])
l = l-1
while l>2:
for each in self.frequent_itemsets_of_order_n[l]:
self.mineAssociationRules(each)
l = l-1
#app.route('/Recommend')
def FindAssociations():
transactions = ["A,C,D,F,G","A,B,C,D,F","C,D,E","A,D,F","A,C,D,E,F","B,C,D,E,F,G"]
apr = Apriori()
apr.implement(transactions)
return jsonify(rules=apr.association_rules)
if __name__ == "__main__":
app.run(port=5000)
I did run some sample code found on the web, and built the above script based on those scripts. They worked out well. The class I built was based on another python program that I had built earlier which worked well. Should I have imported the class from another script instead of building it here?
There are a number of errors in your Apriori which need to fixed. There are a few attempts to divide by self.no_of_transactions (e.g. line 87) which is initialised to None and never changed. Dividing by None raises an exception:
>>> 1/None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'int' and 'NoneType'
This exception is then handled in Flask-API's handle_user_exception() method, which also appears to have a bug as shown in the exception it raises.
The way to fix it is to correct your code so that it does not divide by None.

Categories

Resources