Home » Python » python – return a generated HTML file in django rest framework-Exceptionshub

python – return a generated HTML file in django rest framework-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have the following view, which takes in the data passed in by the user and returns an HTML file (folium map).

I have three questions:

firstly, this view raises an error, in the browser.

TypeError at /locations/
'LocationInfo' object is not iterable

and second,
How do I return the generated HTML file to the user?

third,
I want that when the user inputs the data, the logic will run and return said HTML file.

My business logic here uses the values inputted by the user to plot the map and generates an HTML file saved in the directory, I can return the path to said file, or I have made another option that opens the HTML in another window automatically.

# views.py 

from rest_framework.viewsets import ModelViewSet
from .serializers import LocationInfoSerializer

from .models import LocationInfo
from three_Positions_plotting.app import main


def map_info_to_logic(gdt1, gdt2, uav):
    html_file = main(gdt1=gdt1, gdt2=gdt2, uav=uav)
    return html_file


class LocationInfoViewSet(ModelViewSet):
    queryset = LocationInfo.objects.latest('date_added')
    serializer_class = LocationInfoSerializer

    serializer = LocationInfoSerializer(queryset, many=False)
    values = list(serializer.data.values())

    gdt1 = [values[1], values[2]]
    gdt2 = [values[2], values[3]]
    uav = [values[4], values[5]]

    data = map_info_to_logic(
        gdt1=gdt1,
        gdt2=gdt2,
        uav=uav
    )

My logic running point:

import numpy as np
from Project_Level.angle_condition import MeetAngleCond
from Project_Level.plot_folium import PlotOnMap
from Project_Level.utils import convert_to_xy
from triangulationapi.three_Positions_plotting.dataframes import GetDataToGeoPandas
from triangulationapi.three_Positions_plotting.positions_data_collecting import PositionsData


def main(gdt1: list, gdt2: list, uav: list):
    # Get the complete latitude, longitude, lists.
    positions_data = PositionsData(gdt1=gdt1,
                                   gdt2=gdt2,
                                   uav=uav)
    full_lat_lon_list, lat_list, lon_list = positions_data.calculate_list_lens()

    # Get cartesian coordinates for every point.
    gdt1_xy = np.asarray(convert_to_xy(gdt1))
    gdt2_xy = np.asarray(convert_to_xy(gdt2))

    # Get the angle for every point in f_lat_lon_list
    angles_list = MeetAngleCond()
    plot_angles_list = angles_list.create_angles_list(lat_lon_list=full_lat_lon_list,
                                                      arrayed_gdt1=gdt1_xy,
                                                      arrayed_gdt2=gdt2_xy,
                                                      uav_elev=uav[-1])

    get_final_lists = GetDataToGeoPandas()
    lat_lon_a, lat_lon_b, optimal = get_final_lists.create_gpd_and_final_lists(angles_list=plot_angles_list,
                                                                               lat_list=lat_list,
                                                                               lon_list=lon_list)
    # Initialize a folium map.
    plot = PlotOnMap(lat_lon_a=lat_lon_a,
                     lat_lon_b=lat_lon_b,
                     optimal=optimal)
    plot.create_map(mid_point=gdt1, zoom=13)

    # Plot relevant locations.
    plot.plot_gdt_and_triangulation(gdt1=gdt1, gdt2=gdt2, uav=uav)

    # add some plugins to the map.
    plot.plugins()

    # return the generated html file with the map.
    html_file = plot.return_html_link()

    # auto opens the file.
    #auto_open = plot.auto_open(html_map_file='index.html',
    #                          path='/home/yovel/PycharmProjects/Triangulation-#Calculator/triangulationapi/three_Positions_plotting/index'
    #                               '.html '
    #                          )
How to&Answers: