I like Flask Framework For Web Application – Python Programming

I think Flask is an amazing Python framework because it is versatile and flexible. Although there is a drawback to using Flask instead of Django such as with Flask you have to implement many features that would come out of the box with Django. For example, with Django, you get a really good admin panel without any effort, but with Flask you have to write the whole admin panel yourself using Flask Security or Flask Login with Flask Admin — these packages together can help you to be able to code a completed admin panel that allows an administrator to do backend stuff and maintain the web application. With Django, the admin panel that comes out of the box looks very nice. Somehow though, perhaps I could be wrong but I’ve found Django’s admin panel, even with extensive personal customization, a heavy custom Django admin panel is still looking too similar to the one that comes out of the box. With Flask, since you have to code this yourself and depending on how much time and effort you put into the admin panel, it could look completely alien from one programmer’s Flask admin panel to the next.

I have learned Python for a couple of months or so, and I have found Flask is easier to work with than Django — I fondly remembered I started with Django first — and I had a hard time with making Django’s routing of internal paths to the way I like it. With Flask though, it’s simple as:

@app.route('/')
def home_page():
    custom code goes here
    return render_template('index.html', some_variable_to_pass_into_Jinja2_template=whatever)

The piece of code above would render an index.html page for a website using Flask. The decorator @app refers to the Flask app itself, and the route part is where the custom method home_page would render the template’s index.html page — in this case, the forward-slash (‘/’) would be a shortcut to render the index.html page. Now, let’s say you have a contact page where a web visitor could email you or so, how would you route this?

@app.route('/contact')
def contact_page():
    custom code goes here
    return render_template('contact.html', whatever=whatever, blah=blah, contact_form=contact_form)

So, when a visitor goes to ‘yourwebsite.yourdomain-name/contact — Flask would render the contact.html page.

I like how Flask routes internal paths like this because it’s so easy to see what is going on — not like Django’s black box. If you take a look at the custom code immediately after the @app.route, you would right away know that the block of codes belongs to what template’s page of the website.

I have heard a lot of people loved to use Flask for writing RESTful API, and I actually had done this once as a practice exercise. I could definitely see why people love Flask for this very purpose — it’s so easy to just jsonify the data using Flask. Once you have jsonify the data, you can totally choose how you would like to return these data when a request is made to the app using Flask. Nonetheless, recently I have been using Flask to just create small websites — not RESTful API — and this is fresher in my mind than doing RESTful API.

To sum it up, I just scratch the surface of what Flask can do in this blog post. Based on my own experience, I’ve found Flask is easier to work with than Django. Deploying the Flask web app felt easier than Django too. For example, once I’d finished writing a Flask web app, all I had to do was initialize a GitHub repository, commit, and push the web app to GitHub. Afterward, I could use Heroku or PythonAnywhere to host my Flask web app either through the free tier or paid tier. Setting up Flask with Heroku or PythonAnywhere isn’t that hard. The key is to do a double-check of the requirements.txt to make sure all required packages that Flask needs to run your web app are listed in this file. Heroku and PythonAnywhere rely on this file to install necessary third-party Python packages. Once you set up the necessary steps for Heroku or PythonAnywhere, the last thing you only have to do is to pull your app’s source from GitHub onto Heroku or PythonAnywhere. On Heroku’s CLI, you can just do [git push heroku main], and on PythonAnywhere Bash console you can just do [git pull].

Anywho, I just completed coding a Portfolio website using Flask. The main feature of this website is to allow a user to add various project showcases so he or she can show off the projects. The website relies on Bootstrap 5 and custom CSS I’d written — this means the whole website is responsive to various screen sizes, including mobile phones. In desktop mode, the website showcase two projects one at a time through the pagination feature — allowing visitors to flip to more projects by clicking next or back to previous projects. In mobile mode, I used lazy load to load the first few showcase projects’ images — then the rest would be loaded on-demand as the website visitors could forever scroll downward till all projects had been loaded. Check out the finished product on https://pythongenex.pythonanywhere.com/.

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.

Was Our Universe Instantiated From A Superclass Universe?

