NetBeans
NetBeans is a world class platform for creating applications. Originally, it was a Java IDE. With each release, NetBeans is becomming more versatile. The newiest NetBeans 6.5 release has a new module for creating PHP applications. Ruby and C/C++ development modules were added in the past.In this article, we will talk about a new module for the Python programming language.
Python EA IDE
NetBeans has recently released an Early Access IDE for the Python programming language. Fully functional Python IDE has been an old request or dream of many Python programmers. This NetBeans subproject seems very promising. Currently the project is under development. It has been released as a early access project. It can be downloaded from the following.NetBeans community already created an IDE for PHP and Ruby programming languages. Python is the next logical step. This way, the NetBeans platform brings another very popular language to it's portfolio.
Netbeans itself is created in Java language and Swing GUI. This combination is ideal for a multiplatform, multilanguage IDE.
First impressions
Installing of NetBeans for Python was very easy. The GTK theme was greatly improved and it looks very well. (This comes from a recent JDK version, not NetBeans itself.) The application is very responsive and fast. On my notebook with 512 MB RAM and 1.6 GB processor, NetBeans performed very well. Without specialized benchmarking tools, there is no way how you can tell it from a C/C++ application. Except perhaps, when you run out of memory and do a lot of swapping.System requirements for this Python EA module were not specified on the official NetBeans site. These are typical general system requirements for Ubuntu Linux: Minimal system requirements are: 800 MHz processor, 512 MB memory, 650 MB free disk space. Recommended system requirements are: 2.6 GHz processor, 1 GB memory and 850 free disk space.
The Look and Feel
On Linux, the default is the GTK theme. Developers can choose among three lafs. We have the GTK theme, the metal theme and the motif theme. The motif laf looks interesting, but it has several serious design bugs. The GTK theme has vastly improved in the recent versions of JDK.![Metal look and feel](http://zetcode.com/articles/images/netbeanspython/metallaf.png)
Figure: Metal look and feel
![GTK look and feel](http://zetcode.com/articles/images/netbeanspython/gtklaf.png)
Figure: GTK look and feel
The IDE
The following screenshot shows the NetBeans IDE with the traditional metal look and feel.![NetBeans IDE](http://zetcode.com/articles/images/netbeanspython/netbeans.png)
Figure: NetBeans IDE
The NetBeans IDE is divided into several parts, called simply windows. These are probably the most common windows:
- Projects window
- Source editor window
- Output window
Features
- Code folding
- Code completion
- Syntax highlighting
- Database support
- Web services support
- Dynamic variable highlighting
- Multiline code commenting
![Code editor](http://zetcode.com/articles/images/netbeanspython/codeeditor.png)
Figure: Code editor
The next screenshot shows a dynamic variable highlighting in action. This is a very handy feature. If you place a cursor on a variable, all occurences of this variable are marked. The background of the text is highlighted in a different color. It looks good and is unobtrusive. Unlike eg. in Eclipse. This feature is not available in JDeveloper.
![Variable highlighting](http://zetcode.com/articles/images/netbeanspython/letters.png)
Figure: Variable highlighting
Creating a project
First, we need to create a Python project. There are several ways, how we can create a Python project. From File menu, we select New Project option, or use the Ctrl+Shift+N shortcut. We can also create the project by using the context menu, right click on the project window and select New Project from the pop up window. The fastest way is to click on the new project button on the edit toolbar.![The edit toolbar](http://zetcode.com/articles/images/edittoolbar.png)
Figure: The edit toolbar
Next we do two steps. Choose a type of a project and then provide a name and location for the project. From the Categories box, we select Python, from the Projects box, Python Project.
In the next dialog window, we provide a name and location for our Python Project. There is also a check box. It is checked by default. It means, that by default a new project is set to be the main project. We can have several projects opened in Netbeans. If we click on the build, debug or run button, this is applied to the current main project in the IDE. We can set, unset a main project from the File menu, or by right clicking on the project icon in the projects window.
The last important thing is to choose the correct version of Python interpreter. There are two options. The classic CPython and Jython. The Jython comes prebuilt with Netbeans. The CPython was located during the installation proces. In our article, we will work with CPython examples.
![Python Project](http://zetcode.com/articles/images/netbeanspython/pythonproject.png)
Figure: Python Project
Console Python example
In the first example, we create a simple Python console script.#!/usr/bin/pythonWe create a new project as shown previously and type in the code. Execute the script by pressing Shift + F6.
# console.py
print "Netbeans IDE 6.5"
print "for Python language"
print "Early access"
We should see a text printed in the console window.
![Console](http://zetcode.com/articles/images/netbeanspython/console.png)
Figure: Console
Simple PyGTK application
In the next example, we will create a PyGTK application.Creating a PyGTK application worked out of the box on Linux. No additional settings were necessary. We just need to type in the code and launch the application with the Shift + F6 key combination.
Same for wxPython or PyQt4 applications.
#!/usr/bin/pythonThis small code example creates a window in PyGTK library. After pressing Shift + F6 a small window will pop up. (The window might be minimized by default).
# simple.py
import pygtk
pygtk.require('2.0')
import gtk
class Simple:
def destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self):
win = gtk.Window(gtk.WINDOW_TOPLEVEL)
win.connect("destroy", self.destroy)
win.set_title("Simple")
win.set_position(gtk.WIN_POS_CENTER)
win.show_all()
Simple()
gtk.main()
![Simple PyGTK application](http://zetcode.com/articles/images/netbeanspython/simple.png)
Figure: Simple PyGTK application
MySQL database example
NetBeans Python IDE EA comes with a built-in database support. We can create databases, tables or execute SQL commands inside the IDE environment.In our example, we will create a friends database, friends table and insert some data into the table. A Python script will fetch the data from the table and print them into the console window. We will use MySQL database.
For this example to work, you should have MySQL database system running and have MySQLdb module installed.
To check, if you have MySQL RDMS running, execute the following command:
$ ps -ef | grep mysqlThe following commands search for and install a Python MySQL database module
root 5391 1 0 15:35 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe
...
$ apt-cache search mysqldbIn the Services window, you can see a database and a web services icon. The services window is whown with Ctrl + 5. The MySQL RDMS was running, when I launched the IDE, so NetBeans recongized it. Otherwise we have to create a new connection by right clicking on the icon.
python-mysqldb - A Python interface to MySQL
python-mysqldb-dbg - A Python interface to MySQL (debug extension)
eikazo - graphical frontend for SANE designed for mass-scanning
sql-editor - editor of SQL databases, with 'join' capability
$ sudo apt-get install python-mysqldb
![Services](http://zetcode.com/articles/images/netbeanspython/services.png)
Figure: Services
The first node in the Databases tree shows a MySQL server instance. The instance has 5 different databases. We create a friends database. Right click on the server instance icon and select Create Database... option. In the dialog window, we specify a name for our database, e.g. friends.
Next we right click on the database friends, and select Connect... option. We specify user name and password. Upon successful connection, we shoud see complete connection icon for the friends database.
![Database Connection](http://zetcode.com/articles/images/netbeanspython/connection.png)
Figure: Database Connection
We then right click on the database connection icon and select Execute Command... option. A SQL Commnad window appears, where we will write out SQL commnads.
CREATE TABLE friendsWe create a friends table. To execute this SQL command, click on the Run SQL icon, or press Ctrl + Shift + E.
(
id int not null auto_increment primary key,
name varchar(40),
age int
);
INSERT INTO friends (name, age) VALUESHere we populate the table with some data.
("Erika", 21 ),
("Robert", 34),
("Mike", 22),
("Roman", 29),
("Lenka", 18)
When we run
SELECT * FROM friends
SQL code, we get a new window, where we can insert new record, update or delete records. ![friends table](http://zetcode.com/articles/images/netbeanspython/friends.png)
Figure: friends table
Finally, we write the Python script.
#!/usr/bin/pythonIf all went OK, we get the following list of friends in the output window.
import MySQLdb
try:
conn = MySQLdb.connect(host="localhost", user="root",
passwd="", db="friends")
cursor = conn.cursor()
cursor.execute("select * from friends")
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
else:
data = cursor.fetchall()
for id, name, age in data:
print name, age
cursor.close()
conn.close()
Erika 21
Robert 34
Mike 22
Roman 29
Lenka 18
Issues
I have encountered several issues when working with Python IDE. The IDE is in beta state and it is not finished yet. I will mention some of them.For example, there are issues with the code folding. Code completion does not work well. Too many options pop up, so the programmer is lost in all those values. No matter what I do, I can't get rid of the start page. It is still there, when you restart NetBeans. When you resize the window to a small size and bring it back to original size, toolbars are scattered. The about dialog is huge, but says nothing about the fact, that it is a Python IDE. The Ctrl + / keys comment the code, but this key combination is not available in any of the menus. etc.
0 comments:
Post a Comment