from flask import request
user_ip = request.headers.get('CF-Connecting-IP')
https://api.qiwi.com/partner/bill/v1/bills/<тут_billId>
<ul class="nav navbar-nav navbar-right">
<li {{ home_active }}><a href="/">Home</a></li>
<li {{ places_active }}><a href="/places">Places</a></li>
</ul>
@app.route('/')
@app.route('/home')
def index():
return render_template('index.html', home_active="class=active")
@app.route('/places')
def map():
return render_template('places.html', places_active="class=active")
def some_view():
return render_template('template.html', active='home')
<li class="{% if active=='home' %}active{%endif %}">Home</li>
<li class="{% if active=='blog' %}active{%endif %}">Blog</li>
{% extends "layout.html" %}
{% set active_page = "index" %}
{% set navigation_bar = [
('/', 'index', 'Index'),
('/downloads/', 'downloads', 'Downloads'),
('/about/', 'about', 'About')
] -%}
{% set active_page = active_page|default('index') -%}
...
<ul id="navigation">
{% for href, id, caption in navigation_bar %}
<li{% if id == active_page %} class="active"{% endif
%}><a href="{{ href|e }}">{{ caption|e }}</a></li>
{% endfor %}
</ul>
Т.е. app.run(host='0.0.0.0') согласно туториалу не работает.
$ tree
.
├── main.py
└── wsgi.py
# wsgi.py
from main import app
if __name__ == "__main__":
app.run()
# main.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'У меня работает! Что то ты делаешь не так.'
if __name__ == "__main__":
app.run(host='0.0.0.0')
$ gunicorn --bind 0.0.0.0:5000 wsgi:app
Так вот, какой способ лучшее всего подойдёт небольшому сайту?
Пользователю необходимо предоставить доступ к определенной директории на сервере, откуда он может выбрать файл, который он хочет скачать.
def make_tree(path):
tree = dict(name=path, children=[])
try: lst = os.listdir(path)
except OSError:
pass #ignore errors
else:
for name in lst:
fn = os.path.join(path, name)
if os.path.isdir(fn):
tree['children'].append(make_tree(fn))
else:
tree['children'].append(dict(name=fn))
return tree
@app.route('/uploads/<path:filename>', methods=['GET', 'POST'])
def download(filename):
uploads = os.path.join(current_app.root_path, app.config['UPLOAD_FOLDER'])
return send_from_directory(directory=uploads, filename=filename)
flask его просто не видит
# структура проекта
├── api.py
├── app.py
├── config.py
├── __init__.py
├── start.sh
├── templates
├── venv
└── views.py
# api.py
from flask import Blueprint, jsonify
api = Blueprint('api', __name__)
@api.route('/')
def index():
return jsonify({'api': {'version': 1.1}})
# app.py
from flask import Flask
from .views import blog
from .api import api
app = Flask(__name__)
app.config.from_pyfile('config.py')
app.register_blueprint(blog)
app.register_blueprint(api, url_prefix='/api/v1')
# views.py
from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound
blog = Blueprint('blog', __name__, template_folder='templates')
@blog.route('/')
def show():
try:
return render_template('index.html')
except TemplateNotFound:
abort(404)
# start.sh
#!/bin/sh
. venv/bin/activate
export FLASK_ENV=development
python -m flask run
with Image.open('sts.png').convert("RGBA") as your_image:
do_things(your_image)
def draw_text_on_image(text,
input_file='image.png',
output_file='new_image.png',
dimensions=(1047, 377),
insert_position=(900, 50),
):
with Image.open(input_file).convert("RGBA") as your_image:
temporary_image = Image.new('RGBA', dimensions, (255, 255, 255, 0))
draw = ImageDraw.Draw(temporary_image)
draw.text(insert_position, text, fill=(200, 0, 0, 255))
del draw
output = Image.alpha_composite(your_image, temporary_image)
output.save(output_file)
for filename in os.listdir(WATCH_DIRECTORY):
file_data = os.path.join(WATCH_DIRECTORY, filename)
try:
with open(file_data, 'rb') as file:
data_local = pickle.load(file)
file.close()
print(data_local) # здесь вывод данных с одного файла, открытого в данный момент
g.data_local = data_local # здесь значение g.data_local затирается последним (на каждой итерации)
return g.data_local().all()