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')

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:

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:

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!'


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
>>> "8" * 4
>>> "hello" * 4

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

>>> 8 * 4

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:


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

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?