Wednesday, 17 January 2018

Distributing python code, sort of part 1

Ok so I was chatting to a guy at work and we were thinking that we could give more access to more data and that we should be able to do this using python, now he doesn't really know python and I will admit that I getting there but probably no expert as some of my code shows. 

There were 2 stages that I knew I would need to tackle in order to create this app or whatever. 

1) Create a basic GUI for people to enter SQL 
2) Make it so it can be run in an exe. 

Now step 1 is actually a bit more complex as I would also want to create views of data and probably, for now, a spreadsheet to write the code (until I am skilled enough to do that in tkInter or something). For this though I will stick to tkInter. 

So on this post I intend to post the code (I have shamelessly modified from some I found online, can't find original post to give credit!). The point of this is that you can develop code to run a report and then rather than you having to run the code you can give it to someone else. Of course they will need the same access as you but all the logic and connections are held in the script / exe. 



tkinter: 

You can find loads of great information on tkinter all round the web, it even has its own wiki section. But basically you use tkinter to create graphical user interfaces (GUIs) for your programs. For example I can create a reporting interface so that people can enter the name of the report they want and then it will run the code that is associated with that. Or as in this more simple example I will have a pop up box that allows people to enter SQL. This SQL will then be run against the database that I have specified in the back-end but you could all one of the options to be database to run against. 




The picture above show the 2 screens that I am using here, the first, has a button for enter SQL (this could be 2 buttons with a second for enter report name for example), the second picture shows what this looks like when clicked. There is a text box in which to place your valid query. 

In this example I use the a query against one of the basic examples on my CV database: 

SELECT * FROM dbo.userprofile where userId = 1

When this is all run the following happens: 


The code:

No comments:

Post a Comment