简体   繁体   中英

How to return for loop values without any html template in flask

How to return for loop values without any html template in flask, in the below code I need to get all jokes values having multiple jokes route but i want them to be displayed as a list one below the other, currently the output I am getting is as a whole list item, I am aware i can use jinja for this but here i want to do without creating any html page

from flask import Flask
import pyjokes

app = Flask(__name__)


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

    joke = pyjokes.get_joke()
    return f'<h2>{joke}</h2>'


@app.route("/MultipleJokes")
def jokes():
    jokes = pyjokes.get_jokes() # here jokes is a list item i need to show all item one below the other like for i in jokes print(i)
    return f'<li>{jokes}</li>'

Current Output

 ["Complaining about the lack of smoking shelters, the nicotine addicted Python programmers said there ought to be 'spaces for tabs'.", 'Ubuntu users are apt to get this joke.', 'Obfuscated Reality Mappers (ORMs) can be useful database tools.', 'Asked to explain Unicode during an interview, Geoff went into detail about his final year university project. He was not hired.', "Triumphantly, Beth removed Python 2.7 from her server in 2030. 'Finally!' she said with glee, only to see the announcement for Python 4.4.", "An SQL query goes into a bar, walks up to two tables and asks, 'Can I join you?'", 'When your hammer is C++, everything begins to look like a thumb.', 'If you put a million monkeys at a million keyboards, one of them will eventually write a Java program. The rest of them will write Perl.', 'To understand recursion you must first understand recursion.', "I suggested holding a 'Python Object Oriented Programming Seminar', but the acronym was unpopular.", "'Knock, knock.' 'Who's there?' ... very long pause ... 'Java.'", "How many programmers does it take to change a lightbulb? None, that's a hardware problem.", "What's the object-oriented way to become wealthy? Inheritance.", "Why don't jokes work in octal? Because 7 10 11.", 'How many programmers does it take to change a lightbulb? None, they just make darkness a standard.', "Two bytes meet. The first byte asks, 'Are you ill?' The second byte replies, 'No, just feeling a bit off.'", "Two threads walk into a bar. The barkeeper looks up and yells, 'Hey, I want don't any conditions race like time last!'", "Old C programmers don't die, they're just cast into void.", "Eight bytes walk into a bar. The bartender asks, 'Can I get you anything?' 'Yeah,' replies the bytes. 'Make us a double.'", "Why did the programmer quit his job? Because he didn't get arrays.", "Why do Java programmers have to wear glasses? Because they don't see sharp.", 'Software developers like to solve problems. If there are no problems handily available, they will create their own.', ".NET was named .NET so that it wouldn't show up in a Unix directory listing.", 'Hardware: The part of a computer that you can kick.', "A programmer was found dead in the shower. Next to their body was a bottle of shampoo with the instructions 'Lather, Rinse and Repeat'.", 'Optimist: The glass is half full. Pessimist: The glass is half empty. Programmer: The glass is twice as large as necessary.', 'In C we had to code our own bugs. In C++ we can inherit them.', 'How come there is no obfuscated Perl contest? Because everyone would win.', "If you play a Windows CD backwards, you'll hear satanic chanting ... worse still, if you play it forwards, it installs Windows.", 'How many programmers does it take to kill a cockroach? Two: one holds, the other installs Windows on it.', 'What do you call a programmer from Finland? Nerdic.', "What did the Java code say to the C code? A: You've got no class.", "Why did Microsoft name their search engine BING? Because It's Not Google.", "Pirates go 'arg!', computer pirates go 'argv!'", 'Software salesmen and used-car salesmen differ in that the latter know when they are lying.', "Child: Dad, why does the sun rise in the east and set in the west? Dad: Son, it's working, don't touch it.", 'Why do programmers confuse Halloween with Christmas? Because OCT 31 == DEC 25.', 'How many Prolog programmers does it take to change a lightbulb? false.', 'Real programmers can write assembly code in any language.', 'Waiter: Would you like coffee or tea? Programmer: Yes.', 'What do you get when you cross a cat and a dog? Cat dog sin theta.', "If loving you is ROM I don't wanna read write.", 'A programmer walks into a foo...', "A programmer walks into a bar and orders 1.38 root beers. The bartender informs her it's a root beer float. She says 'Make it a double!'", "What is Benoit B. Mandelbrot's middle name? Benoit B. Mandelbrot.", "Why are you always smiling? That's just my... regular expression.", 'ASCII stupid question, get a stupid ANSI.', "A programmer had a problem. He thought to himself, 'I know, I'll solve it with threads!'. has Now problems. two he", 'Why do sin and tan work? Just cos.', 'Java: Write once, run away.', 'I would tell you a joke about UDP, but you would never get it.', 'A QA engineer walks into a bar. Runs into a bar. Crawls into a bar. Dances into a bar. Tiptoes into a bar. Rams a bar. Jumps into a bar.', "My friend's in a band called '1023 Megabytes'... They haven't got a gig yet!", "I had a problem so I thought I'd use Java. Now I have a ProblemFactory.", 'QA Engineer walks into a bar. Orders a beer. Orders 0 beers. Orders 999999999 beers. Orders a lizard. Orders -1 beers. Orders a sfdeljknesv.', 'A product manager walks into a bar, asks for drink. Bartender says no, but will consider adding later.', 'How do you generate a random string? Put a first year Computer Science student in Vim and ask them to save and exit.', "I've been using Vim for a long time now, mainly because I can't figure out how to exit.", "How do you know whether a person is a Vim user? Don't worry, they'll tell you.", "Waiter: He's choking! Is anyone a doctor? Programmer: I'm a Vim user.", "3 Database Admins walked into a NoSQL bar. A little while later they walked out because they couldn't find a table.", 'How to explain the movie Inception to a programmer? When you run a VM inside another VM, inside another VM ... everything runs real slow!', 'What do you call a parrot that says "Squawk! Pieces of nine! Pieces of nine!"? A parrot-ey error.', 'There are only two hard problems in Computer Science: cache invalidation, naming things and off-by-one-errors.', "There are 10 types of people: those who understand binary and those who don't.", 'There are 2 types of people: those who can extrapolate from incomplete data sets...', "There are II types of people: Those who understand Roman Numerals and those who don't.", 'There are 10 types of people: those who understand hexadecimal and 15 others.', "There are 10 types of people: those who understand binary, those who don't, and those who were expecting this joke to be in trinary.", "There are 10 types of people: those who understand trinary, those who don't, and those who have never heard of it.", 'What do you call eight hobbits? A hobbyte.', 'The best thing about a Boolean is even if you are wrong, you are only off by a bit.', 'A good programmer is someone who always looks both ways before crossing a one-way street.', 'There are two ways to write error-free programs; only the third one works.', 'QAs consist of 55% water, 30% blood and 15% Jira tickets.', 'Sympathy for the Devil is really just about being nice to QAs.', "How many QAs does it take to change a lightbulb? They noticed that the room was dark. They don't fix problems, they find them.", 'A programmer crashes a car at the bottom of a hill, a bystander asks what happened, he says "No idea. Let\'s push it back up and try again".', "What do you mean 911 is only for emergencies? I've got a merge conflict.", "Writing PHP is like peeing in the swimming pool, everyone did it, but we don't need to bring it up in public.", "Why did the QA cross the road? To ruin everyone's day.", 'Number of days since I have encountered an array index error: -1.', 'Number of days since I have encountered an off-by-one error: 0.', 'Speed dating is useless. 5 minutes is not enough to properly explain the benefits of the Unix philosophy.', 'Microsoft hold a bi-monthly internal "productive week" where they use Google instead of Bing.', "Schrodinger's attitude to web development: If I don't look at it in Internet Explorer then there's a chance it looks fine.", 'Finding a good PHP developer is like looking for a needle in a haystack. Or is it a hackstack in a needle?', "Unix is user friendly. It's just very particular about who its friends are.", 'A COBOL programmer makes millions with Y2K remediation and decides to get cryogenically frozen. "The year is 9999. You know COBOL, right?"', 'The C language combines all the power of assembly language with all the ease-of-use of assembly language.', 'An SEO expert walks into a bar, bars, pub, public house, Irish pub, tavern, bartender, beer, liquor, wine, alcohol, spirits...', "What does 'Emacs' stand for? 'Exclusively used by middle aged computer scientists.'", 'What does pyjokes have in common with Adobe Flash? It gets updated all the time, but never gets any better.', "Why does Waldo only wear stripes? Because he doesn't want to be spotted.", 'I went to a street where the houses were numbered 8k, 16k, 32k, 64k, 128k, 256k and 512k. It was a trip down Memory Lane.', "!false, (It's funny because it's true)", "['hip', 'hip'] (hip hip array!)"]

