Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

jinja2 - Get an OpenLayer map in a Flask app template

I'm trying to figure out how to insert an OpenLayers map in a small Flask application.

The structure of this app is as follow:

.
├── app
│   ├── __init__.py
│   ├── forms.py
│   ├── routes.py
│   ├── static
│   │   ├── css
│   │   │   └── style.css
│   │   ├── favicon.ico
│   │   ├── images
│   │   └── js
│   │       ├── map.js
│   │       ├── node_modules # <--- OL is installed in an `ol` folder here inside.
│   │       └── package-lock.json
│   └── templates
│       ├── base.html
│       ├── index.html
│       └── map.html
├── app.py
├── config.py
├── gunicorn_conf.py
├── start_app.sh
└── wsgi.py

I have two extendable blocks in base.html:

    <div class="container">
      {% block content %} {% endblock %}
    </div>

    <div class="map" id="map">
     {% block map %} {% endblock %}
    </div>

The first deserve some textual information input.

And I obviously want to insert the map into the map .

Here's map.html (I guess the templating engine is jinja2):

{% extends 'base.html' %}

{% block map %}
<h2>Hello World Map!</h2>
    <script src="{{url_for('static', filename='js/map.js')}}"></script>
{% endblock %}

And here's map.js, taken from the OpenLayers examples:

// I have to give the full path to each ol import here:
import 'node_modules/ol/ol.css';   
import Map from 'node_modules/ol/Map';
import TileLayer from 'node_modules/ol/layer/Tile';
import TileWMS from 'node_modules/ol/source/TileWMS';
import View from 'node_modules/ol/View';
import {ScaleLine, defaults as defaultControls} from 'node_modules/ol/control';

var layers = [
  new TileLayer({
    source: new TileWMS({
      url: 'https://ahocevar.com/geoserver/wms',
      params: {
        'LAYERS': 'ne:NE1_HR_LC_SR_W_DR',
        'TILED': true,
      },
    }),
  }) ];

var map = new Map({
  controls: defaultControls().extend([
    new ScaleLine({
      units: 'degrees',
    }) ]),
  layers: layers,
  target: 'map',
  view: new View({
    projection: 'EPSG:3857',
    center: [12, 56],
    zoom: 4,
  }),
});

Everything seems to work fine as I can see the loading with status = 200 in my browser network monitor, the title 'Hello World Map!' is well rendered, and when I click to open map.js from the network monitor in a new tab, the raw map.js text content is displayed.

So it's there. But it seems not interpreted by the browser as my page stays... without any map of any kind. A blank page (with the titles and the text, that's ok, but no map). And no error.
I can't figure out what's wrong. How do you debug such error-less errors?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...