mdwiki开发之路二资源与踩坑记录
发布日期:2025-04-13 12:53:18 浏览次数:11 分类:精选文章

本文共 3292 字,大约阅读时间需要 10 分钟。

1、:

如果你没有艺术细胞,偷懒的方法就是到这上面去找,比如登录框界面等。侧边栏选用提到的资源,其他一些资源可能需要进一步探索。

2、DIV的CSS height:100%无效的解决办法:

在css中增加上:

html, body{ margin:0; height:100%; }

这样可以确保body和html元素的高度覆盖整个页面,避免空隙问题。

3、:

在env.py中设置render_as_batch=True

context.configure(    connection=connection,    target_metadata=target_metadata,    render_as_batch=True)

这个设置可以提高数据处理效率,适用于大数据量的处理场景。

4、:

比较有用的功能包括Table of Contents(toc)、CodeHilite(代码高亮)、Meta-Data(文件前面可以添加元数据,比如标题,作者等)、New Line to Break(换行即新行,而不是像原生markdown那样得换两行)、Tables(表格插件)等。

5、关于Flask的:

默认情况下,flask session在你关闭浏览器的时候失效。你可以通过设置permanent session来改变这一行为。

from datetime import timedeltafrom flask import session, app@app.before_requestdef make_session_permanent():    session.permanent = True    app.permanent_session_lifetime = timedelta(minutes=30)

默认情况下,permanent_session_lifetime是31天。

6、关于SQLAlchemy:

建立关系时,可以使用Restaurant模型来关联Dish模型:

class Restaurant(db.Model):    ...    dishes = db.relationship('Dish', secondary=restaurant_dish,        backref=db.backref('restaurants'))

这样可以检索所有属于某个餐厅的菜品:

x = Dish.query.filter(Dish.restaurants.any(name=name)).all()

生成的SQL语句如下:

SELECT dish.*FROM dishWHERE    EXISTS (        SELECT 1        FROM restaurant_dish        WHERE            dish.id = restaurant_dish.dish_id            AND EXISTS (                SELECT 1                FROM restaurant                WHERE                    restaurant_dish.restaurant_id = restaurant.id                    AND restaurant.name = :name            )    )

7、:

关于延迟导入(lazy import),可以采取以下方法:

1. 将import语句写在方法或函数里面,将它的作用域限制在局部。

2. 将from xxx import yyy改成import xxx; xxx.yyy来访问的形式。

3. 对于出现循环import的问题,应该检查代码布局是否合理,必要时可以合并或分离竞争资源。

8、关于Python的一些:

如何检查局部变量的存在:

if 'myVar' in locals():  # myVar exists.

如何检查全局变量的存在:

if 'myVar' in globals():  # myVar exists.

如何检查对象属性的存在:

if hasattr(obj, 'attr_name'):  # obj.attr_name exists.if('attr_name' in dir(obj)):    pass

还有一种不太优雅但有效的方式是通过捕获异常的方式:

try:    myVarexcept NameError:    myVar = None# Now you're free to use myVar without Python complaining.

9、关于Git与Github

项目放在github上,是否经常被识别为javascript项目?知乎这篇问答给出了答案。问题原因:github 是根据项目里文件数目最多的文件类型,识别项目类型。解决办法:项目根目录添加 .gitattributes 文件,内容如下:

*.js linguist-language=python

作用:把项目里的 .js 文件,识别成 python 语言。

10、关于IDE的:

选择合适的IDE是开发效率的重要因素。根据项目需求,可以选择Sublime Text、IntelliJ IDEA、VS Code等。推荐使用Sublime Text,因为它轻量且高效,适合前端开发。

11、关于Celery的:

Celery的周期性任务scheduler需要配置beat和运行beat进程,但仅仅运行beat进程是不够的。正确的做法是同时运行一个worker。对于周期性任务beat,必须同时运行beat和worker;而对于其他类型的任务,只需运行worker即可。

12、:

如果采用gunicorn命令行的形式:

gunicorn -w 4 -b 127.0.0.1:4000 -k gevent -e aliyun_api_key=value,SECRET_KEY=mysecretkey app:app

或者采用gunicorn.conf.py文件的形式:

import multiprocessingbind = "127.0.0.1:4000"workers = multiprocessing.cpu_count() * 2 + 1worker_class='gevent'proc_name = "mdwiki"user = "nginx"chdir='/opt/mdwiki'#daemon=False#group = "nginx"loglevel = "info"#errorlog = "/home/myproject/log/gunicorn.log"#accesslog=raw_env = [   'aliyun_api_key=value',   'aliyun_secret_key=value',   'MAIL_PASSWORD=value',   'SECRET_KEY=mysecretkey',]#ssl#keyfile=#certfile=#ca_certs=

如果采用supervisor配置环境变量:

[program:mdwiki]environment=SECRET_KEY=value,aliyun_api_key=value,aliyun_secret_key=value,MAIL_PASSWORD=valuecommand=/usr/bin/gunicorn -n mdwiki -w 4 -b 127.0.0.1:4000 -k gevent app:app directory=/opt/mdwikiuser=nginxautostart=trueautorestart=trueredirect_stderr=true
上一篇:MDX Cookbook 12 - 计算 SMA 简单移动平均 LastPeriods() 函数的使用
下一篇:MDT 2013 从入门到精通之自动化部署配置

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年05月18日 03时51分07秒