Expected Output

Ubuntu users are apt to get this joke.
Obfuscated Reality Mappers (ORMs) can be useful database tools.
Asked to explain Unicode during an interview, Geoff went into detail about his final year university project. He was not hired. 
Triumphantly, Beth removed Python 2.7 from her server in 2030. 'Finally!' she said with glee, only to see the announcement for Python 4.4.
An SQL query goes into a bar, walks up to two tables and asks, 'Can I join you?'
When your hammer is C++, everything begins to look like a thumb.
If you put a million monkeys at a million keyboards, one of them will eventually write a Java program. The rest of them will write Perl.
To understand recursion you must first understand recursion.
I suggested holding a 'Python Object Oriented Programming Seminar', but the acronym was unpopular.
'Knock, knock.' 'Who's there?' ... very long pause ... 'Java.'
How many programmers does it take to change a lightbulb? None, that's a hardware problem.
What's the object-oriented way to become wealthy? Inheritance.
Why don't jokes work in octal? Because 7 10 11.
How many programmers does it take to change a lightbulb? None, they just make darkness a standard.
Two bytes meet. The first byte asks, 'Are you ill?' The second byte replies, 'No, just feeling a bit off.'
Two threads walk into a bar. The barkeeper looks up and yells, 'Hey, I want don't any conditions race like time last!'
Old C programmers don't die, they're just cast into void.
Eight bytes walk into a bar. The bartender asks, 'Can I get you anything?' 'Yeah,' replies the bytes. 'Make us a double.'       
Why did the programmer quit his job? Because he didn't get arrays.
Why do Java programmers have to wear glasses? Because they don't see sharp.
Software developers like to solve problems. If there are no problems handily available, they will create their own.
.NET was named .NET so that it wouldn't show up in a Unix directory listing.
Hardware: The part of a computer that you can kick.
A programmer was found dead in the shower. Next to their body was a bottle of shampoo with the instructions 'Lather, Rinse and Repeat'.
Optimist: The glass is half full. Pessimist: The glass is half empty. Programmer: The glass is twice as large as necessary.     
In C we had to code our own bugs. In C++ we can inherit them.
How come there is no obfuscated Perl contest? Because everyone would win.
If you play a Windows CD backwards, you'll hear satanic chanting ... worse still, if you play it forwards, it installs Windows. 
How many programmers does it take to kill a cockroach? Two: one holds, the other installs Windows on it.
What do you call a programmer from Finland? Nerdic.
What did the Java code say to the C code? A: You've got no class.
Why did Microsoft name their search engine BING? Because It's Not Google.
Pirates go 'arg!', computer pirates go 'argv!'
Software salesmen and used-car salesmen differ in that the latter know when they are lying.
Child: Dad, why does the sun rise in the east and set in the west? Dad: Son, it's working, don't touch it.
Why do programmers confuse Halloween with Christmas? Because OCT 31 == DEC 25.
How many Prolog programmers does it take to change a lightbulb? false.
Real programmers can write assembly code in any language.
Waiter: Would you like coffee or tea? Programmer: Yes.
What do you get when you cross a cat and a dog? Cat dog sin theta.
If loving you is ROM I don't wanna read write.
A programmer walks into a foo...
A programmer walks into a bar and orders 1.38 root beers. The bartender informs her it's a root beer float. She says 'Make it a 
double!'
What is Benoit B. Mandelbrot's middle name? Benoit B. Mandelbrot.
Why are you always smiling? That's just my... regular expression.
ASCII stupid question, get a stupid ANSI.
A programmer had a problem. He thought to himself, 'I know, I'll solve it with threads!'. has Now problems. two he
Why do sin and tan work? Just cos.
Java: Write once, run away.
I would tell you a joke about UDP, but you would never get it.
A QA engineer walks into a bar. Runs into a bar. Crawls into a bar. Dances into a bar. Tiptoes into a bar. Rams a bar. Jumps into a bar.
My friend's in a band called '1023 Megabytes'... They haven't got a gig yet!
I had a problem so I thought I'd use Java. Now I have a ProblemFactory.
QA Engineer walks into a bar. Orders a beer. Orders 0 beers. Orders 999999999 beers. Orders a lizard. Orders -1 beers. Orders a 
sfdeljknesv.
A product manager walks into a bar, asks for drink. Bartender says no, but will consider adding later.
How do you generate a random string? Put a first year Computer Science student in Vim and ask them to save and exit.
I've been using Vim for a long time now, mainly because I can't figure out how to exit.
How do you know whether a person is a Vim user? Don't worry, they'll tell you.
Waiter: He's choking! Is anyone a doctor? Programmer: I'm a Vim user.
3 Database Admins walked into a NoSQL bar. A little while later they walked out because they couldn't find a table.
How to explain the movie Inception to a programmer? When you run a VM inside another VM, inside another VM ... everything runs real slow!
What do you call a parrot that says "Squawk! Pieces of nine! Pieces of nine!"? A parrot-ey error.
There are only two hard problems in Computer Science: cache invalidation, naming things and off-by-one-errors.
There are 10 types of people: those who understand binary and those who don't.
There are 2 types of people: those who can extrapolate from incomplete data sets...
There are II types of people: Those who understand Roman Numerals and those who don't.
There are 10 types of people: those who understand hexadecimal and 15 others.
There are 10 types of people: those who understand binary, those who don't, and those who were expecting this joke to be in trinary.
There are 10 types of people: those who understand trinary, those who don't, and those who have never heard of it.
What do you call eight hobbits? A hobbyte.
The best thing about a Boolean is even if you are wrong, you are only off by a bit.
A good programmer is someone who always looks both ways before crossing a one-way street.
There are two ways to write error-free programs; only the third one works.
QAs consist of 55% water, 30% blood and 15% Jira tickets.
Sympathy for the Devil is really just about being nice to QAs.
How many QAs does it take to change a lightbulb? They noticed that the room was dark. They don't fix problems, they find them.  
A programmer crashes a car at the bottom of a hill, a bystander asks what happened, he says "No idea. Let's push it back up and 
try again".
What do you mean 911 is only for emergencies? I've got a merge conflict.
Writing PHP is like peeing in the swimming pool, everyone did it, but we don't need to bring it up in public.
Why did the QA cross the road? To ruin everyone's day.
Number of days since I have encountered an array index error: -1.
Number of days since I have encountered an off-by-one error: 0.
Speed dating is useless. 5 minutes is not enough to properly explain the benefits of the Unix philosophy.
Microsoft hold a bi-monthly internal "productive week" where they use Google instead of Bing.
Schrodinger's attitude to web development: If I don't look at it in Internet Explorer then there's a chance it looks fine.      
Finding a good PHP developer is like looking for a needle in a haystack. Or is it a hackstack in a needle?
Unix is user friendly. It's just very particular about who its friends are.
A COBOL programmer makes millions with Y2K remediation and decides to get cryogenically frozen. "The year is 9999. You know COBOL, right?"
The C language combines all the power of assembly language with all the ease-of-use of assembly language.
An SEO expert walks into a bar, bars, pub, public house, Irish pub, tavern, bartender, beer, liquor, wine, alcohol, spirits...  
What does 'Emacs' stand for? 'Exclusively used by middle aged computer scientists.'
What does pyjokes have in common with Adobe Flash? It gets updated all the time, but never gets any better.
Why does Waldo only wear stripes? Because he doesn't want to be spotted.
I went to a street where the houses were numbered 8k, 16k, 32k, 64k, 128k, 256k and 512k. It was a trip down Memory Lane.

you can use this function, adding a <br/> separator between each joke:

def jokes():
    jokes = pyjokes.get_jokes()
    jokes_final = "<br/>".join(jokes)
    return f'{jokes_final}'

or, you can use the render_template , pass the jokes list to it, and make a jinja loop to print each joke - better solution if you have complex html markup to add to the output

One way to do this would be to use jinja2's Template feature:

from flask import Flask
import pyjokes
from jinja2 import Template

app = Flask(__name__)

@app.route("/MultipleJokes")
def jokes():
    jokes = pyjokes.get_jokes()
    joke_template = Template("{% for joke in jokes %}<li>{{joke}}</li>{% endfor %}")
    return joke_template.render(jokes=jokes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM