简体   繁体   English

烧瓶和OpenLayers

[英]Flask and OpenLayers

I have a web application which is deployed by the means of Flask. 我有一个通过Flask部署的Web应用程序。 I'm using the OpenLayers map which requests a stylesheet in the folder scriptdir/theme/default. 我正在使用OpenLayers映射,该映射请求文件夹scriptdir / theme / default中的样式表。 I'm not so happy with my solution, so I hope someone can give me a hint to do it better: 我对我的解决方案不太满意,所以希望有人能给我一个提示以使其做得更好:

@app.route('/theme/default/style.css')
def get_openlayers_css():
   return url_for('static', filename='jslib/theme/default/style.css')

The javascript console shows me this warning: javascript控制台向我显示此警告:

Resource interpreted as Stylesheet but transferred with MIME type text/html: http://localhost:5000/theme/default/style.css
   OpenLayers.Map.OpenLayers.Class.initialize                 OpenLayers.js:424
   ...

The requested stylesheet is located in /static/jslib/theme/default/style.css and the OpenLayers.js file in /static/jslib. 所需的样式表位于/static/jslib/theme/default/style.css中,而OpenLayers.js文件位于/ static / jslib中。 I thought about streaming the css file, but I'm new to flask and therefore I was not able to get this working. 我曾考虑过流式处理css文件,但是我是flask的新手,因此无法正常工作。

Thanks in advance! 提前致谢!

Greetings 问候

you'd better configure your map with theme: null [1] option, which will disable the autoloading of the css, and load it by yourself in the page. 您最好使用theme: null [1]选项配置地图,这将禁用css的自动加载,并自行在页面中加载。

1 - http://dev.openlayers.org/docs/files/OpenLayers/Map-js.html#OpenLayers.Map.theme 1- http://dev.openlayers.org/docs/files/OpenLayers/Map-js.html#OpenLayers.Map.theme

OpenLayers has a weird way of locating the base URL for its resources (see source code ). OpenLayers有一种奇怪的方式来为其资源找到基本URL(请参阅源代码 )。 If you are using the "unpacked" version (not single-file) then OpenLayers.js needs to be inside a folder named lib , otherwise the URL auto-detect will fail. 如果您使用的是“解压缩”版本(而非单个文件),则OpenLayers.js必须位于名为lib的文件夹中,否则URL自动检测将失败。

Also, your get_openlayers_css function is returning a text response containing the URL; 另外,您的get_openlayers_css函数将返回一个包含URL的文本响应。 you probably wanted to write a redirect: 您可能想编写重定向:

@app.route('/theme/default/style.css')
def get_openlayers_css():
   return redirect(url_for('static', filename='jslib/theme/default/style.css'))

It's a normal behaviour. 这是正常现象。 Your method def get_openlayers_css(): returns a URL as string with default MIME type TEXT . 您的方法def get_openlayers_css():以默认MIME类型TEXT返回URL作为字符串。

Try returning the CSS content instead of the path with Flask.send_from_directory . 尝试返回CSS内容,而不是Flask.send_from_directory的路径。

Have a look at the example in Flask documentation . 看一下Flask文档中的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM