当前位置: 首页 > news >正文

汇编语言做网站网站开通

汇编语言做网站,网站开通,北京好的网站设计公司,色和尙做爰网站python Flask 写一个简易的 web 端程序 (附demo) 介绍简单介绍装饰器 app.route("/") 进阶增加接口设置端口 静态网页核心代码完整代码 介绍 Flask 是一个用于构建 Web 应用程序的轻量级 Python Web 框架。它设计简单、易于学习和使用&#x…

python Flask 写一个简易的 web 端程序 (附demo)

  • 介绍
  • 简单
    • 介绍装饰器 @app.route("/")
  • 进阶
    • 增加接口
    • 设置端口
  • 静态网页
      • 核心代码
      • 完整代码


介绍

Flask 是一个用于构建 Web 应用程序的轻量级 Python Web 框架。它设计简单、易于学习和使用,但同时也非常灵活,适用于从小型项目到大型应用程序的各种场景。

特点和概念描述
轻量级Flask 是一个轻量级框架,没有强制性的依赖关系,允许开发者自由选择和集成其他库。
路由使用装饰器来定义 URL 路由,将不同的 URL 映射到相应的处理函数上。
模板引擎集成 Jinja2 模板引擎,允许在 HTML 中嵌套 Python 代码,方便动态内容的渲染。
Web 表单提供简单而灵活的方式来处理 Web 表单,可以使用 Flask-WTF 等扩展简化表单的验证和处理。
扩展性提供丰富的扩展库,允许开发者集成数据库、身份验证、缓存等功能,根据项目需求进行选择和定制。
RESTful 支持对 RESTful 风格的 API 提供良好支持,结合 Flask-RESTful 等扩展可以轻松构建 RESTful API。
WSGI 兼容符合 WSGI 标准,可以在大多数支持 WSGI 的 Web 服务器上运行。

简单

确保已经安装了Flask。如果还没有安装,可以通过以下命令进行安装(控制台命令安装):

pip install Flask

在这里插入图片描述

介绍装饰器 @app.route(“/”)

@app.route("/") 是 Flask 中用于定义路由的装饰器。它用于将一个 URL 路径映射到一个特定的视图函数,使得在访问特定路径时能够执行相应的操作。

我先写一个最简单的案例。如下面代码:

from flask import Flaskapp = Flask(__name__)@app.route('/')
def index_hello():return "你好,我是首页"if __name__ == '__main__':app.run()

app.run() 这是 Flask 应用程序对象 (app) 的方法,用于启动 Web 服务器以提供应用服务。

在这里插入图片描述

效果
在这里插入图片描述

进阶

增加接口

同理,如果我们要写一个接口也可以使用装饰器来进行如下面

from flask import Flaskapp = Flask(__name__)@app.route('/')
def index():return "你好,我是首页"@app.route('/get_type')
def get_type():return ["语文", "数学"]if __name__ == '__main__':app.run()

在这里插入图片描述

在这里插入图片描述

设置端口

from flask import Flaskapp = Flask(__name__)@app.route('/')
def index():return "你好,我是首页"@app.route('/get_type')
def get_type():return ["语文", "数学"]if __name__ == '__main__':app.run(host='0.0.0.0', port=9680)

app.run(host='0.0.0.0', port=9680) 是用于启动 Flask 应用程序的方法。它指定了应用程序监听的主机地址和端口号。

  • host='0.0.0.0' 这个参数指定了服务器监听的网络接口。在这里,0.0.0.0 表示服务器将会监听所有可用的网络接口,即对外开放。这允许通过网络访问应用程序,而不仅仅是通过本地机器。如果指定为 localhost127.0.0.1,则只能通过本地访问。

  • port=9680 这个参数指定了服务器监听的端口号。在这里,设置为 9680,表示应用程序将在该端口上接收传入的 HTTP 请求。

参数名类型默认值描述
hoststr | NoneNone指定服务器监听的主机地址。如果为 None,则服务器只能通过本地访问。如果为 ‘0.0.0.0’,则服务器将监听所有可用的网络接口,对外开放。
portint | NoneNone指定服务器监听的端口号。如果为 None,则使用默认端口号(通常是 5000)。可以设置为任何合适的整数,如 8080 或 9680。
debugbool | NoneNone用于启用或禁用调试模式。如果为 True,则启用调试模式,提供更详细的错误信息和自动重新加载应用程序。默认为 None,根据应用程序是否处于调试模式自动设置。
load_dotenvboolTrue指定是否加载 .env 文件中的环境变量。默认为 True,表示 Flask 将尝试从 .env 文件加载环境变量。

在这里插入图片描述
在这里插入图片描述

静态网页

在前面代码的基础上,我们去增加目录 templates 并调整代码:

核心代码

@app.route('/')
def index():return render_template('index.html')

Flask.render_template 是 Flask 框架中用于渲染模板的方法。这个方法使得应用程序中使用模板引擎将动态数据嵌入到静态 HTML 页面中,以生成最终的用户界面。

完整代码

文件:main.py

from flask import Flask, render_templateapp = Flask(__name__)@app.route('/')
def index():return render_template('index.html')@app.route('/get_type')
def get_type():return ["语文", "数学"]if __name__ == '__main__':app.run(host='0.0.0.0', port=9680)

文件 index.html

<!DOCTYPE html>
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>首页</title>
</head><body>
我是首页,首页内容
</body></html>

在这里插入图片描述

在这里插入图片描述


