How To Make Flask’s Flash Message Appears Only Once After User Is Logged-In!

I have been using Flask to work on a personal website for a couple of days. A small snag got me stumped for a while but I finally figured it out. In Flask, you could use flash to alert signed-in users or visitors about their status on your website such as they’d logged in or whatever… One drawback about Flask’s flash message is that it doesn’t go away unless you refresh the browser. Secondly, in the case of an authenticated user, the flash message indicates he’d logged in could appear again and again every time he or she visited the same page.

To solve the problem of Flask’s flash message isn’t going away, I employed JQuery to make the flash message disappear after nine seconds or so. This is easily done by using JQuery, so I won’t go into the detail of how to do this. What I want to talk about is how to make flash messages only appear once after a user is authenticated and logged in.

First, you need to install Flask-Session. Once you got Flask-Session installed, you need to import like so:

from flask_session import Session
from flask import session

Before you can use Flask-Session, you need to configure it to work with Flask.

# Flask-Session
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
app.config['SESSION_USE_SIGNER'] = True
Session(app)

In a login section of your website/web app, you could do something like this after you execute a function or whatever you do to log the user in:

session['user_id'] = current_user.id
session['flash_session_page_visit'] = 'page_visit'
flash(f'{current_user.name} is logged in!', 'login message')

The session[‘user_id’] = current_user.id is a Python line of code in which you want to store a user object’s property — that you called from the database — into a session which you named as user_id. The next trick is to create a new session out of the thin air just for the purpose of using a negation on it later. For this purpose I’d created ‘page_visit’ session which is just a bogus string, and then I stored this inside session’s flash_session_page_visit. The next step is to create a session_manager_func() as follow:

def session_manager_func():
    if current_user.is_authenticated:
        if not session.get('user_id):
            logout_user()
        else:
            if not session.get('flash_session_page_visit'):
                flash(f'User Status:  {current_user.name} is logged in!', 'user authentication status')

Now you can use the method session_manager_func() inside any Flask’s route to display any flash message just once after a user is logged in. This trick works because you use the negation keyword not to turn flash_session_page_visit into false. Since Flask’s session will be true and be available as long the browser isn’t closed, then this trick negates this session as if it isn’t there at all — this makes the flash message won’t appear twice as long as the browser isn’t closed, reopened, and then logged in again.

The current_user is the object got created by Flask-Login package that I imported for my program. Using Flask-Login I’m able to refer to any logged-in user as current_user either inside my Python script or in Flask’s Jinja HTML page. This makes me use my time more efficiently since I don’t really have to query the database for verifying a logged-in user.

Anywho, if you want to try this, don’t forget to implement some sort of Javascript or JQuery to make the flash message goes away for how many seconds you want this to happen, OK? Hopefully, this little trick will be of use for you as it is for me. Thanks for reading.

XSLT Basics For Transforming One Specific XML Structure To Another XML Structure

I was messing around with R (i.e., a free program that can organize and transform data), and I told R to transform the data into CSV format.  I converted this CSV format file into XML file using one of those CSV to XML free tools on the Internet.  The problem is that the XML file I got from the whole process contains structure in which is radically different from the XML structure that I need to use with a software.  Luckily, XSLT comes to the rescue.  With XSLT, I discover that it’s possible to transform one XML structure to another XML structure.  The problem is that I have no knowledge of how to use XSLT just yet.  Once again, YouTube is a treasure trove of free information, and the YouTube video right after the break introduces me to XSLT basics.  Of course, I may have to do more study and research on XSLT before I would know how to properly transform one XML structure to another XML structure.

Pay Up, Learn How To Code, And Get A Job?

Treehouse is a new educational web service which promises to help guide whoever interests in developing the skill set in web design, web development and iOS development.  Besides promising in training whoever to become a developer in the specific technological areas, Treehouse devises a unique strategy for whoever wants to get hire in their industry.  Treehouse’s strategy is to allow whoever in their programs to take quizes/tests to unlock badges.  The unlocked badges would be presented on a Treehouse participant’s profile.  According to the available unlocked badges, employers would be able to theoretically have a better chance of knowing who might be a good candidate for them to set up an interview with.  Treehouse charges the participants with either $25 or $49 per month.  The $49 per month plan would include additional materials such as more tutorial videos.

In my opinion, nowadays it’s very easy to learn how to become a web designer, web developer, and iOS developer without having to pay a single dollar.  How?  iTunes U is the answer!  Computer users don’t have to use Mac to have access to iTunes U, because they can install iTunes on Windows machines.  After installing iTunes on Windows machines, users can open up iTunes and navigate to iTunes U to download videos that teach users how to write programs in different computer languages, developing iOS applications, designing websites, and a lot more.  iTunes U isn’t for just these specific areas of study, because iTunes U allows users to learn just about any available open course of different universities.  Don’t be fooled by having to navigate to iTunes Store before one could check up on iTunes U, because iTunes U is indeed free!  It’s free to download educational videos so one could watch it at any time.

With knowing that one might not have to pay to learn how to become a developer for the information industry, it might be hard for some people to imagine about paying for programs that provide by educational web services such as Treehouse.  Nonetheless, Treehouse looks attractive to some people, because they think Treehouse might be able to allow them to find jobs that they’re specialized in (e.g., web designing, web development, iOS development).  I surmise that some folks who already have the experiences in these technological areas might just join Treehouse to unlock badges so certain employers might come around and interview them.  This will do if one believes Treehouse will be able to attract enough attentions from various employers of big companies and so on.

I’m not sure Treehouse will definitely be a solution for whoever wants to get a job as web designer or web developer or iOS developer.  The ultimate decision will have to rest upon the shoulders of the employers from various companies in regarding to hire specific Treehouse participants.  Perhaps, Treehouse participants might get the exposures they need for getting interviews, but they still have to score the interviews before the employers hire them.  As I mentioned earlier, Treehouse isn’t providing unique learning materials, because one can just download the similar educational materials within iTunes U.  If iTunes U isn’t carrying certain educational materials, there might be other places and sources for one to acquire such educational materials.

Source:  http://techcrunch.com/2011/11/07/web-design-and-development-community-treehouse-wants-to-teach-you-how-to-code-get-you-a-job/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+
Techcrunch+%28TechCrunch%29