标签归档:css

应用程序未拾取.css文件(烧瓶/ python)

问题:应用程序未拾取.css文件(烧瓶/ python)

我正在渲染一个模板,尝试使用外部样式表进行样式设置。文件结构如下。

/app
    - app_runner.py
    /services
        - app.py 
    /templates
        - mainpage.html
    /styles
        - mainpage.css

mainpage.html看起来像这样

<html>
    <head>
        <link rel= "stylesheet" type= "text/css" href= "../styles/mainpage.css">
    </head>
    <body>
        <!-- content --> 

我的样式均未应用。它与html是我正在渲染的模板有关吗?python看起来像这样。

return render_template("mainpage.html", variables..)

我知道这很有效,因为我仍然能够渲染模板。但是,当我尝试将样式代码从html的“ head”标记中的“样式”块移动到外部文件时,所有样式消失了,留下了一个裸露的html页面。有人看到我的文件结构有任何错误吗?

I am rendering a template, that I am attempting to style with an external style sheet. File structure is as follows.

/app
    - app_runner.py
    /services
        - app.py 
    /templates
        - mainpage.html
    /styles
        - mainpage.css

mainpage.html looks like this

<html>
    <head>
        <link rel= "stylesheet" type= "text/css" href= "../styles/mainpage.css">
    </head>
    <body>
        <!-- content --> 

None of my styles are being applied though. Does it have something to do with the fact that the html is a template I am rendering? The python looks like this.

return render_template("mainpage.html", variables..)

I know this much is working, because I am still able to render the template. However, when I tried to move my styling code from a “style” block within the html’s “head” tag to an external file, all the styling went away, leaving a bare html page. Anyone see any errors with my file structure?


回答 0

您需要有一个“静态”文件夹设置(用于css / js文件),除非您在Flask初始化期间专门覆盖了它。我假设您没有覆盖它。

您的CSS目录结构应类似于:

/app
    - app_runner.py
    /services
        - app.py 
    /templates
        - mainpage.html
    /static
        /styles
            - mainpage.css

请注意,您的/ styles目录应位于/ static下

然后做

<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/mainpage.css') }}">

Flask现在将在static / styles / mainpage.css下查找css文件

You need to have a ‘static’ folder setup (for css/js files) unless you specifically override it during Flask initialization. I am assuming you did not override it.

Your directory structure for css should be like:

/app
    - app_runner.py
    /services
        - app.py 
    /templates
        - mainpage.html
    /static
        /styles
            - mainpage.css

Notice that your /styles directory should be under /static

Then, do this

<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/mainpage.css') }}">

Flask will now look for the css file under static/styles/mainpage.css


回答 1

在jinja2模板(flask使用的模板)中,使用

href="{{ url_for('static', filename='mainpage.css')}}"

但是,这些static文件通常位于static文件夹中,除非另有配置。

In jinja2 templates (which flask uses), use

href="{{ url_for('static', filename='mainpage.css')}}"

The static files are usually in the static folder, though, unless configured otherwise.


回答 2

我已经阅读了多个主题,但都没有解决人们正在描述的问题,我也经历过。

我什至尝试离开conda并使用pip升级到python 3.7,我尝试了所有建议的编码,但均未解决。

这就是原因(问题):

默认情况下,python / flask在以下文件夹结构中搜索静态文件和模板:

/Users/username/folder_one/folder_two/ProjectName/src/app_name/<static>
 and 
/Users/username/folder_one/folder_two/ProjectName/src/app_name/<template>

您可以使用Pycharm(或其他任何工具)上的调试器自己进行验证,并检查应用程序上的值(app = Flask(name))并搜索teamplate_folder和static_folder

为了解决这个问题,您必须在创建应用程序时指定值,如下所示:

TEMPLATE_DIR = os.path.abspath('../templates')
STATIC_DIR = os.path.abspath('../static')

# app = Flask(__name__) # to make the app run without any
app = Flask(__name__, template_folder=TEMPLATE_DIR, static_folder=STATIC_DIR)

