Re: python: server is not receiving input from client flask

2021-06-28 Thread Chris Angelico
On Tue, Jun 29, 2021 at 5:43 AM Jerry Thefilmmaker
 wrote:
>
> Do you mind elaborating a bit more on making one function for any given 
> request?
>
> As far as defining a bunch of functions that get called when particular 
> requests come in I thought that's what I had going on in my codes. No?
>
> Also thank you, on making use of the print statement. Thought once I crossed 
> over to the web side there was no more need for it. But what you said makes 
> sense in terms of debugging, because sometimes I couldn't tell whether the 
> client's variable was getting pass to my function which caused it not to be 
> triggered.
>
> Thank you!
>

Sure. I'll take a step back and look quickly at the way HTTP works and
how Flask figures out what function to call; you probably know all
this but it'll make the explanation easier.

Every HTTP request has a method (GET, POST, PUT, DELETE, etc) and a
request URI. Some of the URI just figures out whether it goes to your
server or not, and the rest is which page within your server. So if
you're running on, say, port 5000 on the same computer the web browser
is on, then you can go to http://localhost:5000/any/path/here and
it'll be handled by your web app. Since the "http://localhost:5000";
part is the way to find the whole server, Flask just looks at the rest
of it - "/any/path/here" - to do its routing. So we have two parts - a
path and a method - that define which function is called.

When you point your browser at "http://localhost:5000/";, that's a GET
request (normal web page fetches are GET requests) with a path of "/".

When you submit the form, that's a POST request (because the form's
method) with a path of "/" (because that's the form's action). I'm
assuming here that nothing is getting in the way of that, but that's
what printing stuff out can help with.

So far, so good. I'm pretty sure none of what I just said is news to
you, but let's focus on those two requests: "GET /" and "POST /".

The app.route decorator lets you associate a function with some
path/method combination. What you have in the code you shared is this:

> @app.route("/", methods = ['POST', 'GET'])
> def play():
>
> @app.route("/", methods = ['POST', 'GET'])
> def check_answer(ans, user):

That tells Flask that the play function should be associated with
"POST /" and "GET /". And then it tells Flask that the check_answer
function should be associated with... the same two routes.

I suspect that what you actually intend is for one of those functions
to just handle GET, and the other to just handle POST. But another way
you could fix this would be to change the URL used as the form's
action. It's up to you and what your plans are for the rest of this
app.

(My recommendation is to start out with each function handling just
one route - say, "GET /" for play() and "POST /" for check_answer -
and only combine them (with multiple keywords in methods=[...]) once
you find that the two functions are duplicating a lot of code.)

Once that's sorted out, you'll want to figure out exactly what
parameters your functions take. As a general rule, the function's
parameters will match the placeholders in the route, so if you're not
using placeholders, you'll probably just want them all to take no
parameters. Here are some examples from a project of mine:

@app.route("/login")
def login():

@app.route("/force/")
def force_timer(id):

@app.route("/")
@app.route("/editor/")
def mainpage(channelid=None):

