小编Dav*_*vik的帖子

Python Dash:自定义 CSS

我想将 CSS 样式表或<style>块提供给 Python Dash 应用程序。我试图在下面做这两项,但都不适合我。应用程序加载正常,但文本保持黑色,而不是绿色。

import dash

from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
from flask import send_from_directory



# define the app
app = dash.Dash()

app.head = [html.Link(rel='stylesheet', href='./static/stylesheet.css'),
    ('''
    <style type="text/css">
    h1 {
        color:green;
    }
    </style>
    ''')]

app.layout = html.Div(html.H1('Hello World!'))


if __name__ == '__main__':
    app.run_server(debug=True)
Run Code Online (Sandbox Code Playgroud)

里面./static/stylesheet.css是一个只有这个的文件:

h1{
  color:green;
}
Run Code Online (Sandbox Code Playgroud)

我试过只有样式表或<style>标签,但它们都没有将 h1 标签变成绿色。

这是我为解决我的问题所做的研究:

https://github.com/plotly/dash/pull/171

https://dash.plot.ly/external-resources

https://github.com/plotly/dash-recipes/blob/master/dash-local-css-link.py

我唯一没有尝试过的(那些链接建议)是从外部链接 (CDN) 加载。但是我希望能够离线加载这个应用程序,所以这不是一个选项。

css python plotly-dash

8
推荐指数
2
解决办法
2万
查看次数

Python Flask“ send_file()”方法TypeError

我正在尝试读取用户上传的图像,然后将图像显示回给他们。我想要执行此操作而不保存上载的图像文件。

我有这样的代码:

    from flask import Flask, redirect, render_template, request, url_for, send_file
    from PIL import Image, ImageDraw
    from io import BytesIO

    app = Flask(__name__)

        @app.route('/', methods=['GET', 'POST']) 
        def index():
            if request.method == 'POST':    
                 img = Image.open(request.files['file'].stream)
                 byte_io = BytesIO()
                 img.save(byte_io, 'PNG')
                 byte_io.seek(0)
                 return send_file(byte_io, mimetype='image/png')
Run Code Online (Sandbox Code Playgroud)

它产生此错误:

TypeError: send_file() got an unexpected keyword argument 'mimetype'

我尝试用mimetype其他有效参数替换,它只会给出相同的错误,但带有新参数的名称。所以我认为问题出在我的bytes_io

更新:

为了澄清,send_file()我指的是内置flask.send_file()方法:

python flask

6
推荐指数
1
解决办法
2308
查看次数

BeautifulSoup:如果找不到HTML元素,则返回None

我正在使用BeautifulSoup在网页中搜索多个元素.

我正在保存我找到的元素,但是因为我的脚本有可能会查找一个元素,并且它对于要解析的特定页面不存在,我对每个元素都有try/except语句:

# go through a bunch of webpages
for soup in soups:
    try: # look for HTML element
         data['val1'].append(soup.find('div', class_="something").text)
    except: # add NA if nothing found
        data['val1'].append("N/A")
    try:
        data['val2'].append(soup.find('span', class_="something else").text)
    except:
        data['val2'].append("N/A")

    # and more and more try/excepts for more elements of interest
Run Code Online (Sandbox Code Playgroud)

是否有更清洁或更好的方式来写这样的东西?

python beautifulsoup

5
推荐指数
1
解决办法
1978
查看次数

标签 统计

python ×3

beautifulsoup ×1

css ×1

flask ×1

plotly-dash ×1