路径TEMPLATE_DIR和STATIC_DIR取决于文件应用程序所在的位置。就我而言,请参见图片,它位于src下的文件夹中。

您可以根据需要更改模板和静态文件夹,并在应用程序= Flask上注册…

实际上,当我弄乱文件夹时,我已经开始遇到问题,有时却无法正常工作。这可以彻底解决问题

html代码如下所示:

<link href="{{ url_for('static', filename='libraries/css/bootstrap.css') }}" rel="stylesheet" type="text/css" >

这个代码

这里的文件夹结构

I have read multiple threads and none of them fixed the issue that people are describing and I have experienced too.

I have even tried to move away from conda and use pip, to upgrade to python 3.7, i have tried all coding proposed and none of them fixed.

And here is why (the problem):

by default python/flask search the static and the template in a folder structure like:

/Users/username/folder_one/folder_two/ProjectName/src/app_name/<static>
 and 
/Users/username/folder_one/folder_two/ProjectName/src/app_name/<template>

you can verify by yourself using the debugger on Pycharm (or anything else) and check the values on the app (app = Flask(name)) and search for teamplate_folder and static_folder

in order to fix this, you have to specify the values when creating the app something like this:

TEMPLATE_DIR = os.path.abspath('../templates')
STATIC_DIR = os.path.abspath('../static')

# app = Flask(__name__) # to make the app run without any
app = Flask(__name__, template_folder=TEMPLATE_DIR, static_folder=STATIC_DIR)

the path TEMPLATE_DIR and STATIC_DIR depend on where the file app is located. in my case, see the picture, it was located within a folder under src.

you can change the template and static folders as you wish and register on the app = Flask…

In truth, I have started experiencing the problem when messing around with folder and at times worked at times not. this fixes the problem once and for all

the html code looks like this:

<link href="{{ url_for('static', filename='libraries/css/bootstrap.css') }}" rel="stylesheet" type="text/css" >

This the code

Here the structure of the folders


回答 3

仍然有下面由codegeek提供的解决方案后的问题:
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/mainpage.css') }}">

Google Chrome浏览器中,按下重新加载按钮(F5)不会重新加载静态文件。如果遵循了公认的解决方案,但仍然看不到对CSS所做的更改,请按ctrl + shift + R忽略缓存的文件并重新加载静态文件。

Firefox中,按下重新加载按钮会出现以重新加载静态文件。

Edge中,按刷新按钮不会重新加载静态文件。按ctrl + shift + R应该忽略缓存的文件并重新加载静态文件。但是,这在我的计算机上不起作用。

Still having problems after following the solution provided by codegeek:
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/mainpage.css') }}"> ?

In Google Chrome pressing the reload button (F5) will not reload the static files. If you have followed the accepted solution but still don’t see the changes you have made to CSS, then press ctrl + shift + R to ignore cached files and reload the static files.

In Firefox pressing the reload button appears to reload the static files.

In Edge pressing the refresh button does not reload the static file. Pressing ctrl + shift + R is supposed to ignore cached files and reload the static files. However this does not work on my computer.


回答 4

我正在运行flask的1.0.2版本。上面的文件结构对我不起作用,但是我找到了一个可以的文件结构,如下所示:

     app_folder/ flask_app.py/ static/ style.css/ templates/
     index.html

(请注意,“静态”和“模板”是文件夹,应命名为完全相同的名称。)

要检查您正在运行的烧瓶版本,应在终端中打开Python并相应地键入以下内容:

进口烧瓶

烧瓶-版本

I’m running version 1.0.2 of flask right now. The above file structures did not work for me, but I found one that did, which are as follows:

     app_folder/ flask_app.py/ static/ style.css/ templates/
     index.html

(Please note that ‘static’ and ‘templates’ are folders, which should be named exactly the same thing.)

To check what version of flask you are running, you should open Python in terminal and type the following accordingly:

import flask

flask –version


回答 5

