from Django forms to pandas DataFrame - django

I am very new to Django, but facing quite a daunting task already.
I need to create multiple forms like this on the webpage where user would provide input (only floating numbers allowed) and then convert these inputs to pandas DataFrame to do data analysis. I would highly appreciate if you could advise how should I go about doing this?
Form needed:

This is a very broad question and I am assuming you are familiar with pandas and python. There might be a more efficient way but this is how I would do it. It should not be that difficult have the user submit the form then import pandas in your view. Create an initial data frame Then you can get the form data using something like this
if form.is_valid():
field1 = form.cleaned_data['field1']
field2 = form.cleaned_data['field2']
field3 = form.cleaned_data['field3']
field4 = form.cleaned_data['field4']
you can then create a new data frame like so:
df2 = pd.DataFrame([[field1, field2], [field3, field4]], columns=list('AB'))
then append the second data frame to the first like so:
df.append(df2)
Keep iterating over the data in this fashion until you have added all the data. After its all been appended you can do you analysis and whatever else you like. You note you can append more data the 2 by 2 thats just for example.
Pandas append docs:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.append.html
Django forms docs:
https://docs.djangoproject.com/en/2.0/topics/forms/
The docs are you friend

Related

What is the best way to have a user enter a time in Flask WT Forms?

I want the user to select a military time within a Flask Form. So far I have a list of hours and a list of minutes. I also use SqlAlchemy and want the data saved into a DateTime column.
hours = ["0","1","2","3","4","5","6","7","8","9","10","11","12","13",
"14","15","16","17","18","19","20","21","22","23"]
minutes = ["00","05","10","15","20","25","30","35","40","45","50","55"]
time_hours = SelectField('Hours', choices=hours)
time_minutes = SelectField('Minutes', choices=minutes)
I could use two separate SelectFields, one for hours and the other for minutes, and combine them within the view, finally saving them into a DateTime field. That will complicate things when I set up an edit page and try to fill in the form's data with the data stored in the database. Does anyone know if there is a "proper" way to select a time within Flask WT Forms?
You could install the package WTForms-Components and use TimeField as follows:
from wtforms_components import TimeField
class YourForm(FlaskForm):
...
time = TimeField('Time')
It will render something like this

How can I add data to my django db. Using excel as an input?

How can I add data to my django db. Using excel as an input?
I extracted the data but i don’t know to go store it in the database
I have two models patient and doctor. I have extracted the data using xlrd, and got a dictionary:
d ={“name”:list of names,”number”: list of numbers , so on }
I used a forms.form model.
you can access excel data with pandas library, you should first make dataframe and then you can save your data using model.objects.get_or_create method in view.
before that you should first define your table in model for save data .

How to filter queryset by string field containing json (Django + SQLite)

I have the following situation.
The Flight model (flights) has a field named 'airlines_codes' (TextField) in which I store data in JSON array like format:
["TB", "IR", "EP", "XX"]
I need to filter the flights by 2-letter airline code (IATA format), for example 'XX', and I achieve this primitively but successfully like this:
filtered_flights = Flight.objects.filter(airlines_codes__icontains='XX')
This is great but actually not.
I have flights where airlines_codes look like this:
["TBZ", "IR", "EP", "XXY"]
Here there are 3-letter codes (ICAO format) and obviously the query filter above will not work.
PS. I cannot move to PostgreSQL, also I cannot alter in anyway the database. This has to be achieved only by some query.
Thanks for any idea.
Without altering the database in any way you need to filter the value as a string. Your best bet might be airlines_codes__contains. Here's what I would recommend assuming your list will always be cleaned exactly as you represent it.
Flight.objects.filter(airlines_codes__contains='"XX"')
As of Django 3.1 JSONField is supported on a wider array of databases. Ideally, for someone else building a similar system from the ground up, this field would be a preferable approach.

How to remove the name "Queryset" from queryset data that has been retrieved in Django Database?

we all know that if we need to retrieve data from the database the data will back as a queryset but the question is How can I retrieve the data from database which is the name of it is queryset but remove that name from it.
maybe I can't be clarified enough in explanation so you can look at the next example to understand what I mean:
AnyObjects.objects.all().values()
this line will back the data like so:
<QuerySet [{'key': 'value'}]
now you can see the first name that is on the left side of retrieving data which is: "QuerySet" so, I need to remove that name to make the data as follows:
[{'key': 'value'}]
if you wonder about why so, the abbreviation of answer is I want to use Dataframe by pandas so, to put the data in Dataframe method I should use that layout.
any help please!!
You don't have to change it from a Queryset to anything else; pandas.DataFrame can take any Iterable as data. So
df = pandas.DataFrame(djangoapp.models.Model.objects.all().values())
Gives you the DataFrame you expect. (though you may want to double check df.dtypes. If there are Nones in your data, the column may end up to be of object type.)
You can use list(…) to convert it to a list of dictionaries:
list(AnyObjects.objects.values())
You will need to serialize it with the json package to obtain a JSON blob, since strings with single quotes are not valid JSON, in order to make it a JSON blob, you can work with:
import json
json.dumps(list(AnyObjects.object.values()))

Django query date range by url

I have a model with an attribute tracking things by date added, and I want to be able to query for objects added after a certain date. There is a DateTimeFromToRangeFilter that looks useful, but I don't see any guidance on how to format my URLs to make use of it. How can I query for objects by a range of dates?
I doubt there is any native support for date-url mapping in Django, but it can be done with some effort.
For example, you may write your url mapping as follows in urls.py
url(r'^date-filter/(?P<from-date>\d{4}-\d{2}-\d{2})/(?P<to-date>\d{4}-\d{2}-\d{2})/$', views.date_filter, name='date_filter'),
And your views might look like this:
import dateutil.parser
def date_filter(request,from_date,to_date):
#You can then convert to_date and from_date to date-time objects
#Like this
to_date_object = parser.parse(to_date)
from_date_object = parser.parse(from_date)
Once you have the date-time objects, you can fire filter queries in any way you like.