Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Replicating Stumbleupon website. What can I do after getting the iFrame loading urls?

I am building the website using Flask and I've got the iFrame to work properly loading the urls that are stored in a list (it works better than in PHP wohoo!). I was recommended to use Flask Session so the content won't get repeated but I got really confused, thought Session were only for logging in users. Could anyone explain this to me?

Here is the link: http://flask.pocoo.org/docs/0.10/api/#flask.session

Here is what I've done:

app.py

from flask import Flask, render_template
app = Flask(__name__)

import random

@app.route("/")
def index():
    urls = [
         'http://www.w3schools.com',
         'http://techcrunch.com/',
         'https://www.fayerwayer.com/',
    ]
    iframe = random.choice(urls)
    return render_template('index.html', iframe=iframe)

if __name__ == "__main__":
    app.run(debug=True)

templates/layout.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="/static/css/normalize.css"/>
    <link rel="stylesheet" type="text/css" href="/static/css/foundation.css"/>

</head>
<body>
    <nav class="top-bar">
        <h3 align="center"><a href="/">Stumble</a></h3>
    </nav>

    {% block content %} 
    {% endblock content %}

</body>
</html>

templates/index.html

{% extends 'layout.html' %}
{% block content %}
     <iframe frameborder='0' noresize='noresize' style='position: absolute; background: transparent; width: 100%; height:100%;' src="{{ iframe  }}" frameborder="0"></iframe>
 {% endblock content %}

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

The random.choice() will run uniquely for each visitor, so that's not an issue, Iain Simmons.

The session can be used to store pretty much anything you want. You could record what sites they've seen and, if the random choice is one they've already seen, pick a new random choice. If the list of choices is empty, tell them they've reached the end of the internet?

I'm not familiar with the Flask Session module, but maybe the random choice would be set only once upon running the Flask 'app', so each different user of your site would always get the same src attribute value for the iframe...

I'm not sure about that though, because I assumed the code in your index route would get run each time someone went to the corresponding URL...

Kenneth Love, any ideas?