NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

class ZipcodePriceFilterAPIView(APIView):
def get(self, request):

try:
zipcode = request.query_params.get('zipcode')
min_price = request.query_params.get('min_price')
max_price = request.query_params.get('max_price')
queryset = Activity.objects.filter(zipcode=zipcode)
if not zipcode:
return Response({'error': 'Zipcode parameter is required.'}, status=status.HTTP_400_BAD_REQUEST)

queryset = Activity.objects.filter(zipcode=zipcode)

listing_mls_ids = [activity.listing_mls_id for activity in queryset]
price_list = [activity.listing_price for activity in queryset]
average_price = sum(price_list) /
len(price_list) if price_list else 0

if min_price and max_price:
queryset = queryset.filter(
listing_price__gte=min_price, listing_price__lte=max_price)
elif min_price:
queryset = queryset.filter(listing_price__gte=min_price)
elif max_price:
queryset = queryset.filter(listing_price__lte=max_price)

serializer = ActivitySerializer(queryset, many=True)

# Create the histogram
dff = pd.DataFrame.from_dict(serializer.data)
dff.to_csv("MyData.csv", index=False) # os.path(setting.BASE_DIR,
avg_price = dff['listing_price'].astype('float64').mean()
id_list=dff['listing_mls_id'].tolist()
price_list=dff['listing_price'].astype('float64').tolist()

id_list.append('Average_Val')
price_list.append(avg_price)

# Plot the histogram
plt.figure(figsize=(26,10))
plt.bar(id_list, price_list)

# set labels and title
plt.xlabel('Listing MLS ID')
plt.ylabel('Listing Price')
plt.title('Histogram of Listing Price by MLS ID')

# Define the path where you want to save the image
# save_directory = "/home/admin1/Documents/django project/listingservice/listingapplication/static/"#os.path.join(settings.STATIC_ROOT,"Images") # Change this to your desired path
save_directory = "/home/admin1/Documents/django project/listingservice/listingapplication/media/Images"#os.path.join(settings.MEDIA_ROOT,"Images") # Change this to your desired path
os.makedirs(save_directory, exist_ok=True) # Create the directory if it doesn't exist
plot_path = os.path.join(save_directory, 'listing_price_histogram.png')

# plot_path = os.path.join(settings.MEDIA_ROOT, 'listing_price_histogram.png')
# os.makedirs(plot_path, exist_ok=True) # Create the directory if it doesn't exist
# print(settings.MEDIA_ROOT)

# Save the plot to a file
plt.savefig(plot_path)
plt.close() # Close the plot to free memory

# Construct the URL for the saved plot
# plot_url = request.build_absolute_uri(f'{settings.STATIC_URL}Images/listing_price_histogram.png')
plot_url = f'{settings.MEDIA_URL}Images/listing_price_histogram.png'
print("polt:",plot_url)

return Response({
'activities': serializer.data,
'plot_url': plot_url
}, status=status.HTTP_200_OK)


except Exception as e:
print('?????',e)
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)


class AvragePriceOfZipcodeAPIView(APIView):
def get(self, request):
zipcode = request.data.get('zipcode')
if zipcode:
queryset = Activity.objects.filter(zipcode=zipcode)
price_list = [activity.listing_price for activity in queryset]
average_price = sum(price_list) /
len(price_list) if price_list else 0
return Response({'average_price': average_price})
else:
return Response({'error': 'Zipcode parameter is required.'}, status=400)


def filter_by_zipcode_price(request):
if request.method == 'GET':
zipcode = request.GET.get('zipcode')
min_price = request.GET.get('min-price')
max_price = request.GET.get('max-price')

# Construct the API endpoint URL with the provided parameters
api_url = 'http://127.0.0.1:8000/api/filter-by-zipcode-price-api/'
params = {'zipcode': zipcode,
'min_price': min_price,
'max_price': max_price}
# Make the API request
response = requests.get(api_url, params=params)

# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
data = response.json()
plot_url = data.get('plot_url', None)

# Pass the data to the template
context = {
'listings': data['activities'],
'plot_url': plot_url
}
return render(request, 'listing-data.html', context)
else:
# Handle the error case
# return render(request, 'error.html', {'error_message': 'Error fetching data from the API'})
return render(request, 'filter-by-zipcode.html')

return render(request, 'filter-by-zipcode.html')
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.