运行python需要网吗-如何在网络浏览器中运行python脚本并处理结果?

1586010002-jmsa.png

I have a written a short python script which takes a text and does a few things with it. For example it has a function which counts the words in the text and returns the number.

How can I run this script within django?

I want to take that text from the view (textfield or something) and return a result back to the view.

I want to use django only to give the script a webinterface. And it is only for me, maybe for a few people, not for a big audience. No deployment.

Edit: When I first thought the solution would be "Django", I asked for it explicitly. That was of course a mistake because of my ignorance of WSGI. Unfortunately nobody advised me of this mistake.

解决方案

First off, is your heart really set on it being Django? If not I'd advise that Django, whilst an awesome framework, is a bit much for your needs. You don't really need full stack.

You might want to look at Flask instead, which is a Python micro-framework (and dead easy to use)

However, since you asked about Django...

You can create a custom Django command (docs here) that calls your script,

that can be called from a view as described in this question.

This has the added benefit of allowing you to run your script via the Django management.py script too. Which means you can keep any future scripts related to this project nice and uniform.

For getting the results of your script running, you can get them from the same bit of code that calls the command (the part described in the last link), or you can write large result sets to a file and process that file. Which you choose would really depend on the size of your result set and if you want to do anything else with it afterwards.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值