Category 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

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.

 

Flask: Secure password the with werkzeug hashing


Werkzeug started is a simple collection of various utilities for WSGI applications and it becomes one of the most advanced WSGI utility modules in Python and is used to build many advanced frameworks such as Flask.

Here is how you can use Werkzeug hashing in your apps. In order to do this, you may need the following import statement.

from werkzeug.security import generate_password_hash, check_password_hash

The hashing is simplified with generate function.

hash_password = generate_password_hash(form.password.data, method='sha256')

The check password method will validate the input. In both cases, you can see the URL contain the hash strings instead of the regular password string.

For more help Please refer, 

https://pypi.python.org/pypi/Werkzeug/

Click to access werkzeug.pdf

http://werkzeug.pocoo.org/

 

 

Installing Python Flask Framework


Flask is the one of the most popular micro Frame work which enable programmers to quickly build web applications. Linkedin, Pinterest are some of the finest example of this framework. The Frame work was initially coded by Armin Ronacher of Pocoo. Lets start with Installation

Install Flask

First up all you need to install python on your system, if you done’t have get one from www.python.org 
You can install Flask from Python Package Index which require the PIP command and a internet connection. Learn how to get PIP?
Install flask using the following command line, the simplest way to install Flask on your system.
On Windows go to your command prompt/shell and issue the pip command.
D:/> pip install flask
It will take some time to finish the procedures.

Uninstalling Flask

D:/> pip install flask
will remove Flask packages from your system

Create your first App

Flask is Fun

Create a file called hello.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
return "Hello World!"

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

And Easy to Setup

C:/> pip install Flask
c:/> python hello.py
* Running on http://localhost:5000/
Learn more about Flask

Create Python Flask website on Pythonanywhere


Flask is one of the light weight Python Frame work which can be used to develop awesome website

Image result for flaskwith minimal coding. Take a look at the few Flask powered website, click here

For development purpose you can simply launch on project at a time in Pythonanywhere.com, a free python hosting website.

Create Web Application

Go to your Pythonanywhere.com dashboard and locate Web Tab and add a new Application.
Choose appropriate Flask and Python version and finish the project. Your Flask project resides in Mysite folder. Go to your flask_app.py file and edit and add few lines of code for adding more urls as follows
from flask import Flask
app = Flask(__name__)
@app.route(“/”)
def index():
return “Index Page!”

@app.route(“/post”)
def post():
return “Post Page”

@app.route(“/about”)
def about():
return “About the page”

@app.route(“/contact”)
def contact():
return “Contact us”

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

You can also use Console tab for advanced operations.

Let’s take a look at your flask

username.pythonanywhere.com — Index
username.pythonanywhere.com/post  — Post page
username.pythonanywhere.com/contact  — contact page