• Home
  • Documentation
    • Screenshots
    • Tutorial
      • First database application
    • Credits
    • How to
  • Development
    • Known issues
    • Development notes
  • Download
  • Blog


Pytonic is an open source full integrated IDE that makes easy to build business applications with IronPython.

Creating a first simple application

Posted by: Pytonic on 17 Sep 2011

1/ Launching the Pytonic application

Go into PyTonic folder and double click on "PyTonic.exe" file. 

Starting Pytonic IDE

2/ Create a new project

Click on "Create a new project" (you can also use the "Project" menu, then select "New project").

Creating a new PyTonic / IronPython project

Make sure that ".NET IronPython Project" is selected, then enter "SimpleTutorial" as project name and choose the location of the project on your hard drive.

If the folder doesn't exists yet, it will be created if the option "Create directory for project" is checked.

Choosing project type

You should have something similar to the following screenshot.

The default project template has been loaded.

PyTonic default project template

3/ Database configuration

A database must be configured to be able to use PyTonic. Currently Microsoft SQL Server, MySQL and SQLite database engine are supported.

First create the database on your SQL Server.

Then, double click on "config/database.xml" file to edit it.

Enter the correct parameters to connect to the database you just created.
"DatabaseEngine" can be "mssql" or "mysql" or "sqlite". 
Save the file.

Note :

When using "sqlite", "DatabaseServer" can be left empty or filled with an absolute path. If you left it empty, the database will be stored in the project folder, using the name defined in "DatabaseName".

Database engine configuration, IronPython

4/ Defining the database model

Before starting to develop, we need to define a first table in our database.
Rename the file "database/tables/table_company.xml" to "table_people.xml", then open it. 

Defining the database model

Change the table name from "company" to "people" and remove every lines starting with "<addfield".

Now we will add some fields to the "people table".

A quick way to add a new field is to use the code snippets :
Hit "CTRL-B" then select the "addField_varchar" snippet. 

Adding fields to the table

Add 5 fields, like on the next screenshot :

Adding new fields to table

Save the "table_people.xml" file.

5/ Generating the database model

Each time you make changes to your database, you need to regenerate the database model.
Click on the refresh button. 


Generating the database model

You may get the following error when clicking on the refresh button.
If it happens, first check if your database parameters are correct, then close and restart PyTonic and click again on the "refresh" button. 

Exception when generating the database model

Once the model has been generated, you will see 2 new files in the "database" folder :
- "model.cs" which contains the C# code that will create the database objects
- "model.dll" a compiled version of the "model.cs" file, for faster loading 

Generated model files

6/ Creating a new module "people"

We will create a new module to manage the "people" table.

Right click on "module" folder and select the option "add -> new folder".

Creating a new module

Set the new folder name to "people".

By convention the module should have the same than the main table that will be used in that module.
When using the templates to create the forms and the Python code, this folder name will be used to define what table to use. 

Creating module "people"

7/ Creating files to manage the list of "peoples"

First we will use a template to create a Python file to manage the list of records in the "people" table.

Right click on the "people" folder and select "Add -> DevExpress -> New Python-List..."

Adding a Python file to manage the people list

Set the name to "list_people.py".

By convention, the name is set to "list_" + table name + ".py". 

Setting "Python list" file name

Now we will use another template for the form that will display the table grid for the table "people".

Right click on the "people" folder and select "Add -> DevExpress -> New Form-List..."

Adding a database list form from template

Set the name to "list_people.xml".

Like for the Python file, by convention, the name is set to "list_" + table name + ".xml". 

Defining name for the people list form

8/ Modifying the design of the "people list" form

We need to modify the default form provided in the template to use the correct table fields.

Right click on the "list_people.xml" file and select "Design form".

Designing the people list form

The form designer is opened, showing the default design provided by the template we used.

Click on "Run designer" button to define the columns we want to display in the data grid.

Opening the grid designer

Click on "Columns" button in the left panel.

Modifying data grid columns

Remove the columns "column_code" and "column_name" by selecting them in the list and clicking on "Remove" button.

Remove existing columns in the datagrid

Add a new column by clicking on "Add" button.

Adding a column to datagrid

Give the name "column_firstName" to the column.

Set column name

Set the Caption property to "people.firstName" and the FieldName property to "people_firstName".

By convention the caption will be "module name.field name".

Define column caption and field name

Now create another column to display the "last name".

Create another column

Change the caption of the form by modifying the "Text" property (be sure that the form is selection).

Be careful, don't use the "delete" key. In the current version, "delete" key is *always* removing the selected component. Use the "backspace" key instead when you want to delete some text in a field.

Renaming main form caption

Set the value to "people.list" (the convention is "module_name.identifier", this will be used in the localization module and explained in next tutorials).

Setting main form caption

Save the modified design (CTRL-S) and close the designer.

Now, let's run this form.

Open the "list_people.py" file in the IDE (double click on it) and launch it by pressing the "F5" key. 

Running the form

You will receive this error message (the default template tries to use a table field that doesn't exists) :

Field name error

Once you click on "OK", the form will still be displayed :

Empty people list data grid

9/ Modifying the Python code

We need to modify the Python code, first close the displayed form (shortcut : CTRL-W).

Changing field name in Python code

Locate the "load_records" function (this function is in charge of loading data from the database to display them in the grid) and change "people_name" to "people_lastName, people_firstName".

Changing the field name used to sort the results

Save the file (CTRL-S) and relaunch it (F5 key).

Now the form is displayed again without error, but we still have no data, we need to allow data entry.

10/ Managing data entry

 Right click on "people" folder and select "Add -> DevExpress -> New Form-add...".

Adding a new form to manage data entry

Set the name of the new file to "add_people.xml".

Giving a name to the new form

Right click on this newly created "add_people.xml" file and select "Design form" option.

Designing the add form

The designer is opened and the default template for the data entry form is displayed.

Design people add form

Change the labels to match with the fields that are in our "people" table.

Changing labels

Delete the two "TextBox" controls (select them and press the delete key). 

Deleting existing text edit controls

Your form will look like that :

Existing fields deleted

Now add a new TextBox component on the form (using drag and drop) and name it "people_firstName" (by convention with name it like the database field).

Add new textBox

Add another TextBox named "people_lastName", your form should look like that :

Adding another TextBox

Save the file and close the designer.

We now need to add the Python code to manage the data entry, right click on "people" folder and select "Add -> DevExpress -> New Python-Add...".

Adding Python template to manage data entry

The new Python file should be named "add_people.py".

Naming the Python file

Open the "add_people.py" file and locate the "loadRecord" function.

Some code needs to be modified to use the fields of our table :

Python code to be modified

Change "_code" to "_firstName" and "_name" to "_lastName" :

Modifying the Python code

Location the "addRecord" function :

Locate the IronPython code used when adding a record

Change "_code" to "_firstName" and "_name" to "_lastName" :

Table fields that needs to be changed in the IronPython code

Save the file.


11/ Testing the application

Select / open the file "list_people.py" in the editor.

IronPython list file

Launch it by pressing the F5 key.

You should now be able to add / edit / delete the records.

Adding a record to database with IronPython

Categories

(C) 2018 Hypee


 
Stay in Touch
  • #Our RSS Feeds