If my memory isn’t too disconnected, then quantum mechanics — to how I remember best — is a science describing the smallest of the smallest matters and realms in which everyday physics rules do not apply. Quantum mechanics would try to explain the inner working mechanism of the smallest realm that forms particles that are even smaller than electrons – and these particles form electrons and other similar matters. To this day, the inner working mechanism of the quantum realm isn’t so clear, and so scientists sometimes can’t explain such as why one particle can appear to be at two different places at the same time. Theoretically, scientists like to think every matter in the universe is a wave of some sort, and this wave of some sort allows any matter to appear to be in multiple places at once.

To be honest, I don’t think we are too sure about all of this. In fact, I believe that some inner working mechanism of the universe would forever be hidden away from the human mind. Why? To explain this question in my own words, I like to allure you to the idea of object-oriented programming. In programming, languages such as Python use the concept of OOP to allow any programmer to be able to use new features from a class to enhance a programming project he or she is working on — but the programmer doesn’t necessarily need to know all the details of the class or how it was formed. In most cases, a programmer could import packages that got multiple classes and methods into a program he or she is working on, and then access the package’s class to create a new object in his or her image through cherry-picking some features from the package or package’s class — or just creating brand new features for the object through writing new functions and so forth. An example would be a programmer would import Flask into a program to allow the program to serve HTML/CSS/Javascript pages easily — in this case, the program doesn’t need an external web server such as Apache or whatever.

So, I imagine, you could too, that quantum mechanics could be like mechanics that allow our universe to import features from a superclass universe in which the inner working mechanisms of these features won’t be revealed to the human mind since the human mind is boxed inside this universe. Perhaps by chance, the superclass universe is generous that it would allow a mechanism to allow the quantum mechanics to reveal the thread back to itself, but to find out this truth I surmise it won’t be easy. Regardless, it’s not so hard for any of us to imagine this universe of ours is an object which could have been born from a superclass object like how a programmer could create a new object from a superclass without knowing how the superclass was created. If this is true, we could easily tempt to believe that this universe of ours is just one object among many hundreds of billions of similar objects out there that were also instantiated from the superclass universe.

Can China’s CBDC Replace The Dollar?

As the United States and China continue to develop a more hostile relationship since both countries are trying to out-compete against one another on the global stage. The United States is the current superpower, and China is the upcoming (emergent) superpower. Both countries can spend a lot of money to improve their military strength in terms of troops, hardware, and technology. Both countries are worrying that the opponent would do so well in terms of global trade, influence, and everything else that it would outstrip the home country’s advantage on the global scale! Imagine that one day China could be so influential that it would prevent most countries around the world speak up and side with the United States to prevent China from invading Taiwan out of fear that China would stop trading and even worse will start a military campaign against the outspoken country in a near future. In reverse, China wouldn’t want the United States to continue to dominate the globe since China is now stronger and would become a huge boulder that could block the United States’ global domination.

We all know that the United States Dollar is the dominant reserve currency in which even China itself is holding a large number of dollars just to allow home companies and home local governments to transact with countries that are only wanting to trade in dollars on a global scale. As the United States is getting more hostile toward China for obvious reasons such as wanting to prevent China from dominating the world with its technical standards and influence and whatever else, China knows that it could not rely on the United States’ goodwill in the long run. As the United States sanctions Iran and North Korea and few other countries from conducting dollars on the global scale, China knows that it could be sanctioned by the United States if both countries are going to get even more hostile toward each other shortly. This is why China has been creating a different global transactional path known as CIPS which allows China to conduct trades on a global stage without relying on the SWIFT system in which the Dollar is dominated.

Instead of just being satisfied with what CIPS has to offer for China in terms of conducting trades with countries that are friendly to China on the global stage such as Iran, China is taking one step further by pushing for CBDC (Central Bank Digital Currency). As we speak, it’s estimated that roughly 83% of the world’s central banks are looking at creating their own CBDC version. China’s CBDC is the only digital currency in which has already being pushed to use by its citizens, and this means China’s CBDC has progressed the most out of all CBDC projects out there in the world. As we speak, Canada, Sweden, and few other countries are further along but behind China in terms of making progress in creating a CBDC system. The United States is unfortunately way behind many other countries in creating its CBDC system.

As China makes progress in creating and promoting and updating its CBDC system, the unintentional or intentional, depending on who you’re asking the question, advantage that comes with the creation of this CBDC system is that many countries who are signed up to be a member of BRI (Belt and Road Initiative) may adopt China’s CBDC sooner than we could have imagined for trades on the global stage. How come? Perhaps, using China’s CBDC for conducting trades could cut down the costs of transactional fees, and the digitalization of the logistic application such as clearing customs and so on could be done on the blockchain (which underpins China’s CBDC) smoothly. For example, country A bought huge containers of China’s export for its own import need could just open up an app and with a few clicks here and there — the logistical digitalization through the usage of CBDC — and can order many huge containers of products to be imported without needing to file paper works online or offline for clearing customs and paying huge currency conversion fee and whatnot — meanwhile, the app would show when and where the products have been moved and when will the product arrive on the doorstep of country A.

After watching the video right after the break, you might as well be as educated on the topic of digital currency as I am since the information I’ve shared in the post was learned from the video itself. I have no affiliation with the people in the video right after the break, and I’m sharing the video since it got so much important information about our future. For example, the speakers in the video mention that through Alipay and Wechat, Chinese people become cashless society but China’s CBDC won’t replace Alipay and Wechat but rely on these two platforms to get popular. Another example is that the speakers mention how the digitalization of currency could do away with overdraft protection fees in which the poor are most likely being affected — as we speak 30 billion dollars of overdraft protection fees could have been conducted annually if my understanding of what the speakers have conveyed is correct.

In summary, I think China’s CBDC may displace the dollars on the global stage but won’t replace the dollars on the global stage. To the best of my understanding from watching the video, I could understand that China’s CBDC will allow countries to use CBDC to cut costs and time in conducting trades with China on the global stage. Since China has been pushing for closer ties with BRI members, I could see BRI members will rather use China’s CBDC over the dollars in the longer term. This means China’s CBDC will displace the dollars on global trade but the Dollar will still be a reserve currency for some countries that want to do trade in dollars on the global stage. The dollar won’t go away but the Dollar may lose influence to China’s CBDC over a longer period. This is why the United States too may have to push hard on creating its CBDC system to compete against China’s more progressive and developed CBDC system as we speak. If the United States doesn’t do so, I fear the United States will be way behind in competing against China in terms of trades and currency influence on the global stage.

Will Humans Go The Way of The Horses?

I just watched this video, and it’s an old video from the year 2018. We’re now on the second day of the year 2021, and I really can’t imagine how even more efficient in speed and the level of automation has had occurred in the factory in which the video right after the break reveals. The question is, will humans go the way of the horses in the future? Hmm… I think that we can automate pretty much everything except for human intuition and creativity. Well, just maybe not yet!

Nano Dimension Leads The Way of 3D Printing Electronics Through Additive Manufacturing Electronics

I don’t know much about the manufacturing process of PCBs (Printed Circuit Boards) and other electronic devices. I know that doing the 3D printing of complicated layers for electronic devices from just a sophisticated 2D blueprint is rather complicated. Luckily, I’ve come across some information that enlightens me on this whole shebang.

CEO of Nano Dimension, Yoav Stern, suggests that currently, the process of unsophisticated 3D printing of electronic devices can only print 2D electronic layers on any 3D devices. Yoav Stern suggests that the 3D printed technology using the Additive Manufacturing Electronics method patented by Nano Dimension can do better. According to Yoav Stern, Nano Dimension can allow any company to print complicated electronic devices such as PCBs easily from sophisticated 2D blueprints (i.e., blueprints generated by computer software). It means that these 3D printers made by Nano Dimension can deploy as sophisticated prototype 3D printers.

The benefits of using in-house prototype 3D printers to print electronic prototypes are plenty. For example, IP (Intellectual Property) can be kept in-house secretly since the company doesn’t have to send out the IP to a third party to work on the prototypes. Moreover, these 3D printers from Nano Dimension can also be used as backup manufacturing machines for electronic devices such as PCBs. When the needs arise, any company can switch on the 3d printers to start doing the sophisticated 3D printing of electronic devices.

Honestly, I’d purchased shares of Nano Dimension’s stock, and so I’m glad to be able to get more details on this company through the video right after the break. In the video, Yoav Stern goes into detail on why Nano Dimension’s Additive Manufacturing Electronics technology (3D printers) can outdo their competitors.