Search code examples
pythondjangofolium

Manipulate string data inside an api response


I have an API in my DRF which sends data in a HTML String

Views.py

class map(APIView):

    def get(self, request):
        ....
        data = pd.DataFrame({'lat': latitude, 'lon': longitude, 'name': markers_list})
        m = folium.Map(location=[21, 78], tiles="OpenStreetMap", zoom_start=4.75)
        for i in range(0, len(data)):
            folium.Marker([data.iloc[i]['lon'], data.iloc[i]['lat']], popup=data.iloc[i]['name']).add_to(m)

        print(" m is ", m)

        html_string = m._repr_html_()
        context = {'map2': html_string}


        return Response(context)

And the context is:

{
    "map2": "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh...cHQ+\"
style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
}

In my Iframe I just need data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh...cHQ+ which is in front of <iframe src=\ in the response, How Can I retrieve this data?


Solution

  • try splitting your "map2" key like this :

    input.split("\"")
    

    it will generate array with datas splitted like :

    array = ['...', '...', 'data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh...cHQ+\' ]
    

    in this case, the datas you want is at the 3rd position so array[2]