In the case of mainpage, it handles both "GET /" and "GET
/editor/12345", so the function might get a parameter and might not -
hence it takes a parameter with a default. For what you're doing here,
there are no placeholders in your routes, so all your routed functions
will take no args (like login in my example - it only ever handles
"GET /login", so it doesn't need anything else).

Hope that helps!

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python: server is not receiving input from client flask

2021-06-28 Thread Jerry Thefilmmaker
On Monday, June 28, 2021 at 2:41:23 PM UTC-4, Chris Angelico wrote:
> On Tue, Jun 29, 2021 at 4:36 AM Jerry Thefilmmaker 
>  wrote: 
> > @app.route("/", methods = ['POST', 'GET']) 
> > def play(): 
> >
> > @app.route("/", methods = ['POST', 'GET']) 
> > def check_answer(ans, user):
> When you're building a Flask app, you're defining a bunch of functions 
> that get called when particular requests come in. There needs to be 
> exactly *one* function for any given request. I would actually 
> recommend making the GET and POST endpoints completely separate here, 
> and designing them in independent functions. 
> 
> One extremely helpful technique here: "If In Doubt, Print It Out". 
> Event-driven code can be hard to debug, so don't be afraid to make 
> extensive use of print() calls to examine various aspects of the 
> incoming request. Even just a simple thing like print("check_answer 
> called") can tell you all kinds of things. 
> 
> HTTP is stateless by nature. You will have to think about each request 
> independently, and figure out how to carry the information you need. 
> Play around with it, have fun, and see what you can learn! 
> 
> ChrisA

Do you mind elaborating a bit more on making one function for any given request?

As far as defining a bunch of functions that get called when particular 
requests come in I thought that's what I had going on in my codes. No? 

Also thank you, on making use of the print statement. Thought once I crossed 
over to the web side there was no more need for it. But what you said makes 
sense in terms of debugging, because sometimes I couldn't tell whether the 
client's variable was getting pass to my function which caused it not to be 
triggered.

Thank you!
-- 
https://mail.python.org/mailman/listinfo/python-list


[RELEASE] Python 3.9.6, 3.8.11, 3.7.11, and 3.6.14 are now available

2021-06-28 Thread Łukasz Langa
Python 3.9.6

Get it here: https://www.python.org/downloads/release/python-396/ 

Python 3.9.6 is the newest major stable release of the Python programming 
language, and it contains many new features and optimizations. There’s been 146 
commits since 3.9.5 which is a similar amount compared to 3.8 at the same stage 
of the release cycle. See the change log 
 for details.

On macOS, we encourage you to use the universal2 binary installer variant 
whenever possible. The legacy 10.9+ Intel-only variant will not be provided for 
Python 3.10 and the universal2 variant will become the default download for 
future 3.9.x releases. You may need to upgrade third-party components, like 
pip, to later versions once they are released. You may experience differences 
in behavior in IDLE and other Tk-based applications due to using the newer 
version of Tk. As always, if you encounter problems when using this installer 
variant, please check https://bugs.python.org  for 
existing reports and for opening new issues.

The next Python 3.9 maintenance release will be 3.9.7, currently scheduled for 
2021-08-30.

The First Security-Only Release of Python 3.8

Get it here: https://www.python.org/downloads/release/python-3811/ 

Security content in this release contains three fixes. There’s also two fixes 
for 3.8.10 regressions. Take a look at the change log 
 for details.

According to the release calendar specified in PEP 569 
, Python 3.8 is now in security 
fixes only stage of its life cycle: 3.8 branch only accepts security fixes and 
releases of those are made irregularly in source-only form until October 2024. 
Python 3.8 isn’t receiving regular bugfixes anymore, and binary installers are 
no longer provided for it. Python 3.8.10 was the last full bugfix release of 
Python 3.8 with binary installers.

Security releases of 3.7.11 and 3.6.14

Get them here:
https://www.python.org/downloads/release/python-3711/ 

https://www.python.org/downloads/release/python-3614/ 

Security content in those releases contains five fixes each. Check out the 
relevant change logs for 3.7.11 
 and 3.6.14 
 for details.

Similarly to 3.8, Python 3.7 and 3.6 are now in security fixes only stage of 
their life cycle. Python 3.7 will be providing them until June 2023 while 
Python 3.6 ends its life in December 2021.

We hope you enjoy the new releases

Your friendly release team,
Ned Deily @nad 
Steve Dower @steve.dower 
Łukasz Langa @ambv 


signature.asc
Description: Message signed with OpenPGP
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python: server is not receiving input from client flask

2021-06-28 Thread Chris Angelico
On Tue, Jun 29, 2021 at 4:36 AM Jerry Thefilmmaker
 wrote:
> @app.route("/", methods = ['POST', 'GET'])
> def play():
>
> @app.route("/", methods = ['POST', 'GET'])
> def check_answer(ans, user):

When you're building a Flask app, you're defining a bunch of functions
that get called when particular requests come in. There needs to be
exactly *one* function for any given request. I would actually
recommend making the GET and POST endpoints completely separate here,
and designing them in independent functions.

One extremely helpful technique here: "If In Doubt, Print It Out".
Event-driven code can be hard to debug, so don't be afraid to make
extensive use of print() calls to examine various aspects of the
incoming request. Even just a simple thing like print("check_answer
called") can tell you all kinds of things.

HTTP is stateless by nature. You will have to think about each request
independently, and figure out how to carry the information you need.
Play around with it, have fun, and see what you can learn!

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


python: server is not receiving input from client flask

2021-06-28 Thread Jerry Thefilmmaker
I am new at python but newer at flask, and I'm trying to deploy a web app where 
I:
1. Send the user the question
2. Get the answer from the user
3. Send additional information based on the answer

I am able to send the question but stuck at getting the answer from the user. 
Base on the answer from the user the server returns a specific answer from a 
list of dict.

Here's my html:




THE QUIZ



QUIZ GAME


Welcome


Would you like to play


Enter your answer: 
 





Here's my python code:
@app.route("/", methods = ['POST', 'GET']) 
def play():

qa = questions.qa #list of dict with questions
user1, user2 = [], [] #containers for each player's answers

if request.method == 'POST':
answers = request.form.get('answers')
random.shuffle(questions.qa)
response = quiz(qa, user1)

return response 

def quiz(qa, user):
for rand_q in qa:

i = rand_q['question']
i2 = check_answer(i, user)

#print(rand_q["question"])
return i2

@app.route("/", methods = ['POST', 'GET'])
def check_answer(ans, user):
if request.method == 'POST':
answers = request.form.get('answers')
if 'yes' in answers:
user.append('yes')
i3 = ans.get(answers, '')#if the client answers "yes" to the 
question this part iterates over the part corresponds to yes in the list of dict

return i3 #this should send 2nd part of question/rand_q

Can anyone help me see what I'm doing wrong, and how do I pass the 2nd answer 
from my list of dict to the client. Thanks!
-- 
https://mail.python.org/mailman/listinfo/python-list


Syntactic sugar

2021-06-28 Thread Michael F. Stemper

I often want to execute a chunk of code n times

for iter in range(n):
  chunkofcode

Sometimes, the chunk of code doesn't care about which iteration
it's on. A week or two back, I would have sworn that I came across
a syntax for the above that eliminates the iteration counter.

This morning, I had use for that syntax, but couldn't find it
on-line or guess what it was. Does this syntax exist, or am I
simply imagining things?

Thanks,
--
Michael F. Stemper
There's no "me" in "team". There's no "us" in "team", either.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Syntactic sugar

2021-06-28 Thread Michael F. Stemper

On 28/06/2021 11.57, Stefan Ram wrote:

"Michael F. Stemper"  writes:

for iter in range(n):
   chunkofcode


   When the counter does not matter, people sometimes write "_":

for _ in range( n ):
 chunkofcode


That looks like what I wanted.

Thanks!

--
Michael F. Stemper
Indians scattered on dawn's highway bleeding;
Ghosts crowd the young child's fragile eggshell mind.
--
https://mail.python.org/mailman/listinfo/python-list