惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
有赞技术团队
有赞技术团队
B
Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
Martin Fowler
Martin Fowler
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
博客园 - Franky
小众软件
小众软件

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟
使用4 行 Pyhon 代码扩展 Scratch
种瓜 · 2020-08-26 · via 夜行人

文章目录

使用 CodeLab Adapter 可以轻松扩展 Scratch 的能力,甚至只需要 4 行 Python 代码。

让我们使用 4 行 Python 代码来扩展 Scratch 的能力, 做一个反转字符串的例子: 将来自 Scratch 的字符串反转后返回

1
2
3
4
5
6
7
8
# pip install "codelab_adapter_client>=2.0.2"
from codelab_adapter_client.utils import run_monitor, send_simple_message

def monitor(msg):
    return msg[::-1]

# send_simple_message("hello")
run_monitor(monitor)

运行以上代码(如果你是新手,推荐使用Mu editor上运行)(依赖见文末)。如果使用 CodeLab Adapter 内置的JupyrtLab编程,则所有依赖都已经就绪.

现在我们可以在 CodeLab 创作平台中与之交互。

主动发送消息

前边例子展示了,使用 Python 代码扩展 Scratch 的能力,你可以使用任何第三方库!

前头的例子是一种服务模式, 响应 Scratch 过来的消息。

另有一种模式也很常见: 主动发送信息。

可以使用 send_simple_message 做到这点:

1
2
3
4
5
6
7
import time
from codelab_adapter_client.utils import run_monitor, send_simple_message


while True:
    time.sleep(5)
    send_simple_message("hi")

以上代码每隔 5 秒钟给 Scratch 发送一条信息:

稍加调整可以做一个番茄钟报时器。主意这是主动通知。

提醒

mu的最新版本(1.1.0) 已经支持安装第三方库

如果你遇到pip 的ssl问题,可以将 ~/.pip/pip.conf修改为:

1
2
3
[global]
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple