Tag Archives: Flask

flask-gentelella : A beautiful admin bootstrap for pythoners


Gentelella is a free to use bootstrap admin template for developing Python Flask web app, with custom Ajax libraries which includes variety of chart scripts like eChart, Morris Chart etc.

It packed with all must have AJAX and CSS libraries for a Python Web Geek

The bootstrap is also a ready to use flask app, all you need to do is customize the custom script. The app uses blueprint to organize flask routes, which is automatically created based on based on modules.

You can develop database powered app with less pain in few hours with this awesome bootstrap from Colorlib.

Clone and start using the project today

git clone https://github.com/afourmy/flask-gentelella.git
cd flask-gentelella

For instruction and the bootstrap project please visit GitHub page

Have a look at my demo app hosted on heroku.com which is not using any database, it depends on simple json API

How to pass python list to JavaScript in Flask app


You can’t use Python list directly into JavaScript, JavaScript lislt/array is not compatible with Python or wise versa. So what to do.

Solution

I have a list of names, which I want pass to a JavaScript in a Flask ninja template. Ninja template allow use to safely convert the python list to Json string then we can pass it to the JavaScript function / just JavaScript

<script type="text/javascript">

        dn = {{ distabr|tojson }}
</script>

You can’t use same route variable in multiple places in same template, it may not serve the purpose

In JavaScript Functions

Suppose you have separate JavaScript function files for some functionality like drawing analytic plots and pie diagram, or some donut chart. So how do we pass value to those functions ?

You can create a JavaScript as above, in your current template, then in function create a script variable to use it as follows.

function init_echarts() {
    var distNames=dn;
}

That’s all you need to know to embed some morris diagram to your flask app.

You should look at Morris JS , it is free and easy to use JavaScript charts library.

Pass variables to other templates in Flask


Flask is one of my Python frame work in which I build simple web application with ease and peace. Today we learn how to pass a variables to other templates in a Flask application.

Suppose your web app have a base template and a top nav bar template which included using

{% include "site_template/top_navigation.html" %}

and you want to pass a title or something else from a Index page to navigation bar where you want show up some text.

Using {% set %} you can create global variables.

The solution

We can use a set block in top of the index page as follows ,to pass variables to other sub templates

{% set mytitle=title %}

In the route it will look like

return render_template('index.html',  title=title)

and in the nav bar page we can use as mytitle as variable using

<div id="site_title"><h2  >{{ mytitle}} </h2></div>

If you had doubt and suggestion please leave a comment

Pyboard for covid-19 status


Pyboardc19 is my leisure time project, I was start working one week ago now it is hosted on Heroku.com.

The dashboard offer live update for all Indian state and for rest of the world in the upcoming updates.

This is a minimal Flask,Python project, all the data which is source of the report is coming from official IndianCovid19 API as json file.

I also used bootstrap template, morris chart morrischart library, echart for this project.

Pyboard app Python  project
iPyboard App

Please share and comment. The project will be soon available on my GitHub Profile, so that you can use it for create your own.

Thanks to awesome bootstrap template from Colorlib.

Escape: I want to include more countries to dashboard, if you don’t mind please share json API

Thank you

Build a Flask-Python web App


Building a dynamic website made so easy with Python -Flask, a Micro FrameWork which helps us to create the websites like Twitter and even more. Forget about the complicated web programming languages, start learning Python. So where you start, Python or Flask ?. I should say, you have to learn basics of how Python first, just have a look at Python.org

IDE

Which IDE I should choose? IDE helps you to write, compile, build programs. Pycharm is a good choice, Visual Studio also had added the feature for Python programmers in their 2013 edition onwards. You can Google it for more options. I Prefer Pycharm.

Flask and Web, How it works?

Flask arranges web pages as basic URL or routes.  Let’s take a look at the Pycharm App code.

from flask import Flask
app = Flask(__name__)

@app.route(‘/’)
def hello_world():
return ‘Hello World!’

if __name__ == ‘__main__’:
app.run()

When the Index page is loaded the @app.route(‘/’) will be fired with an initial page. You can create as much as routes you want to say ‘/blog’, ‘/news/ etc.

 

How to redirect a route to another in Flask app


How to redirect a route to another in Flask?

Python and Flask have the rich set of libraries and Classes which offer variety of tasks. You need not do lots code in Flask. Let’s come to the topic.

Finest example of redirecting route/web page is, when user login successfully , it is wise to redirect the login route to ‘chat room’, on a chat app. To do this you need to import the following packages.

from flask import redirect, url_for, request

After that you can use the redirect and url_for functions with return statement as follows

app.route(‘/login’, methods=[‘POST’, ‘GET’])
def login():
form = LoginForm()

if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user:
if check_password_hash(user.password, form.password.data):
login_user(user, remember=form.remember.data)
return redirect(url_for(‘chatroom’))
return ‘Invalid user name’

return render_template(‘signin.html’, form=form)

In our Chat example if the user login then the route will be redirected to ‘Chatroom’ route, which is the chatroom page for our app.

Flask Mysql database connection


How to connect a MySQL database with you Flask python app. MySQL is the most commonly used database today, here is the code

app.config['DATABASE_FILE'] = 'uchat'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://scott:tiger@localhost/' + app.config['DATABASE_FILE']
app.config['SECRET_KEY'] = '123456790'
app.config['SQLALCHEMY_ECHO'] = True

With MySQL driver called pymysql which is available in Python Package Index  Library, you can connect even local database for development purpose.  To install this package use  pip

Run the following command from your command prompt

pip install pymysql

and pip will download and install necessary packages.

SqlAlchemy[SQLite] databse connection in Python-Flask


Flask is a Python framework which can be used to build the web application from scratch. Flask is micro frame work which capable to build websites like Instagram, twitter and anything at programmers will.

SqlAlchemy is a package in Python Flask which simplifies data base connections. The following simple Application will explain ….. how.

This project was built on windows OS and Pycharm IDE

The connection string

from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
 app.config['DATABASE_FILE'] = 'app2.db'
 app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE_FILE']
 app.config['SECRET_KEY'] = '123456790'
 db = SQLAlchemy(app)

So the db is like the cursor to the database.

Making of Database enabled Class

The User class exclusively has some database capabilities, such as querying tables. It is a fine example of how inheritance helping to build better applications.

class User(db.Model, UserMixin):
 id = db.Column(db.Integer, primary_key=True)
 username = db.Column(db.String(80), unique=True)
 email = db.Column(db.String(120), unique=True)