• Skip to primary navigation
  • Skip to main content
pythonGrid

pythonGrid

Datagrid for Python Web Frameworks

  • Home
  • Docs
  • Demo
  • Download
  • Contact

pythongridstg

set_caption()

pythongridstg · April 28, 2020 ·

  • Parameter(s):
    • caption: the text for the grid caption
  • Description:
    • When set, this method displays text above the column header as the datagrid caption. When this function is not called, pythonGrid sets the table name as the default caption. For example, the caption will be displayed as “Orders” when the table name is “Orders”.
  • Remark:
    • When the caption is set to empty string, e.g. “”, the space used for displaying caption will be hidden, leaves only the column header, grid, and the footer. To display the caption without any text uses ” ” (without the quote).

Example

grid.set_caption('Orders Table')

Initialize Grid

pythongridstg · April 27, 2020 ·

In Flask, it uses view functions to handle application routes. View functions are mapped to one or more route URLs so that Flask knows what logic to execute when a client requests a given URL such as “https://example.com/grid“.

We have two view functions that require initialization.

index()

The file routes.py contains our def index() view functions associate with root URL /. This means that when a web browser requests the URL, Flask is going to invoke this function and pass the return value of it back to the browser as a response.

Inside the function, it creates a new instance of the PythonGrid class and assigns this object to the local variable grid. Note orders is a table from the sample database sampledb.

grid = PythonGrid('SELECT * FROM orders', 'orderNumber', 'orders')

PythonGrid initializer shown above requires 3 parameters, in the order from left to right:

  1. A simple SQL SELECT statement. Always include the primary key as one of the columns if not using the wildcard start(*).
  2. The name of the database table primary key. 
  3. Name of the database table used in the SQL statement.

Note

Do NOT include WHERE clause in 1st SQL SELECT statement. Instead use set_query_filter() method.

The view function passes the grid object into the rendered template from grid.html template.

return render_template('grid.html', title='GRID', grid=grid)

data()

Next, we need the data for the grid (thus the datagrid 🙂

In the next view function data(), we create a new instance for PythonGridDbData class that is responsible for retrieve data from the database.

It has required only 1 parameter, which should be the SAME sql select statement used for PythonGrid.

data = PythonGridDbData('SELECT * FROM orders')

At this point, we can run our program with the command below

flask run

It should give you a beautiful datagrid with data come from the table orders.   frequently)

Congrats! You just made your first grid.

Run Demo

Database Connection Settings

pythongridstg · April 27, 2020 ·

Find file config.py, and set the database connection properties according to your environment. The demo uses MySQL database.

You can also use a socket to connect to your database without specifying a database hostname.

PYTHONGRID_DB_HOSTNAME = 'mysqldatabase.example.com'
PYTHONGRID_DB_NAME = 'sampledb'
PYTHONGRID_DB_USERNAME = 'root'
PYTHONGRID_DB_PASSWORD = 'root'
PYTHONGRID_DB_TYPE = 'mysql+pymysql'

For Postgres set database type to postgres+psycopg2

PYTHONGRID_DB_TYPE = 'postgres+psycopg2'

Install Dependents

pythongridstg · April 27, 2020 ·

pythonGrid uses SQLAlchemy and a few other important modules. They are listed in requirements.txt.

pip install -r requirements.txt

Install Flask Framework

pythongridstg · April 27, 2020 ·

Next, you need to install Flask framework (other frameworks will be supported in the future). You got two options.

It is highly recommended to use Python virtual environment. Basically, a Python virtual environment is a self-contained separate copy of Python installation. Different applications can then use different virtual environments with a different copy of Python without worrying about system permissions.

The following command will create a virtual environment named venv stored in a directory also named venv.

python3 -m venv venv

Activate the new virtual environment:

source venv/bin/activate

Now the terminal prompt is modified to include the name of the activated virtual environment

(venv) $ _

With a new virtual environment created and activated, finally, let’s install dependents:

  • « Go to Previous Page
  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to page 4
  • Go to Next Page »

pythonGrid

pythonGrid is a free and open-source datagrid library made for Python web framework

Copyright © 2025 · pythonGrid | privacy policy

  • Demo
  • Documentation
  • Download
  • T&C
  • EULA
  • Contact Us