Reputation: 76
So I've been using Google App Engine Webapp2 for years. Now Python 2 is deprecated and Webapp2 only works for Python 2. All my classes use are designed to be interpreted by webapp2. Instead of trying to change tens of thousands lines of code by changing all the classes for the pages to Flask I'm working on a simple fix. I just need when the class says self.response.write("whatever your writing") to compile all of that and return it to work with flask. The problem I'm running into is adding .write() I can do just self.response("whatever add") but I can't figure out how to make self.response.write("whatever") work. Any advice on how to add .write() and make it work would be greatly appreciated. Heres what I've done so far and this code works great but I still need to add .write().
from flask import Flask
from flask import request
app = Flask(__name__)
class BaseHandler():
def __init__(self):
self.data = ""
def response(self,stuffAdd):
self.data+=stuffAdd
class MainPage(BaseHandler):
def get(self):
self.response("first thing to add")
self.response("second thing to add")
@app.route('/',methods=['GET'])
def hello_world():
if request.method == 'GET':
newMain=MainPage()
newMain.get()
return newMain.data
Upvotes: 2
Views: 438
Reputation: 76
Thanks to Tom it works now. If anyone is trying to convert old Webapp2 classes to Flask here is the code to make it work with python 3. Your webapp2 syntax will work to use flask secure sessions, do redirects, do post requests, and to write on the page. That will handle most of what webapp2 does. Right now you will have to manually change your webapp2 handlers to the Flask handler at the bottom but I'm going to write a loop to iterate over that too and I'll post it here later.
from flask import Flask, session,request,redirect
app = Flask(__name__)
class Response:
def __init__(self, handler):
self.handler = handler
def write(self, stuffAdd):
self.handler.data += stuffAdd
class BaseHandler:
def __init__(self):
self.data = ""
self.ChangePage="None"
self.response = Response(self)
self.session = session
self.request= {}
def redirect(self,arg):
self.ChangePage=arg
class FirstPage(BaseHandler):
def get(self):
self.redirect("/SecondPage")
def post(self):
self.response.write("First Page Post")
class SecondPage(BaseHandler):
def get(self):
self.response.write("second page")
def post(self):
self.response.write("Second Page Post")
class MainPage(BaseHandler):
def get(self):
self.session["name"]="Some Name"
self.session["phone"]="8675309"
self.response.write("""
<html>
<head>
</head>
<body>
""")
self.response.write("first thing to add")
self.response.write("""
<form method="post" action="/">
<input type="text" name="name">
<input type="submit">
""")
self.response.write("""
</body>
</html>
""")
def post(self):
name=self.request.get('name')
self.response.write("""
<html>
<head>
</head>
<body>
""")
self.response.write("this is the new post page<br/><br/>I posted " + name)
SessionName=self.session.get("name")
SessionPhone=self.session.get("phone")
self.response.write("<br/><br/>my session name is " + SessionName)
self.response.write("<br/><br/>my session phone number is " + SessionPhone)
self.response.write("""
</body>
</html>
""")
app.secret_key = 'your-secret-key'
@app.route('/',methods=['GET','POST'])
def daMain():
if request.method == 'GET':
daPage=MainPage()
daPage.get()
if daPage.ChangePage=="None":
return daPage.data
else:
return redirect(daPage.ChangePage)
if request.method == 'POST':
darequests=request.form
daPage=MainPage()
daPage.request=darequests
daPage.post()
if daPage.ChangePage=="None":
return daPage.data
else:
return redirect(daPage.ChangePage)
@app.route('/FirstPage',methods=['GET','POST'])
def daFirst():
if request.method == 'GET':
darequests=request.form
daPage=FirstPage()
daPage.request=darequests
daPage.get()
if daPage.ChangePage=="None":
return daPage.data
else:
return redirect(daPage.ChangePage)
if request.method == 'POST':
darequests=request.form
daPage=FirstPage()
daPage.request=darequests
daPage.post()
if daPage.ChangePage=="None":
return daPage.data
else:
return redirect(daPage.ChangePage)
@app.route('/SecondPage',methods=['GET','POST'])
def daSecond():
if request.method == 'GET':
daPage=SecondPage()
daPage.get()
if daPage.ChangePage=="None":
return daPage.data
else:
return redirect(daPage.ChangePage)
if request.method == 'POST':
darequests=request.form
daPage=SecondPage()
daPage.request=darequests
daPage.post()
if daPage.ChangePage=="None":
return daPage.data
else:
return redirect(daPage.ChangePage)
Upvotes: 0
Reputation: 24052
I think this will do what you want:
class Response:
def __init__(self, handler):
self.handler = handler
def __call__(self, stuffAdd):
self.handler.data += stuffAdd
def write(self, arg):
# You can do whatever you want here
whatever_you_want(self.handler, arg)
class BaseHandler:
def __init__(self):
self.data = ""
self.response = Response(self)
Response
is a callable class, meaning you can call an instance of the class like a function, in which case the __call__
method is invoked.
I just used a stub whatever_you_want(self.handler, arg)
for write
, but you can obviously do whatever you want there.
Upvotes: 4