Forums

How to return two functions in one app route? - Flask

I want to return two functions at the same time. for example, in regular Python idle: (not including '@app.route('list3')

@app.route('/list3')
def list3():
    a = [1,2,3,4,5,6,7,8]
    b = str(a[::-1])
    return b

def hello2():
    return 'hello World!'

def double():
    return list3(), hello2()

this returns: ('[8, 7, 6, 5, 4, 3, 2, 1]', 'hello World!') I want this to happen in the Flask url but I only get '('[8, 7, 6, 5, 4, 3, 2, 1])'

how to add the text to the string function or vice versa?

Don't use Flask at 3 AM

There is no point in that code where you are calling the double function, so there is no way for it to appear as the return value of the Flask view.

Don't reply at 3 AM

ok, I've tried it another way, as would work in the Python idle:

@app.route('/list3')
def list3():
    a = [1,2,3,4,5,6,7,8]
    b = str(a[::-1])
    return b , 'hello world!'

however, this just returns the reversed list, but without 'hello world' text - tried setting the 'hello world' as an html file then calling it with render_template funciton, however it still does not return the list and text on the same url page

so, how to get them to both appear at the same time? cheers

What you're technically doing in your code is implicitly returning a two-element tuple containing the reversed list as its first element, then the string "hello world!" as its second.

Now, what Flask views ideally should return is an HTTP response object, which contains all of the information that should be sent back to the browser -- the contents of the page, but also various headers that are important for telling the browser how to display those contents. Flask has some clever code built in to convert return values that are not that kind of object into proper HTTP responses.

You can see from that link that what it does when it gets a tuple is not what you want.

However, what you actually want (I think :-) is to display a page that has text containing both the list and the string "hello world!". The easiest way to do that is to take advantage of the fact that Flask's conversion code works really well with strings: you just create a string that contains the exact page body that you want, like this:

@app.route('/list3')
def list3():
    a = [1,2,3,4,5,6,7,8]
    b = str(a[::-1])
    return "{} hello world!".format(b)

The last line contains a Python string format expression so you can customise that easily to add more text, or to put different non-string things into the returned text.

great , thanks - that's done the trick!

Will read through all that info as well

You could also use this:

return f'{} hello world!'

ITYM

return f'{b} hello world!'

...?

ok thanks

how about this code - I want it to return (80, 160) but just get 8888888888 - it only converts the first line?

def my_function(x):
  a = 10 * x
  b = 20 * x
  return a, b

Are you calling it with the string "8" rather than the number 8?

In Python, multiply a string by a number repeats that string, so

>>> "a" * 4
'aaaa'
>>> "8" * 4
'8888'
>>> "hello" * 4
'hellohellohellohello'

Whereas multiplying a number by a number works as you would expect:

>>> 8 * 4
32

If the value that you're passing to my_function is coming from a request in a Flask app, then it will be a string, so you need to convert it to an integer before calling the function:

my_function(int(something))

ok, I've worked this one out now, but what I'm really getting at is how to have two comepletely different functions displaying their output on the same page without having to merge them together using the 'format' system

is this possible?

No, you can't do that. But you can make two completely different people make fun of you.

for example (in flask_app.py):

@app.route('/example')
def htmllink():
    return 'This is the example <br />  HTML page! and TEST PAGE ' '{:+d}'.format(42)

def new():
    return 'add new stuff here....'

so I'ld like def htmllink and def new to display their output on the same url but without joining them

what if the two functions can't be joined?

How do you want the result to look like in your browser?

just basically one output above the other

:::python return f'{value_1}<br/>{value_2}'

sure, i get that, but I want to return two functions separately

can that be done?

No. In its normal form. a Flask view expects just one return value -- the string to return to the browser. That's just how HTTP works -- a browser makes a request to your site, and returns a response. So if you want to display two things in that page that comprises that response, you need to format them into a string,

There are more complicated ways to set them up -- for example, a view that was accessed via JavaScript code might make a request and receive a JSON-encoded response that contained multiple items, but even then, what would be happening is that the server-side code would be converting those items into a single specially-formatted string, and then the JavaScript would be decoding that to extract the different objects.

Perhaps you could give some more details about what what you're trying to do by returning two objects and why you think that doing a string format won't achieve that aim?

@MadMartin: Why can't I do this?

What he's trying to do:

lol

I'm starting to think that @MadMartin is actually @johnobama

no worries, I'm not that other poster

anway think I get it now, it can't be done - so just have to learn all the formatting rules now

no worries, I'm not that other poster

Vsauce: Or am I?

@MadMartin Why are there pictures of horses on your website?

My website is so much better

Please do not post on the forums at 3 AM.

I@m gonna check out your webite now

horses, it's a hobby - horseracing etc

Make sure to try out the love calculator

I got 28% on your 'love calculator'

Pertius and Nisa

MadMartin and Nisa only gets 27%

not sure why he gets 1% less?

This is the function that calculates the percent:

import random


def calc_percent(person, crush):
    random.seed(person + crush)
    return random.randrange(101)

I'm having probs trying to get this working, any ideas?

 return render_template('base.html'), '{}'.format(processed_text)

You're trying to return two values again...

enter image description here

Pertius and Nisa - 79%

yet MadMartin and Nisa seems to get fixed at 27% still

latest offering not quite working:

return render_template('index.html', processed_text=processed_text)

you see I want 'processed_text' to appear on the index.html page (the one with the horses) at the same time

You need to put {{ processed_text }} on the template where you want it to appear, and make sure you reload the web app and check for typos

hey, great, thanks - that's done the trick!

check out the masterpiece: http://madmartin.pythonanywhere.com/form2

Glad you got it working!

@dull, I love that bear pic :-)