Use external data in Django - python

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.

Related

How to input MySQL database data to a html table using 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!

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.

Save JSON file into structured database with Python

I am building a warehouse consisting of data that's found from a public facing API. In order to store & analyze the data, I'd like to save the JSON files I'm receiving into a structured SQL database. Meaning, all the JSON contents shouldn't be contained in 1 column. The contents should be parsed out and stored in various other tables in a relational database.
From a process standpoint, I need to do the following:
Call API
Receive JSON
Parse JSON file
Insert/Update table(s) in a SQL database
(This process will be repeated hundreds and hundreds of times)
Is there a best practice to accomplish this - from either a process or resource standpoint? I'd like to do this in Python if possible.
Thanks.
You should be able to use json.dumps(json_value) to convert your JSON object into a JSON string that can be put into an sql database.

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.

Storing SpiffWorkflow states in Django

I'd like to use SpiffWorkflow in conjunction with Django, but apparently SpiffWorkflow can only serialize its states to JSON and XML:
https://github.com/knipknap/SpiffWorkflow/tree/master/SpiffWorkflow/storage
SpiffWorkflow allows serialization of running workflow, so I could store it essentially as a byte stream somewhere (either in filesystem or in Django's DB). But that deprives me of all the advantages of Django.
Is there some way of mapping dictionary or deserialized JSON structure onto objects that Django can use as a regular Django object (stored by Django ORM in a database)? Would writing such a Django backend for SpiffWorkflow/its workflow's JSON representation be complicated? I'm asking because I have basically no experience in Django.
You can use NoSQL as database backend instead of a RDBMS. I suggest MongoDB as it uses JSON notation to store data and could be used in Django projects using mongodb-engine.
MongoDB is schemaless (read MongoDB website article about being schemaless). You may store your data with any structure you want and change it later on the fly.
There are also other NoSQL backends supported by Django like Redis, Elasticsearch and etc. and you can take a look at them to find the best fit for your special needs.

Categories

Resources