烧瓶项目的结构不同。正如您所提到的,项目结构是相同的,但是唯一的问题是样式文件夹。样式文件夹必须位于静态文件夹内。

static/styles/style.css

The flask project structure is different. As you mentioned in question the project structure is the same but the only problem is wit the styles folder. Styles folder must come within the static folder.

static/styles/style.css

回答 6

还有一点要添加到该线程上。

如果在.css文件名中添加下划线,则将无法使用。

One more point to add on to this thread.

If you add an underscore in your .css file name, then it wouldn’t work.


回答 7

还有一点要添加。除了上面提到的答案外,请确保将以下行添加到app.py文件中:

app = Flask(__name__, static_folder="your path to static")

否则,flask将无法检测到静态文件夹。

One more point to add.Along with above upvoted answers, please make sure the below line is added to app.py file:

app = Flask(__name__, static_folder="your path to static")

Otherwise flask will not be able to detect static folder.


回答 8

如果以上任何方法都不起作用,并且您的代码是完美的,则请按Ctrl + F5尝试进行强制刷新。它将清除所有机会,然后重新加载文件。它为我工作。

If any of the above method is not working and you code is perfect then try hard refreshing by pressing Ctrl + F5. It will clear all the chaces and then reload file. It worked for me.


回答 9

我很确定它类似于Laravel模板,这就是我的方法。

<link rel="stylesheet" href="/folder/stylesheets/stylesheet.css" />

引用: CSS文件路径问题

一个简单的flask应用程序,它从.html文件读取其内容。外部样式表被阻止?

I’m pretty sure it’s similar to Laravel template, this is how I did mine.

<link rel="stylesheet" href="/folder/stylesheets/stylesheet.css" />

Referred: CSS file pathing problem

Simple flask application that reads its content from a .html file. External style sheet being blocked?


Js-beautify-javascript 格式化工具

JS 格式化

这个小美化器将重新格式化和重新缩进bookmarklet、丑陋的JavaScript、解压由Dean Edward的流行打包程序打包的脚本,以及对由NPM包处理的脚本进行部分去模糊处理javascript-obfuscator

我把这个放在最前面和中心,因为现有的业主目前在这个项目上的工作时间非常有限。这是一个很受欢迎并被广泛使用的项目,但它迫切需要有时间致力于修复面向客户的错误以及内部设计和实现的潜在问题的贡献者

如果您有兴趣,请看一下CONTRIBUTING.md然后修复标有“Good first issue”贴上标签并提交请购单。尽可能多地重复。谢谢!

安装

您可以安装node.js或python的美化器

Node.js JavaScript

您可以安装npm包。js-beautify全局安装时,它提供一个可执行文件js-beautify剧本。与Python脚本一样,美化结果被发送到stdout除非另有配置,否则

$ npm -g install js-beautify
$ js-beautify foo.js

您还可以使用js-beautify作为一个node库(本地安装,npm默认值):

$ npm install js-beautify

Node.js JavaScript(VNext)

以上安装了最新的稳定版本。要安装测试版或RC版,请执行以下操作:

$ npm install js-beautify@next

Web库

美容师可以作为Web库添加到您的页面上

JS美颜托管在两个CDN服务上:cdnjs和生菜

要从这些服务之一提取最新版本,请在您的文档中包含以下一组脚本标记:

“>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.0/beautify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.0/beautify-css.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.0/beautify-html.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.0/beautify.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.0/beautify-css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.0/beautify-html.min.js"></script>

<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.14.0/js/lib/beautify.js"></script>
<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.14.0/js/lib/beautify-css.js"></script>
<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.14.0/js/lib/beautify-html.js"></script>

通过更改版本号可以使用较旧的版本

免责声明:这些都是免费服务,所以有no uptime or support guarantees

python

要安装Python版本的美容器,请执行以下操作:

$ pip install jsbeautifier

与JavaScript版本不同,Python版本只能重新格式化JavaScript。它不适用于HTML或CSS文件,但您可以安装CSS-美化对于CSS:

$ pip install cssbeautifier

