记仇本展示
如题所述,项目已经写好了,是基于local_storage
存储在本地的项目
该项目运行时是基于brython
, 你可能想问,为什么不使用原生python
来编写网页呢,这个有个误区是,网页是由html
代码编写的,而原生python
想要操作DOM
非常难,所以brython
正是为这个来操作的。
初始打开页面,因为没有数据展示,所以只有一个增加按钮。
当我们点击【画个圈圈记下来】按钮后,会刷新为新增页面,例如:
此时,我们只需要输入信息,比如 记老婆的仇,缘由为 买switch游戏透露给丈母娘,还得被骂。
此时点击记仇,就可以存储到页面上了。
此时若点击已原谅,则可以删除该记录。
brython 之 local_storage
你可能细心发现了,哎,关掉了浏览器,下次打开,怎么还会有记录在上面呢,这是因为用了local_storage
,那么,什么是local_storage
呢?
哎,我们使用的是brython
中的local_storage
但是,它可不是python
定义的哦,而是HTML 5
提供的存储数据的API之一,可以在浏览器中保持键值对数据块。
现在来展示使用一下brython
存储和删除的操作。
导入库:
from browser.local_storage import storage
存储数据,例如键值信息juejinName
存储为pdudo
storage[juejinName] = "pdudo"
查询的话,直接使用storage[变量]
就好,若为空,则返回None
v = storage[juejinName]
循环所有的key
,需要引入window
库,再使用for...in
来完成
from browser import window for key in window.localStorage: print(key)
也可以直接使用for...in
来遍历storage
而删除数据呢?只需要像删除字典一下
del storage[juejinName]
storage
是不是操作起来和字典非常类似呢?减少了开发者的学习成本。
制作项目
有了上述前置条件后,我们再看该项目,便可以总结为 针对localStorage
的增删查,首先当页面加载出来的时候,我们需要先遍历一下localstorage
数据,从而映射为一个table
,例如:
for key in window.localStorage: tr = html.TR() datas = json.loads(storage[key]) delBtn = html.BUTTON("已原谅") delBtn.dataset["id"] = datas["id"] delBtn.className = "confirm-btn" delBtn.bind("click",delGrudges) td = html.TD(delBtn+" "+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(datas["id"])))) tr <= td for tdVal in datas["whos"],datas["Text"]: td = html.TD(tdVal) tr <= td tb <= tr userWindows <= tb
上述代码是遍历localStorage
,而后在定义删除按钮,等将其他值组合完毕后,全部加载进table
中,而后再页面上显示。
而添加数据呢?
def saveGrudges(ev): getWhoVal = document["whos"].value getTextVal = document["textArea"].value if getWhoVal == "" or getTextVal == "": return document["saveBtn"].unbind("click") ids = int(time.time()) datas = { "id": ids, "whos": getWhoVal, "Text": getTextVal } storage[str(ids)] = json.dumps(datas)
上述代码,显示获取input
和textarea
框中的值,再判断是否用户没有输入,我们将数据组装为一个字典,而后转换为字符串,再存入localstage
中。
还有其他操作,这个可以直接看代码说明,brython
很简单的。
以上是怎么用Python编写个有趣的记仇本的详细内容。更多信息请关注PHP中文网其他相关文章!

Python脚本在Unix系统上无法运行的原因包括:1)权限不足,使用chmod xyour_script.py赋予执行权限;2)Shebang行错误或缺失,应使用#!/usr/bin/envpython;3)环境变量设置不当,可打印os.environ调试;4)使用错误的Python版本,可在Shebang行或命令行指定版本;5)依赖问题,使用虚拟环境隔离依赖;6)语法错误,使用python-mpy_compileyour_script.py检测。

使用Python数组比列表更适合处理大量数值数据。1)数组更节省内存,2)数组对数值运算更快,3)数组强制类型一致性,4)数组与C语言数组兼容,但在灵活性和便捷性上不如列表。

列表列表更好的forflexibility andmixDatatatypes,何时出色的Sumerical Computitation sand larged数据集。1)不可使用的列表xbilese xibility xibility xibility xibility xibility xibility xibility xibility xibility xibility xibles and comply offrequent elementChanges.2)

numpymanagesmemoryforlargearraysefefticefticefipedlyuseviews,副本和内存模拟文件.1)viewsAllowSinglicingWithOutCopying,直接modifytheoriginalArray.2)copiesCanbecopy canbecreatedwitheDedwithTheceDwithThecevithThece()methodervingdata.3)metservingdata.3)memore memore-mappingfileShessandAstaStaStstbassbassbassbassbassbassbassbassbassbassbb

Listsinpythondonotrequireimportingamodule,helilearraysfomthearraymoduledoneedanimport.1)列表列表,列表,多功能和canholdMixedDatatatepes.2)arraysaremoremoremoremoremoremoremoremoremoremoremoremoremoremoremoremoremeremeremeremericdatabuteffeftlessdatabutlessdatabutlessfiblesible suriplyElsilesteletselementEltecteSemeTemeSemeSemeSemeTypysemeTypysemeTysemeTypysemeTypepe。

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

禅工作室 13.0.1
功能强大的PHP集成开发环境