蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

对应完整实现:learning_logic.py

PYTHONWindows / Python3.12 / Dash4.5.0 / Plotly7.1.0 / Pandas3.1.0 / dbc2.0.4 / DAQ0.6.0 / Edge156learning_logic.py
learning_logic.py44 行
import math,base64,binascii,csv,io
from pathlib import PurePosixPath
from urllib.parse import parse_qs
from dash import no_update
from dash.exceptions import PreventUpdate
def square_message(value):
 if value is None or value=='':return '请输入一个数字。'
 if isinstance(value,bool):return '请输入有效数字。'
 try:number=float(value)
 except(TypeError,ValueError,OverflowError):return '请输入有效数字。'
 if not math.isfinite(number)or abs(number)>1000000:return '数字必须有限且绝对值不超过1000000。'
 return f'{number:g} 的平方是 {number*number:g}'
def echo_message(value):return '蓝色天空网 · 你输入了:'+(''if value is None else str(value))
def keep_nonempty(value):return no_update if value in(None,'')else echo_message(value)
def submitted(clicks,value):
 if not clicks:raise PreventUpdate
 return f'第{clicks}次提交:'+(''if value is None else str(value))
def upload_summary(contents,filename):
 if contents is None:return '尚未上传模拟CSV'
 name=PurePosixPath(str(filename or'').replace('\\','/')).name
 if not name.lower().endswith('.csv'):return '只接受CSV文件。'
 if not isinstance(contents,str)or len(contents)>12000:return '文件过大或格式错误。'
 try:
  header,data=contents.split(',',1)
  if header not in {'data:text/csv;base64','data:application/vnd.ms-excel;base64','data:application/octet-stream;base64'}:return '不支持的文件类型。'
  raw=base64.b64decode(data,validate=True)
  if len(raw)>8192:return '文件超过8192字节。'
  reader=csv.DictReader(io.StringIO(raw.decode('utf-8-sig')))
  if reader.fieldnames!=['topic','minutes']:return '列必须为topic,minutes。'
  rows=list(reader)
  if len(rows)>10:return '最多10行。'
  total=0
  for row in rows:
   if set(row)!={'topic','minutes'}:return 'CSV行列不匹配。'
   if not row['topic']or len(row['topic'])>60:return '课程名称无效。'
   value=row['minutes']
   if not isinstance(value,str)or not value.isascii()or not value.isdecimal()or not 0<=int(value)<=10000:return '分钟必须为0..10000整数。'
   total+=int(value)
  return f'{name}:{len(rows)}行,合计{total}分钟(模拟资料)。'
 except(ValueError,UnicodeError,binascii.Error,KeyError,TypeError,csv.Error):return '无法解析有效CSV。'
def query_topic(search):
 values=parse_qs((search or'').lstrip('?'),keep_blank_values=True).get('topic',['html'])
 return {'html':'HTML','css':'CSS','javascript':'JavaScript'}.get(values[0],'未识别课程')if len(values)==1 else'重复参数'
本例需在 Windows / Python3.12 / Dash4.5.0 / Plotly7.1.0 / Pandas3.1.0 / dbc2.0.4 / DAQ0.6.0 / Edge156 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows / Python3.12 / Dash4.5.0 / Plotly7.1.0 / Pandas3.1.0 / dbc2.0.4 / DAQ0.6.0 / Edge156

入口文件:projects/dash-lab/learning_logic.py

预期结果与验证条件观察提示
  • 按该章真实条件核对;其它控件组合/环境保持独立待处理

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:Dash

课程关联工程包(登记关联12章) · 含当前章节或实例 · mbskycom202611a496.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习