用法

您可以在Web浏览器中使用JS美化器美化javascript,也可以在命令行上使用node.js或python美化javascript

Web浏览器

打开beautifier.io选项通过UI提供

Web库

上面的脚本标记公开了三个函数:js_beautifycss_beautify,以及html_beautify

Node.js JavaScript

全局安装时,美容器提供一个可执行文件js-beautify剧本。美化结果发送到stdout除非另有配置,否则

$ js-beautify foo.js

要使用js-beautify作为一个node库(在本地安装之后),为javascript(Js)、CSS或HTML导入并调用适当的Beautifier方法。所有三个方法签名都是beautify(code, options)code是要美化的代码字符串。Options是一个具有您希望用来美化代码的设置的对象

配置选项名称与CLI名称相同,但使用下划线而不是破折号。例如,--indent-size 2 --space-in-empty-paren会是{ indent_size: 2, space_in_empty_paren: true }

var beautify = require('js-beautify').js,
    fs = require('fs');

fs.readFile('foo.js', 'utf8', function (err, data) {
    if (err) {
        throw err;
    }
    console.log(beautify(data, { indent_size: 2, space_in_empty_paren: true }));
});

python

安装后,要使用Python进行美化,请执行以下操作:

$ js-beautify file.js

美化的产出流向stdout默认情况下,

要使用jsbeautifier因为库很简单:

import jsbeautifier
res = jsbeautifier.beautify('your javascript string')
res = jsbeautifier.beautify_file('some_file.js')

或者,要指定一些选项,请执行以下操作:

opts = jsbeautifier.default_options()
opts.indent_size = 2
opts.space_in_empty_paren = True
res = jsbeautifier.beautify('some javascript', opts)

配置选项名称与CLI名称相同,但使用下划线而不是破折号。上面的示例将在命令行上设置为--indent-size 2 --space-in-empty-paren

选项

以下是Python和JS脚本的命令行标志:

CLI Options:
  -f, --file       Input file(s) (Pass '-' for stdin)
  -r, --replace    Write output in-place, replacing input
  -o, --outfile    Write output to file (default stdout)
  --config         Path to config file
  --type           [js|css|html] ["js"] Select beautifier type (NOTE: Does *not* filter files, only defines which beautifier type to run)
  -q, --quiet      Suppress logging to stdout
  -h, --help       Show this help
  -v, --version    Show the version