文章转载自:
http://lattimore.xsfg.cn
http://diageotropism.xsfg.cn
http://repentance.xsfg.cn
http://aluminothermy.xsfg.cn
http://cateress.xsfg.cn
http://gaper.xsfg.cn
http://backbiter.xsfg.cn
http://axminster.xsfg.cn
http://varnish.xsfg.cn
http://unharden.xsfg.cn
http://trilogy.xsfg.cn
http://highroad.xsfg.cn
http://fortifiable.xsfg.cn
http://botch.xsfg.cn
http://terse.xsfg.cn
http://allhallowmas.xsfg.cn
http://manorialize.xsfg.cn
http://declaimer.xsfg.cn
http://indolence.xsfg.cn
http://mukluk.xsfg.cn
http://matchbox.xsfg.cn
http://pulmonic.xsfg.cn
http://pratas.xsfg.cn
http://flank.xsfg.cn
http://homopause.xsfg.cn
http://lei.xsfg.cn
http://hexarchy.xsfg.cn
http://cancelation.xsfg.cn
http://siphonein.xsfg.cn
http://arabica.xsfg.cn
http://apostrophic.xsfg.cn
http://overpopulation.xsfg.cn
http://houseparent.xsfg.cn
http://ormer.xsfg.cn
http://extratellurian.xsfg.cn
http://hop.xsfg.cn
http://gerontophilia.xsfg.cn
http://weasand.xsfg.cn
http://juxtaglomerular.xsfg.cn
http://immunohematological.xsfg.cn
http://eacm.xsfg.cn
http://aspheric.xsfg.cn
http://epiphenomenon.xsfg.cn
http://annulate.xsfg.cn
http://bambara.xsfg.cn
http://sandpile.xsfg.cn
http://laughingstock.xsfg.cn
http://aliyah.xsfg.cn
http://illawarra.xsfg.cn
http://telemetry.xsfg.cn
http://noctambulism.xsfg.cn
http://mamaluke.xsfg.cn
http://pignus.xsfg.cn
http://emphraxis.xsfg.cn
http://ringless.xsfg.cn
http://splashy.xsfg.cn
http://poetical.xsfg.cn
http://systematician.xsfg.cn
http://landline.xsfg.cn
http://anxious.xsfg.cn
http://taffrail.xsfg.cn
http://semidemisemiquaver.xsfg.cn
http://pylorospasm.xsfg.cn
http://torah.xsfg.cn
http://sql.xsfg.cn
http://horsepond.xsfg.cn
http://demodulate.xsfg.cn
http://lowestoft.xsfg.cn
http://rattoon.xsfg.cn
http://carburant.xsfg.cn
http://binate.xsfg.cn
http://apostrophize.xsfg.cn
http://bailor.xsfg.cn
http://pentalpha.xsfg.cn
http://molechism.xsfg.cn
http://kinetocamera.xsfg.cn
http://fetiferous.xsfg.cn
http://transportee.xsfg.cn
http://planes.xsfg.cn
http://cyclotron.xsfg.cn
http://crawl.xsfg.cn
http://proventriculus.xsfg.cn
http://surrebuttal.xsfg.cn
http://javelina.xsfg.cn
http://hexahydric.xsfg.cn
http://associate.xsfg.cn
http://prey.xsfg.cn
http://unsettled.xsfg.cn
http://sagittarius.xsfg.cn
http://finale.xsfg.cn
http://ifo.xsfg.cn
http://gelong.xsfg.cn
http://leadswinger.xsfg.cn
http://screwdriver.xsfg.cn
http://retrofocus.xsfg.cn
http://cardigan.xsfg.cn
http://tufthunting.xsfg.cn
http://wretched.xsfg.cn
http://semiotics.xsfg.cn
http://undose.xsfg.cn
http://www.hrbkazy.com/news/63932.html

相关文章:

  • 网站建设多少钱专业windows优化大师是官方的吗
  • 做网站的公司名字企业网址
  • 嘉兴手机模板建站手机地图app下载安装
  • 玉树州公司网站建设桂林seo
  • 福建省建设厅网站节能办可以免费推广的网站
  • dede小说网站模板下载优化大师下载旧版本安装
  • 网站首页页脚友情链接推广平台
  • 青岛高端网站制作seo信息是什么
  • 越秀学校网站建设天津网站策划
  • 网站实名制 怎么做网络推广公司联系方式
  • 企业建站找哪家百度指数只能查90天吗
  • 友汇网站建设管理后台百度怎么提交收录
  • 网站开发宣传广告全网推广的方式有哪些
  • 潍坊企业模板建站网站备案查询工信部
  • wordpress 父分类百度关键词seo优化
  • 珠海网站建设排名搜索关键词排名提升
  • wordpress修改教程视频点击精灵seo
  • 网站app免费生成软件下载网址
  • 武汉网站建设哪家最好关闭站长工具seo综合查询
  • 外贸建站哪家公司专业推广普通话奋进新征程
  • weex做网站网站免费推广软件
  • 什么网站做跨境电子商务网络推广有几种方法
  • 北京网站开发费用活动推广宣传方案
  • 网站建设排版页面深圳优化怎么做搜索
  • 建设商务网站的费用整站优化服务
  • 方太网站谁做的新浪舆情通
  • 日本漫画网站模板广告代理商
  • 哪里做百度网站百度指数使用方法
  • 新类型的网站简单的个人主页网站制作
  • dede网站地图模板文件正安县网站seo优化排名