我想循环遍历列表,列表从零开始,但列表起始索引为1,例如:
valueList = [1, 2, 3, 4]
secondList = ['a', 'b', 'c', 'd']
for i, item in enumerate(valueList, start=1):
print(secondList[i])
Run Code Online (Sandbox Code Playgroud)
代码失败,索引超出范围错误(我意识到这是因为我在列表的长度-1加上起始值,并且python列表为零索引,因此使用i以这种方式调用另一个中的第i个元素列表无效).以下工作,但增加大于零的测试看起来是非pythonic.
valueList = [1, 2, 3, 4]
secondList = ['a', 'b', 'c', 'd']
for i, item in enumerate(valueList, start=0):
if i > 0:
print(secondList[i])
Run Code Online (Sandbox Code Playgroud)
枚举不是正确的选择,还有另一种方式吗?
在HTML中,我可以直接链接到页面上的特定位置,假设有一个具有给定id的元素:
<a href="http://www.example.com/stuff.html#exactlocation">Go there</a>
Run Code Online (Sandbox Code Playgroud)
在Flask中我尝试添加锚点render_template但是我得到了jinja2.exceptions.TemplateNotFound: stuff.html#exactlocation.
@main.route('/exactlocation')
def exactlocation():
return render_template('stuff.html#exactlocation')
Run Code Online (Sandbox Code Playgroud)
如何链接到模板中的特定位置?
我正在使用nginx(通过gunicorn)为flask应用提供静态文件。
默认静态文件夹中的静态文件可以正常工作:
<link rel="stylesheet" href="{{ url_for('static', filename='css/fa/font-awesome.min.css') }}" />
Run Code Online (Sandbox Code Playgroud)
但是对于我想限制登录用户访问权限的其他静态文件,我使用的是Flask提供的静态文件夹:
app.register_blueprint(application_view)
application_view = Blueprint('application_view', __name__, static_folder='application_static')
Run Code Online (Sandbox Code Playgroud)
在HTML中,我这样调用一个静态文件:
<link rel="stylesheet" href="{{ url_for('application_view.static', filename='css/main.css') }}" />
Run Code Online (Sandbox Code Playgroud)
然后在application / application_static中,我有受限制的静态文件。这在本地Flask安装上运行良好,但是当我使用Nginx从/ static文件夹提供文件的方式部署到生产计算机时,出现“网络错误:404未找到-website.com/application_static/main.css”。
关于如何配置Ngix来处理此问题的任何想法?
conf.d / mysitename.conf文件:
upstream app_server_wsgiapp {
server localhost:8000 fail_timeout=0;
}
server {
listen 80;
server_name www.mysitename.com;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
server_name www.mysitename.com;
listen 443 ssl;
#other ssl config here
access_log /var/log/nginx/www.mysitename.com.access.log;
error_log /var/log/nginx/www.mysitename.com.error.log info;
keepalive_timeout 5;
# nginx serve up static files and never send to the WSGI …Run Code Online (Sandbox Code Playgroud) 要在 Bokeh 中创建直方图,我可以使用:
p = Histogram(results, yscale="linear", bins=50, title = 'hist plot')
show(p)
Run Code Online (Sandbox Code Playgroud)
但 yscale 的选项只有“线性”、“分类”、“日期时间”
关于如何创建具有对数刻度的直方图有什么想法吗?
我希望能够检测自上次登录 Flask 站点后用户是否关闭了浏览器。我已经读过 Flask 会话应该在关闭浏览器时过期,但据我所知,会话数据存储在服务器上,并且在浏览器会话中持续存在。用户关闭浏览器时如何清除所有会话数据?
主应用程序.py:
@mainapp.route('/')
def home():
if 'user_name' in session:
logger.debug( 'Logged in as {0}'.format(escape(session['user_name'])))
return render_template('home.html')
Run Code Online (Sandbox Code Playgroud)
用户视图.py:
@userviews.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm(request.form)
if form.validate_on_submit():
#get user from db
session['user_name'] = user.user_name
Run Code Online (Sandbox Code Playgroud)
设置.py:
app.secret_key = 'somethingreallysecret'
Run Code Online (Sandbox Code Playgroud) 我正在从子组件向其父组件发送事件.我想通过父节点中的方法拦截信号.但是无论是否发出事件,监听功能始终触发.请注意,我使用的是单个文件组件和Vue-router.
另外,我发现很少有VueJS示例使用单个文件组件,对于noob,从一个文件中的简单Vue应用程序转换为多个单个文件组件可能会令人困惑.
家长:
<template>
....html here
</template>
<script>
import Child from './Child.vue'
export default {
name: 'Parent',
data () {
return {
stage: 1
}
},
components: {
Child
},
created: function () {
// the following line always runs even when listen for non-existent event eg this.$on('nonsense'...)
this.$on('child-event', this.stage = 2)
}
}
Run Code Online (Sandbox Code Playgroud)
儿童:
<template>
<button v-on:click="sendEvent" type="button" class="btn btn-success">Next</button>
</template>
<script>
export default {
name: 'Child',
data () {
return {
response_status: 'accepted'
}
},
methods: {
sendEvent: …Run Code Online (Sandbox Code Playgroud) 我正在尝试从另一个模块导入一个函数,但在运行时出现错误:
TypeError: _this.getData is not a function.
Run Code Online (Sandbox Code Playgroud)
数据.js
function getData() {
return [
{ id: 1,
name: 'Pluto',
type: 'Dwarf Planet'
},
{ id: 2,
name: 'Neptune',
type: 'Planet'
}
]
}
export { getData }
Run Code Online (Sandbox Code Playgroud)
工人.js
import getData from data.js
this.data = this.getData()
Run Code Online (Sandbox Code Playgroud)
然后在运行时,我收到了上面提到的浏览器错误。关于我做错了什么的任何想法?
我无法将contentEditable元素显示为一个固定的框(例如2个字符宽).因为它只是填充的宽度.当框中有文本时工作正常,但我希望有一个宽度为一的空盒子.有任何想法吗?
HTML:
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="so.css" />
<body>
<h1 id="prompt">Type something in the box:</h1>
<h1 id="textarea"></h1>
</body>
Run Code Online (Sandbox Code Playgroud)
CSS:
#prompt, #textarea{
display:inline;
}
#textarea{
contentEditable: true;
width: 2em; /* 20px doesn't work either */
cursor: text;
background-color:rgba(132, 225, 132, 0.9);
border: 1px solid black;
padding: 2px;
}
Run Code Online (Sandbox Code Playgroud) 我试图在散景散点图中反转 y 轴并设置 x 和 y 的范围。我在用:
BokehPlot.bokeh_scatter(data=df, x_range=(min_utc, max_utc), y_range=(min_val, max_val))
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
TypeError: bokeh_scatter() got an unexpected keyword argument 'x_range'
Run Code Online (Sandbox Code Playgroud)
任何想法如何在散景散点图中使用熊猫数据框输入反转轴
我正在使用ESLint来检查构建时的javascript代码,我得到一个no-unused-vars错误,然后是同一个变量的no-undef错误.我无法确定变量如何既未使用也未定义.
export function count(){
if (counter > 3){
const a = 'big';
} else {
const a = 'small';
}
return a;
}
Run Code Online (Sandbox Code Playgroud)
鉴于上面的伪代码,我从ESLint得到以下错误:
line 3 error 'a' is defined but never used no-unused-vars
line 5 error 'a' is defined but never used no-unused-vars
line 7 error 'a' is not defined no-undef
Run Code Online (Sandbox Code Playgroud)
有关如何绕过这个的任何想法?