How to display a google map in flask/django? - python

There's not much to be said about it except for the absence of any form of documentation having the least possible understandable way of doing it. Google's documentation only includes JS snippets that are good enough if I know how to use them in flask, django, or whatever python web framework.

With Flask and Django you make HTML pages in the end, and from Flask you can just return some HTML with the parameters you need.
The Google maps embed API uses an iframe, so you can display a map on your page like this:
<iframe
width="600"
height="450"
style="border:0"
loading="lazy"
allowfullscreen
referrerpolicy="no-referrer-when-downgrade"
src="https://www.google.com/maps/embed/v1/place?key=API_KEY
&q=Space+Needle,Seattle+WA">
</iframe>
"You can set the Maps Embed API URL as the src attribute of an iframe" says the doc with that in https://developers.google.com/maps/documentation/embed/get-started
In your Flask or Django view thing, you can then have for example the geocoordinates of the map place you want, gmaps has had a nice API for ages.
For adding markers, heatmaps etc. from code, the Maps client side JS api works, so you'd do that on the client but can get data for the front JS code from your Python backend. I have a simple old gmaps app that is browser side JS only, reads the marker data from a JSON, in https://github.com/playsign/insideoulu/blob/master/map.js

There is an extension in flask it only requires
Jinja
Flask
A google api key
And to use in your project use thy dependency manager and pip-install it like this
pip install flask-googlemaps
For how-to-use check out their documentation here it's pretty straightforward.

Related

Conver Python to html using Django

I have a Python program for customers to query price. Each time a customer can input some necessary information, the program will calculate and return the price to customer. Note: during the calculation process, the program also need to query a third party map service web API to get some information (such as google map API or other similar service).
I have a website developed using web development tools such Wix, Strikingly. It offers a capability to customize a web page by simply input a block of HTML codes. So, I want to study the possibility of using Django to convent my python program into HTML (incl. add some user interface such as text box and button), which can then be pasted into the website to form a unique webpage.
I am not sure if it is doable? Especially, the part of connecting third party map service API. Would Django be able to convert this part automatically to HTML as well? (how does it deal with API key and connection).
Python itself runs only on the console, and is meant to be the backend in site development, whereas HTML is meant only to be the frontend, so no calculation or data fetching. Wix is a frontend tool with some content management that offers customization but still in the frontends (html/css), and there's nothing more you could do with the content management other than using the built in table like feature. Trying to use the html generated by wix will be so much pain due to its css name optimization and making it quite unscalable.
If you don't wish to learn frontend building at all then you could look up other html generator tool for the frontend codes. From there, django itself is capable of building the entire website, using the html you generated as template, and passing the data you've computed into the templates. That's what Django is meant to do. In this case you would need to learn Django itself, which I would recommend if you intend to showcase your project as an interactive program rather just console logs.
Other alternatives include converting your python codes into javascript, which is capable of doing calculations and fetching from APIs, and you can include the javascript code directly in HTML with the tag.

How can I embed Superset Apache into Flask web app?

I have a flask web application with many pages where users can insert some data stored in mysql using sqlalchemy. I would create another page with an interactive dashboard. I googled and I found superset that it seems perfect for my app. It's possible embed apache superset (or similar) into my web app? There are some tutorial?
It is possible to embed Apache Superset in any web application using iframe. Copy the URL of the dashboard that you want to embed and add ?standalone=true at the end. This URL can be used in the iframe to render the application.
Problem with the solution:
In the above mentioned method you will be seeing login page inside the iframe when you access the dashboard. You will have to login to the web application and then login to Superset.
Possible solutions
You can make the dashboard public as mentioned in the doc: https://superset.incubator.apache.org/security.html?highlight=public#public
Implement a custom authenticator as mentioned in the post: https://medium.com/#sairamkrish/apache-superset-custom-authentication-and-integrate-with-other-micro-services-8217956273c1
I would recommend the second approach as it would give protection to your data.

Django and React

I haven't been able to locate anything saying one way or the other, but does anyone know if you can have function based views in django and have react as your frontend? No use of templates.
If so, would it look like this?
# fad index route
def fad_index(request):
context = {'fads': Fad.objects.all()}
return render(request, 'fad/fad_index.html', context)
But instead of fad_index.html, would it just be the route that you want it to take in react?
When you use Django views, whether class or function based, you are required to render html responses, so there is no point on using React this way since the front-end functionality is already embedded in the html you are rendering.
React is thought for creating front-end applications that run independently from the back-end application. For that, the back-end application must provide specific url points for fetching or posting data from and to the front-end. These url points are known as end-points, and this back-end architecture is known as API, and come in two main flavors, SOAP and REST, and differ, basically, in the format of the data that comes from and goes to the front-end. SOAP APIs use xml, and REST APIs use json.
If you have a REST API back-end, you can use ajax in a React front-end to fetch and post data from and to the back-end's end-points.
For making a REST API back-end using Django, you need to complement it with another library called Django Rest Framework, that allows Django views to render json packages instead of html, so your React front-end can use these end-points.
React is simply a component composition library. You can use it to render single components on a web page or as the rendering layer of a more complex Single Page Application (SPA)
The general best practice when creating single page applications is to send the data over in a JSON format using a REST API. JSON because it can be parsed in almost any language and REST because the server should not care what the client is specifically doing.
There is a module called django rest framework that allows you to return raw data from REST style endpoints in a JSON format.

how to show a django application inside a site in wordpress

I have a site made in wordpress and i´d like to show my django application inside this site. I don´t know how to do that. How do call my django application and show it inside the site.
Your other option - far more work and technical than using an iframe - is to show the content in the WordPress site by using django-rest-framework (on the Django app) then pulling in the content via API requests.
It would be a lot of work, but well worth learning and provide you the most flexibility.
The steps would be
Install django-rest-framework for your Django app
Setup up and configure an API
Either use PHP on the server or JavaScript on the frontend to pull in the content from the API
Only using iframes, because they bore are diffrerent frameworks using different languages etc... so you will make to make one iframe inside your wordpress page and inside that page to load one url from your django application
https://www.w3schools.com/tags/tag_iframe.asp
try to detail your issue. Do you need a link in your wordpress´site ? have you tried loading it using iframe ?
It´s really important to show inside a wordpress´site ?

Facebook, Django, and Google App Engine

I'm experimenting with app-engine-patch (Django for GAE) on Google App Engine. And I would like to write a Facebook application. Is it possible to use PyFacebook and its middleware? Or is there some other solution?
I run a system on for social networks and facebook on GAE with back-end in Python, front end in Javascript and Flash. I use mostly client side js libraries to pass data back to the server side datastore. This library for facebook to be exact: http://code.google.com/p/facebookjsapi/
There is a reason for this. Most of what we are doing will be running on its own site, in iframes in different social networks and in widgets etc. But for the most part this has worked very well. It is good because we can swap out our backend at any time or even run it on multiple platforms as it is also using a python rest GAE library but any backend would do with this setup.
Adding the Facebook directory from the PyFacebook install directory to the app-engine-patch application allows you to add 'facebook.djangofb.FacebookMiddleware', to the MIDDLEWARE_CLASSES in settings.py. Then your view can use 'import facebook.djangofb as facebook' and '#facebook.require_login().'
I haven't gone end to end, but when I tried to display the view preceded by '#facebook.require_login()', I was redirected to the Facebook login.
According to this post, you need a slightly modified PyFacebook that you can download from a URL given in said post (I haven't tried it myself, though).
Edit: that link is wrong -- better link and more discussion on this thread.

Categories

Resources