简体   繁体   English

如何在 Google Colaboratory Python Notebook 中使用 Flask?

[英]How to use Flask in Google Colaboratory Python Notebook?

I am trying to build a website using Flask, in a Google Colab Python notebook.我正在尝试在 Google Colab Python 笔记本中使用 Flask 构建网站。 However, running a regular Flask code that works on a regular Python, fails to work on Google Colab.但是,运行可在常规 Python 上运行的常规 Flask 代码无法在 Google Colab 上运行。 I need code that will work it out please.. :)我需要可以解决的代码.. :)

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要重新启动 Flask,您可以单击菜单:runtime->restart runtime

Share link here : 在这里分享链接:

在此处输入图片说明

!pip install flask-ngrok


from flask import Flask
from flask import request
from flask_ngrok import run_with_ngrok

app = Flask(__name__)
run_with_ngrok(app)  # Start ngrok when app is run

# for / root, return Hello Word
@app.route("/")
def root():
    url = request.method
    return f"Hello World! {url}"

app.run()

https://medium.com/@kshitijvijay271199/flask-on-google-colab-f6525986797b https://medium.com/@kshitijvijay271199/flask-on-google-colab-f6525986797b

The server side code AKA: backend服务器端代码又名:后端

from flask import Flask
import threading

app = Flask(__name__)

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

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

Rendering the server inside the colab在 colab 中渲染服务器

import IPython.display

def display(port, height):
    shell = """
        (async () => {
            const url = await google.colab.kernel.proxyPort(%PORT%, {"cache": true});
            const iframe = document.createElement('iframe');
            iframe.src = url;
            iframe.setAttribute('width', '100%');
            iframe.setAttribute('height', '%HEIGHT%');
            iframe.setAttribute('frameborder', 0);
            document.body.appendChild(iframe);
        })();
    """
    replacements = [
        ("%PORT%", "%d" % port),
        ("%HEIGHT%", "%d" % height),
    ]
    for (k, v) in replacements:
        shell = shell.replace(k, v)

    script = IPython.display.Javascript(shell)
    IPython.display.display(script)

display(6060, 400)
from flask_ngrok import run_with_ngrok
from flask import Flask

app = Flask(__name__)

run_with_ngrok(app)  
@app.route("/")

def home():
    return f"Running Flask on Google Colab!"

app.run()

I found an example that run Flask in a notebook environment. 我找到了一个在笔记本环境中运行Flask的例子。 You can use their approach. 你可以使用他们的方法。

https://parselmouth.readthedocs.io/en/latest/examples/web_service.html https://parselmouth.readthedocs.io/en/latest/examples/web_service.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在谷歌合作实验室的笔记本上运行 nbconvert - How to run nbconvert on notebook in google colaboratory 如何在Google Colaboratory的Jupyter笔记本中安装svmutil? - How to install svmutil in Jupyter notebook on Google Colaboratory? 如何在Python IDE中使用Google Colaboratory服务器作为python解释器? - How to use Google Colaboratory server as python interpreter in Python IDE? 如何在 Colaboratory Google 上使用 Selenium? - How to use Selenium on Colaboratory Google? 如何在 Google Colaboratory 中使用 IBM python 模块“pixiedust”? - How to use IBM python module "pixiedust" in Google Colaboratory? 如何在 google colaboratory 中导入 python 文件? - How to import python files in google colaboratory? 如何在Google合作实验室中以编程方式清除Python输出? - How to Clear Python Output Programatically in Google Colaboratory? google colaboratory中的Python底图 - Python basemap in google colaboratory Jupyter Notebook 中 Google Colaboratory 上的 IOPub 错误 - IOPub Error on Google Colaboratory in Jupyter Notebook 如何将 csv 文件(并使用它)从谷歌驱动器上传到谷歌合作实验室 - How to upload csv file (and use it) from google drive into google colaboratory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM