如何在Google Colaboratory Python Notebook中使用Flask? [英] How to use Flask in Google Colaboratory Python Notebook?

查看:98
本文介绍了如何在Google Colaboratory Python Notebook中使用Flask?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google Colab Python笔记本中使用Flask构建网站.但是,运行可在常规Python上运行的常规Flask代码无法在Google Colab上运行.我需要可以解决的代码..:)

I am trying to build a website using Flask, in a Google Colab Python notebook. However, running a regular Flask code that works on a regular Python, fails to work on Google Colab. I need code that will work it out please.. :)

推荐答案

服务器代码:

import socket
print(socket.gethostbyname(socket.getfqdn(socket.gethostname())))

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

import threading
threading.Thread(target=app.run, kwargs={'host':'0.0.0.0','port':80}).start() 

客户代码:

import requests
r = requests.get("http://172.28.0.2/")
print(r.status_code)
print(r.encoding)
print(r.apparent_encoding)
print(r.text)

要重新启动Flask,您可以点击菜单:运行时->重新启动运行时

To restart Flask you may click menu: runtime->restart runtime

共享链接此处:

这篇关于如何在Google Colaboratory Python Notebook中使用Flask?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