Beautifier Options:
  -s, --indent-size                 Indentation size [4]
  -c, --indent-char                 Indentation character [" "]
  -t, --indent-with-tabs            Indent with tabs, overrides -s and -c
  -e, --eol                         Character(s) to use as line terminators.
                                    [first newline in file, otherwise "\n]
  -n, --end-with-newline            End output with newline
  --editorconfig                    Use EditorConfig to set up the options
  -l, --indent-level                Initial indentation level [0]
  -p, --preserve-newlines           Preserve line-breaks (--no-preserve-newlines disables)
  -m, --max-preserve-newlines       Number of line-breaks to be preserved in one chunk [10]
  -P, --space-in-paren              Add padding spaces within paren, ie. f( a, b )
  -E, --space-in-empty-paren        Add a single space inside empty paren, ie. f( )
  -j, --jslint-happy                Enable jslint-stricter mode
  -a, --space-after-anon-function   Add a space before an anonymous function's parens, ie. function ()
  --space-after-named-function      Add a space before a named function's parens, i.e. function example ()
  -b, --brace-style                 [collapse|expand|end-expand|none][,preserve-inline] [collapse,preserve-inline]
  -u, --unindent-chained-methods    Don't indent chained method calls
  -B, --break-chained-methods       Break chained method calls across subsequent lines
  -k, --keep-array-indentation      Preserve array indentation
  -x, --unescape-strings            Decode printable characters encoded in xNN notation
  -w, --wrap-line-length            Wrap lines that exceed N characters [0]
  -X, --e4x                         Pass E4X xml literals through untouched
  --good-stuff                      Warm the cockles of Crockford's heart
  -C, --comma-first                 Put commas at the beginning of new line instead of end
  -O, --operator-position           Set operator position (before-newline|after-newline|preserve-newline) [before-newline]
  --indent-empty-lines              Keep indentation on empty lines
  --templating                      List of templating languages (auto,django,erb,handlebars,php,smarty) ["auto"] auto = none in JavaScript, all in html

它们对应于两个库接口的带下划线的选项键

每个CLI选项的默认值

{
    "indent_size": 4,
    "indent_char": " ",
    "indent_with_tabs": false,
    "editorconfig": false,
    "eol": "\n",
    "end_with_newline": false,
    "indent_level": 0,
    "preserve_newlines": true,
    "max_preserve_newlines": 10,
    "space_in_paren": false,
    "space_in_empty_paren": false,
    "jslint_happy": false,
    "space_after_anon_function": false,
    "space_after_named_function": false,
    "brace_style": "collapse",
    "unindent_chained_methods": false,
    "break_chained_methods": false,
    "keep_array_indentation": false,
    "unescape_strings": false,
    "wrap_line_length": 0,
    "e4x": false,
    "comma_first": false,
    "operator_position": "before-newline",
    "indent_empty_lines": false,
    "templating": ["auto"]
}

CLI中未显示的默认值

{
  "eval_code": false,
  "space_before_conditional": true
}

请注意,并非所有默认值都通过CLI显示。从历史上看,Python和JSAPI并不是100%相同的。还有一些其他情况使我们无法实现100%的API兼容性

从环境或.jsBeaufyrc加载设置(仅限JavaScript)

除了CLI参数之外,您还可以通过以下方式将配置传递给JS可执行文件:

  • 任何jsbeautify_-带前缀的环境变量
  • 一个JSON-由指示的格式化文件--config参数
  • 一个.jsbeautifyrc包含以下内容的文件JSON以上文件系统的任何级别的数据$PWD

此堆栈中前面提供的配置源将覆盖后面的配置源

设置继承和特定于语言的重写

这些设置是一个浅树,其值对于所有语言都是继承值,但可以被覆盖。这适用于在任一实现中直接传递给API的设置。在Javascript实现中,从配置文件(如.jsBeaufyrc)加载的设置也可以使用继承/覆盖

下面是一个示例配置树,显示了语言覆盖节点的所有支持位置。我们将使用indent_size要讨论此配置的行为方式,但可以继承或覆盖任意数量的设置,请执行以下操作:

{
    "indent_size": 4,
    "html": {
        "end_with_newline": true,
        "js": {
            "indent_size": 2
        },
        "css": {
            "indent_size": 2
        }
    },
    "css": {
        "indent_size": 1
    },
    "js": {
       "preserve-newlines": true
    }
}

使用上述示例将产生以下结果:

  • HTML文件
    • 继承indent_size从顶层设置开始,共4个空间
    • 这些文件还将以换行符结尾
    • HTML中的JavaScript和CSS
      • 继承HTMLend_with_newline设置
      • 将它们的缩进覆盖为2个空格
  • CSS文件
    • 将顶级设置重写为indent_size共1个空间
  • JavaScript文件
    • 继承indent_size从顶层设置开始,共4个空间
    • 设置preserve-newlinestrue

CSS和HTML

除了js-beautify可执行文件,css-beautifyhtml-beautify还提供了进入这些脚本的简单界面。或者,js-beautify --cssjs-beautify --html将分别完成相同的任务

// Programmatic access
var beautify_js = require('js-beautify'); // also available under "js" export
var beautify_css = require('js-beautify').css;
var beautify_html = require('js-beautify').html;

// All methods accept two arguments, the string to be beautified, and an options object.

CSS和HTML美化程序在范围上要简单得多,并且拥有的选项要少得多

CSS Beautifier Options:
  -s, --indent-size                  Indentation size [4]
  -c, --indent-char                  Indentation character [" "]
  -t, --indent-with-tabs             Indent with tabs, overrides -s and -c
  -e, --eol                          Character(s) to use as line terminators. (default newline - "\\n")
  -n, --end-with-newline             End output with newline
  -b, --brace-style                  [collapse|expand] ["collapse"]
  -L, --selector-separator-newline   Add a newline between multiple selectors
  -N, --newline-between-rules        Add a newline between CSS rules
  --indent-empty-lines               Keep indentation on empty lines

HTML Beautifier Options:
  -s, --indent-size                  Indentation size [4]
  -c, --indent-char                  Indentation character [" "]
  -t, --indent-with-tabs             Indent with tabs, overrides -s and -c
  -e, --eol                          Character(s) to use as line terminators. (default newline - "\\n")
  -n, --end-with-newline             End output with newline
  -p, --preserve-newlines            Preserve existing line-breaks (--no-preserve-newlines disables)
  -m, --max-preserve-newlines        Maximum number of line-breaks to be preserved in one chunk [10]
  -I, --indent-inner-html            Indent <head> and <body> sections. Default is false.
  -b, --brace-style                  [collapse-preserve-inline|collapse|expand|end-expand|none] ["collapse"]
  -S, --indent-scripts               [keep|separate|normal] ["normal"]
  -w, --wrap-line-length             Maximum characters per line (0 disables) [250]
  -A, --wrap-attributes              Wrap attributes to new lines [auto|force|force-aligned|force-expand-multiline|aligned-multiple|preserve|preserve-aligned] ["auto"]
  -i, --wrap-attributes-indent-size  Indent wrapped attributes to after N characters [indent-size] (ignored if wrap-attributes is "aligned")
  -d, --inline                       List of tags to be considered inline tags
  -U, --unformatted                  List of tags (defaults to inline) that should not be reformatted
  -T, --content_unformatted          List of tags (defaults to pre) whose content should not be reformatted
  -E, --extra_liners                 List of tags (defaults to [head,body,/html] that should have an extra newline before them.
  --editorconfig                     Use EditorConfig to set up the options
  --indent_scripts                   Sets indent level inside script tags ("normal", "keep", "separate")
  --unformatted_content_delimiter    Keep text content together between this string [""]
  --indent-empty-lines               Keep indentation on empty lines
  --templating                       List of templating languages (auto,none,django,erb,handlebars,php,smarty) ["auto"] auto = none in JavaScript, all in html

指令

指令允许您从源文件中控制美化器的行为。指令放在文件内部的注释中。指令的格式为/* beautify {name}:{value} */用CSS和JavaScript编写。在HTML中,它们的格式为<!-- beautify {name}:{value} -->

忽略指令

这个ignore指令使美化器完全忽略文件的一部分,将其视为不解析的文字文本。美化后以下输入保持不变:

// Use ignore when the content is not parsable in the current language, JavaScript in this case.var a =  1;/* beautify ignore:start */ {This is some strange{template language{using open-braces?/* beautify ignore:end */

保留指令

注意:此指令仅适用于HTML和JavaScript,不适用于CSS

这个preserve指令使美化器解析,然后保留一段代码的现有格式

美化后以下输入保持不变:

// Use preserve when the content is valid syntax in the current language, JavaScript in this case.
// This will parse the code and preserve the existing formatting.
/* beautify preserve:start */
{
    browserName: 'internet explorer',
    platform:    'Windows 7',
    version:     '8'
}
/* beautify preserve:end */

许可证

您可以自由地以任何您想要的方式使用它,以防您觉得它对您有用或有效,但您必须保留版权声明和许可证。(麻省理工学院)

学分

还要感谢杰森·戴蒙德、帕特里克·霍夫、诺姆·索松科、安德烈亚斯·施耐德、戴夫·瓦西列夫斯基、维塔利·巴特马诺夫、罗恩·鲍德温、加布里埃尔·哈里森、克里斯·J·舒尔、马蒂亚斯·拜恩斯、维托里奥·甘巴莱塔等人

(Readme.md:js-Beautify@1.14.0)