I am writing an application using python.net. My objective is to fill a DataGrid programmatically with data. The code I am working in is below: Show
I was able to add the columns. However, the code is failing to add items(rows) to the DataGrid. Does anyone know how can I do this? Published May 22, 2020Last updated Jun 16, 2020 pythonGrid is a new free open source library to create a fully working datagrid for CRUD (Create, Read, Update, & Delete) for Flask that connects to a relation database such as Postgres or MySql/MariaDB database. It makes everyday datagrid tasks extremely easy. Standard functions like sorting, pagination, search, and CSV export are supported out-of-box without complicated programming. pythonGrid does not require creating a separate data model for each database table. It requires only two lines of code for a basic CRUD.
Requirements
Quick StartA couple of quick-start options are available:
Files includedWithin the download you will see something like this:
pythonGrid has three main files in
Creating the DatabaseFind the sample database in the folder sampledb. Using your favorite MySQL os Postgres client (more database supports are coming).
Install PythonFirst of
all, if you don't have Python installed on your computer, download and install it from the Python official website now.
Next, install the Flask framework. Install Flask Framework via Virtual EnvironmentIt is highly recommended to use Python virtual environment. A Python virtual environment is a self-contained separate copy of Python installation. Different applications can then use different virtual environments with a modified Python copy without worrying about system permissions. The following command will create a virtual environment named
Activate the new virtual environment:
Now the terminal prompt is modified to include the name of the activated virtual environment
With a new virtual environment created and activated, finally, let's install dependents: Install DependentspythonGrid uses SQLAlchemy to support different types of databases.
ConfigurationFind file You can also use a socket to connect to your database without specifying a database hostname.
For Postgres set database type to
Initialize GridFlask uses view functions to handle the 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 three view functions that need initialization. index()The file Inside the function, it creates a new instance of the PythonGrid class and assigns this object to the local variable
PythonGrid initializer shown above requires 3 parameters:
The view function pass the grid object into the rendered template from
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 to populate our datagrid.
export()Export function is almost identical to data function above except we need to use
Hello, GridAt this point, we can run our program with the command below.
It should give you a beautiful datagrid with data from A List of Common Datagrid FunctionsFrom the basic grid, we can add new functions such as changing title, adding search, and enabling export, set text align, etc., through simple function calls.
See the list of complete pythonGrid documentation.
Please stay tuned for the second part of the step-by-step walkthrough for the rest of CRUD operations, including Add, Edit, and Delete! If you have any questions about this tutorial, feel free to comment below or reach out to me. |