Python - Dynamic 'navigation menu' based on entries in Database - python

I'm wondering how I can dynamically generate options in a navigation menu based on values from a table in my database.
I have some records in a database table. For each I would like to create a Menu Item for based on a specific field.
i.e.
TABLE: pages,
id int,
page_name varchar(255),
page_info varchar (255);
So based on how many page_name are in table 'pages' i want to create a menu item/button for. The obvious reason i would want this is so that if and when I create a new entry into my database, it will automatically appear as an option on my menu. (Side note: I plan to change the content 'page_info' of div's based on what menu item is selected)
I know that this could be quite simply achieved in PHP but How could this possibly be achieved in Python?

This is a really open-ended question which is hard to give you a specific answer. However, if I was making something like this I would use a simple Flask application with pyodbc to query whatever database you are pulling the data from. From there I would use a jinja template to build the actual html with our database results.
Pyodbc Docs
Flask Quickstart
Jinja2 Docs

Related

Flask and SQLAlchemy sort in display without new query?

I'm displaying the results from an SQLAlchemy (Flask-SQLAlchemy) query on a particular view. However the sorting/order is only set by what I originally passed into the query ( order_by(desc(SelectedTable.date_changed)) ). I'm trying to now add functionality that each column that is displayed can be selected to order the presentation.
Is there a way to alter the way a returned query object is sorted once it's returned to create this behavior? Or will I need to build custom queries for each possible column that could be sorted by and ascending/descending?
Is there a recipe for implementing something like this? I've tried google, here, the Flask, Flask-SQLAlchemy, and SQLAlchemy docs for something along these lines but haven't seen anything that touches on the subject and beginning to think that I'm going to need to use custom queries or without new queries try some JavaScript in the Jinja Template to achieve this.
Thanks!

Python or SQL: Populating an excel form (multiple times and saving outputs) from another table

Problem: Customer has requested we fill out a form (excel) for each item we provide them. Due to us providing them a large amount of parts, I would like to figure out a way to automate it as much as possible.
Idea: Create a table ('Data') with each part number and relevant information in the columns. Use Python to read 'Data' table, open blank customer form, populate blank customer form, and then save customer form.
Questions:
Can SQL accomplish this task as well? In relation to this task, I've only really created flat table outputs with SQL. Not really sure how this would work.
Recommended Python packages / documentation?
Similar example with code available? Just helps me learn being able to walk through something.
Any other ideas? Maybe I am tackling this issue the wrong way.
I am just unsure of my best path of action.
You could create a simple table on your SQL system (PostgreSQL, MySQL), so you can add modify simply your items.
Then you can export your table in excel format as the customer wants with:
Copy (Select * From foo) To '/tmp/test.csv' With CSV DELIMITER ',';
You can also do it with python, but i think it's more complicated to update item with python, with a SQL system you could create and HTML/PHP front-end page making it more customizable.

Saving datas from one database to another

I want to copy the datas from one database to another in Postgres. I wrote a script in django and was able to grab a datas from one specific table but how can i add that data in other database.New database has same table and column name, i want to save that old database files to new database.
This might be easy for some of you guys but i really couldnt figure that out.
I'm not familiar with either API but if the rows/columns have the same dimmensions you could do something like (and this is partially pseudocode):
for x in range(height):
for y in range(width):
data = call_data_from_database_A(x, y)
new_entry = enter_data_into_database_B(x, y)
Where the Call_data is you're getting data from that specific row/column, and enter_data enters the data into that specific row/column. I'm not familiar with either API but if you find the two I'm sure you could figure it out rather quickly.
Instead of writing your own import and export code, why not use the native capabilities of Postgres and dump the table from your old database then import it into your new one:
http://www.postgresql.org/docs/current/static/sql-copy.html
The simplest way to do this with Django (move one Django database to another, defined with a different model django database) is to write a 2 Django views and one jquery html page.
The first view will be in the original Django app. It will essentially create a json object model of the database and push it out on a get request. This is custom to your Django's models.
The second view will be in the new Django app. This will take in json data and format it to match your current Django database (fields might not match up exactly, hence the reason for doing this migration). You then just add elements into the new database just as you were creating a new Django model entry(example).
I personally use a one off jquery html page that gets the json data from the first view and posts it to the second one. You could exclude this piece and just write it all in python in the second view, but I find doing it this way to be much cleaner.

GUI for database tables with pygtk and glade

I'm building a database front-end with python and glade. I need to present SQL query results in the form of database tables inside my app's window (schema followed by tuples/records). Both the schema and the database entries are dynamic because the schema could be that of a join operation or in general altered and the number of tuples could be any valid number.One possible solution could be to format a given table with python, create a text object in my GUI and change its' value to that produced by python. Advices and suggestions are very welcome.
Given that the number and name of the columns to display isn't known beforehand, you could just create a gtk.TreeView widget in glade and modify it as you need in the application code.
This widget could be updated to use a new model using gtk.TreeView.set_model and the columns could be adapted to match the information to be dsplayed with the gtk.TreeView.{append,remove,insert}_column columns.
Regarding the model, you coud create a new gtk.ListStore with appropriate columns depending on the results from the database.
I hope this helps.

Python: map widgets to database for reading and editing it

I am relatively new to database GUI programming, and I want to make a simple app in Python, which allows user to access and edit database. I want to view/edit tables and specific records plus generate some specific reports about stored data. For example, if we have a table with employees name and position, it should allow to edit name and select position from list and immediately change database according to changes. For one employee record it should output name and, again give a selectable list of positions. Also, it should have a dialog to add employees.
So, is there a way to create widgets for data tables and specific records which allows to output and edit data with automatic changes in database? I want to reduce the need for writing methods, which look at changes in view and reflect them at model.
I am using PyQt for writing GUI. Solutions for SQL, or ORMs like SQLAlchemy would both be fine.
You can use the Qt Database GUI Layer.
If you want to use SQLAlchemy too, you can take a look at Camelot.
UPDATE
A good introduction to the Qt Database GUI Layer is chapter 15 of the book
"Rapid GUI Programming with Python and Qt".

Categories