How to use Flask in Google Colaboratory Python Notebook?

夙愿已清 提交于 2019-12-06 04:08:20

问题


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.. :)


回答1:


the server code:

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() 

client code:

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

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

share link: https://colab.research.google.com/drive/1ocoD0B0h2F8HhwWz9j9Td4uhvoTk_Um2



来源:https://stackoverflow.com/questions/54465816/how-to-use-flask-in-google-colaboratory-python-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!