当前位置: 首页 > 工具软件 > Frozen-Flask > 使用案例 >

python flask服务器搭建_怎么使用python flask搭建静态服务器

相云
2023-12-01

Static files handled by Flask for your application or any of its blueprints.

Views with no variable parts in the URL, if they accept the GET method.

New in version 0.6: Results of calls to flask.url_for() made by your application in the request for another URL. In other words, if you use url_for() to create links in your application, these links will be “followed”.

This means that if your application has an index page at the URL / (without parameters) and every other page can be found from there by recursively following links built with url_for(), then Frozen-Flask can discover all URLs automatically and you’re done.

Otherwise, you may need to write URL generators.

URL generators

Let’s say that your application looks like this:

@62616964757a686964616fe59b9ee7ad9431333363393734app.route('/')def products_list():

return render_template('index.html', products=models.Product.all())@app.route('/product_/')def product_details():

product = models.Product.get_or_404(id=product_id)

return render_template('product.html', product=product)

If, for some reason, some products pages are not linked from another page (or these links are not built by url_for()), Frozen-Flask will not find them.

To tell Frozen-Flask about them, write an URL generator and put it after creating your Freezer instance and before calling freeze():

@freezer.register_generatordef product_details():

for product in models.Product.all():

yield {'product_id': product.id}

Frozen-Flask will find the URL by calling url_for(endpoint, **values) whereendpoint is the name of the generator function and values is each dict yielded by the function.

You can specify a different endpoint by yielding a (endpoint, values) tuple instead of just values, or you can by-pass url_for and simply yield URLs as strings.

Also, generator functions do not have to be Python generators using yield, they can be any callable and return any iterable object.

All of these are thus equivalent:

@freezer.register_generatordef product_details():  # endpoint defaults to the function name

# `values` dicts

yield {'product_id': '1'}

yield {'product_id': '2'}@freezer.register_generatordef product_url_generator():  # Some other function name

# `(endpoint, values)` tuples

yield 'product_details', {'product_id': '1'}

yield 'product_details', {'product_id': '2'}@freezer.register_generatordef product_url_generator():

# URLs as strings

yield '/product_1/'

yield '/product_2/'@freezer.register_generatordef product_url_generator():

# Return a list. (Any iterable type will do.)

return [

'/product_1/',

# Mixing forms works too.

('product_details', {'product_id': '2'}),

]

Generating the same URL more than once is okay, Frozen-Flask will build it only once. Having different functions with the same name is generally a bad practice, but still work here as they are only used by their decorators. In practice you will probably have a module for your views and another one for the freezer and URL generators, so having the same name is not a problem.

Testing URL generators

The idea behind Frozen-Flask is that you can use Flask directly to develop and test your application. However, it is also useful to test your URL generators and see that nothing is missing, before deploying to a production server.

You can open the newly generated static HTML files in a web browser, but links probably won’t work. The FREEZER_RELATIVE_URLS configuration can fix this, but adds a visible index.html to the links. Alternatively, use the run() method to start an HTTP server on the build result, so you can check that everything is fine before uploading:

if __name__ == '__main__':

freezer.run(debug=True)

Freezer.run() will freeze your application before serving and when the reloader kicks in. But the reloader only watches Python files, not templates or static files. Because of that, you probably want to use Freezer.run() only for testing the URL generators. For everything else use the usual app.run().

Flask-Script may come in handy here.

Controlling What Is Followed

Frozen-Flask follows links automatically or with some help from URL generators. If you want to control what gets followed, then URL generators should be used with the Freezer’s with_no_argument_rules and log_url_for flags. Disabling these flags will force Frozen-Flask to use URL generators only. The combination of these three elements determines how much Frozen-Flask will follow.

Configuration

Frozen-Flask can be configured using Flask’s configuration system. The following configuration values are accepted:

FREEZER_BASE_URL

Full URL your application is supposed to be installed at. This affects the output of flask.url_for() for absolute URLs (with _external=True) or if your application is not at the root of its domain name. Defaults to 'http://localhost/'.

FREEZER_RELATIVE_URLS

If set to True, Frozen-Flask will patch the Jinja environment so that url_for() returns relative URLs. Defaults to False. Python code is not affected unless you use relative_url_for() explicitly. This enables the frozen site to be browsed without a web server (opening the files directly in a browser) but appends a visible index.html to URLs that would otherwise end with /.

New in version 0.10.

FREEZER_DEFAULT_MIMETYPE

The MIME type that is assumed when it can not be determined from the filename extension. If you’re using the Apache web server, this should match the DefaultTypevalue of Apache’s configuration.  Defaults to application/octet-stream.

New in version 0.7.

FREEZER_IGNORE_MIMETYPE_WARNINGS

If set to True, Frozen-Flask won’t show warnings if the MIME type returned from the server doesn’t match the MIME type derived from the filename extension. Defaults to False.

New in version 0.8.

FREEZER_DESTINATION

Path to the directory where to put the generated static site. If relative, interpreted as relative to the application root, next to the static and templates directories. Defaults to build.

FREEZER_REMOVE_EXTRA_FILES

If set to True (the default), Frozen-Flask will remove files in the destination directory that were not built during the current freeze. This is intended to clean up files generated by a previous call to Freezer.freeze() that are no longer needed. Setting this to False is equivalent to setting FREEZER_DESTINATION_IGNORE to ['*'].

New in version 0.5.

FREEZER_DESTINATION_IGNORE

A list (defaults empty) of fnmatch patterns. Files or directories in the destination that match any of the patterns are not removed, even if FREEZER_REMOVE_EXTRA_FILES is true. As in .gitignore files, patterns apply to the whole path if they contain a slash /, to each slash-separated part otherwise. For example, this could be set to ['.git

 类似资料: