How to input MySQL database data to a html table using python? - python

I'm fairly new to using MySQL, HTML, and Python synchronously. I have a website that I create using HTML, CSS, and Javascript. Then I use Python to enter data into a MySQL database. My question is how can I create a table on my HTML side and input MySQL data into the table using Python. I would like the table to grow row-wise dynamically (i.e. I don't want to refresh/reload and the new data should enter the HTML table in a new row so that the table display's all of the data in the MySQL database).
So far I am entering data correctly into the MySQL database and it is being populated. However, I'm stuck on how to:
Create an HTML table that grows dynamically based on amount of data
Use Python to input the data from the MySQL database into the table
I would like a solution WITHOUT using PHP
I'm very new to MySQL, HTML, and Python and therefore, any and all help is greatly appreciated. Thank you in advance!

It seems like you need to use a server to manage the operations of your website smoothly. You should probably check out flask, its extremely user friendly. If you have any amount of experience with Python it shouldn't be difficult to figure it out.
Backend: Flask has the added functionality of handling MySQL databases and handle the operations whenever the server receives requests. Your server can handle this request by responding with a JSON. Check out flask.ext.mysqldb. With Python's json library you can easily convert the query into JSON. Check this answer out for more details
Frontend: You can use a variety of methods with JavaScript to handle HttpRequests, like fetch and XMLHttpRequest. You can call your server and ask your server for the data that is stored in your backend and populate tables with the JSON you receive.
Dynamically growing HTML: this is a dummy function assuming that you receive a JSON response from your server and you have only 2 columns in your MySQL table. The code below is JavaScript.
Taking a dummy HTML like this:
<table id="table">
</table>
With a JavaScript function handling the response of your server.
const response = //JSON response from server
var table = document.getElementById("table");
table.innerHTML = ""; //making sure we don't present corrupt data
response.forEach((result, index) => {
const content = ` <tr>
<td>${result['col1']}</td>
<td>${result['col2']}</td> </tr>}`;
table.innerHTML += content;
})
Side note: You can make use of CSS classes to style these tables!
I know this is a very high level explanation of what is to be done. If you have any more clarifications let me know!

Related

How to set parameters for an API POST query using Python and JSON

I'm querying a real estate API using Python (requests), with POST data submitted in JSON format.
I'm getting responses as expected - however each time I want to make a query I'm editing the fields in a hardcoded JSON object in the .py file.
I'd like to do something a bit more robust - eg using a user prompt to populate the JSON object to be submitted, based on the API search schema (see JSON file (pastebin)) (open to alternative python based solutions to this).
The linked schema includes the full list of parameters available to query - I'll likely trim this down to the ones that are most relevant to the queries that I'm building/POSTing, so that there are less parameters to deal with. I'd like to know of a Pythonic way to cycle through the Parameters in the Schema and then add the ones I wish to submit for a query to the JSON object?
TIA.

Use external data in Django

How can I list external Json data get from mongodb into Django page?
The Json data has table structure.
Django is should just read this data not require create, update and delete actions.
How can I do this?
You can use any MongoDB python library (like this). Import it in your views, get the data and send to the template as you would do with any other type of data.

How do I display data from MongoDB in an HTML page?

Django Version - 1.10.5
I have uploaded the file on MongoDB, with a few fields like e-mail, employee_id
and of course the file from an HTML page. I can see it in mongoDB. What I want to do is retrieve the data on an another HTML page using one of those fields(for example putting in a email id that was used to upload the article will give the entire article on the same HTML page.) . How do I go around that?
From my understanding of the question, current state is:
You have imported data to MongoDB
You want to retrieve some data into HTML
I didn't understand where you are stuck and hence simple steps would be to create your models.py file defining the fields that you mentioned, email_id, employee_id, etc. Check details on how
Django models API lets you create, retrieve, update and delete objects. Next topic in this documentation page.
I recommend reading through Django documentation thoroughly and trying out things in a demo app from here - Django Models and Databases
Extra: You could use raw SQL queries if you used SQL based DB. But Django quotes:
You should be very careful whenever you write raw SQL. Every time you use it, you should properly escape any parameters that the user can control by using params in order to protect against SQL injection attacks. Please read more about SQL injection protection.

Is it possible to use web2py language in javascript file (.js)?

I'm trying to use AJAX with web2py language but I have a problem
My code is:
javascript
$(document).ready(function(){
$(".className").click(function(){
jQuery.ajax({
type:'POST',
url:'getName',
data:{
itemName:'a'
},
timeout: 20000,
success: function(msg) {
alert(msg);
},
error: function(objAJAXRequest, strError){
alert( "Error:" + strError );
}
});
});
default.py
def getName():
itemName=request.vars.itemName
return "Name: " + itemName
The thing is I want to use the data from the database, but is it possible to use
{{for item in tableName:}}
var name={{=item.name}}
like this?
I'm not sure how to extract data from DB in javascript.
Can you help me a bit?
Cheers
The short answer is that you can't directly extract data from the db in javascript using web2py. You have to query the db with web2py, and then use web2py to send your query data to the javascript (or more accurately since you're using ajax, use jquery/javascript to pull your query data down from web2py). Make sure that you actually need to perform logic on the client (javascript) side here, because the easiest thing to do would be to perform all your logic in python in the web2py controller.
However, if you do for some reason need to perform logic on your data on the client side, then you are on the right track. The easiest thing for you to do would be fetch the records you need from the db in the web2py controller, then package the records up as a json object in your web2py controller ("import simplejson" or you can do it with the standard library with the latest version of python), then return that json object to make it available for your js to fetch using the ajax request you've included above. Only at that point should you loop through the json object using the javascript to get the data you need.
That said, if you're just trying to get a field from a single record from the database, the easiest thing to do would be just to query the database correctly to get that record, and return it in a web2py controller for your ajax script to get.

Create chart/statistics for selected mysql table through python

I'd like to start by asking for your opinion on how I should tackle this task, instead of simply how to structure my code.
Here is what I'm trying to do: I have a lot of data loaded into a mysql table for a large number of unique names + dates (i.e., where the date is a separate field). My goal is to be able to select a particular name (using rawinput, and perhaps in the future add a drop-down menu) and see a monthly trend, with a moving average, and perhaps other stats, for one of the fields (revenue, revenue per month, clicks, etc). What is your advice - to move this data to an excel workbook via python, or is there a way to display this information in python (with charts that compare to excel, of course)?
Thanks!
Analyze of such data (name,date) could be seen as issuing ad-hoc SQL queries to get timeseries information.
You will 'sample' your information by a date/time frame (day/week/month/year or more detailled by hour/minute) depending of how large is your dataset.
I often use such query where the date field is truncate to the sample rate, in mysql DATE_FORMAT function is cool for that (postgres and oracle use date_trunc and trunc respectivly)
What you want to see in your data is in your your WHERE conditions.
select DATE_FORMAT(date_field,'%Y-%m-%d') as day,
COUNT(*) as nb_event
FROM yourtable
WHERE name = 'specific_value_to_analyze'
GROUP BY DATE_FORMAT(date_field,'%Y-%m-%d');
execute this query and output to a csv file. You could use direct mysql commands for that, but I recommend to make a python script that execute such query, and you can use getopt options for output formatting (with or without columns headers, use different separator than default one, etc). And even you can build dynamically the query based on some options.
To plot such information, look at time series tools. If you have missing data (date that won't appears in result of such sql query) you should take care for the choice. Excel is not the correct one for that, I think (or not master enough it), but could be a start.
Personaly I found dygraph, a javascript library, really cool for time series plotting, and it can be used with a csv file as source. Careful in such configuration, due to crossdomain security constraint, the csv file and html page that display the Dygraph object should be on the same server (or whatever the security constraint of your browser want to accept).
I used to build such webapp using django, as it's my favourite web framework, where I wrap url call as this :
GET /timeserie/view/<category>/<value_to_plot>
GET /timeserie/csv/<category>/<value_to_plot>
The first url call a view that simply output a template file with a variable that reference the url to get the csv file for the Dygraph object :
<script type="text/javascript">
g3 = new Dygraph(
document.getElementById("graphdiv3"),
"{{ csv_url }}",
{
rollPeriod: 15,
showRoller: true
}
);
</script>
The second url call a view that generate the sql query and output the result as text/csv to be rendered by Dygraph.
It's "home made" could stand simple or be extended, run easily on any desktop computer, could be extended to output json format for use by others javascript libraries/framework.
Else there is tool in opensource, related to such reporting (but timeseries capabilities are often not enough for my need) like Pentaho, JasperReport, SOFA. You make the query as datasource inside a report in such tool and build a graph that output timeserie.
I found that today web technique with correct javascript library/framework is really start to be correct to challenge that old fashion of reporting by such classical BI tools and it make things interactive :-)
Your problem can be broken down into two main pieces: analyzing the data, and presenting it. I assume that you already know how to do the data analysis part, and you're wondering how to present it.
This seems like a problem that's particularly well suited to a web app. Is there a reason why you would want to avoid that?
If you're very new to web programming and programming in general, then something like web2py could be an easy way to get started. There's a simple tutorial here.
For a desktop database-heavy app, have a look at dabo. It makes things like creating views on database tables really simple. wxpython, on which it's built, also has lots of simple graphing features.

Categories

Resources