【问题标题】:How to trigger a google cloud function with inputs [duplicate]如何使用输入触发谷歌云功能[重复]
【发布时间】:2021-11-05 16:04:09
【问题描述】:

在下面的脚本中,我手动将字符串值分配给 4 个变量(学院、部门、课程、部门)。

import pandas as pd

def open_seats(request):
  college = "ENG"
  department = "EC"
  course = "414"
  section = "A1"

  url = 'https://www.bu.edu/link/bin/uiscgi_studentlink.pl/1630695081?ModuleName=univschr.pl&SearchOptionDesc=Class+Number&SearchOptionCd=S&KeySem=20223&ViewSem=Fall+2021&College=' \
        + college + '&Dept=' + department + '&Course=' + course + '&Section=' + section


  table = pd.read_html(url)[4]
  class_table = table['Class']
  open_seats_table = table['OpenSeats']

  new_table = pd.concat([class_table, open_seats_table], axis=1)


  full_section_string = college + '\u00A0' + department + course + '\u00A0' + section

  for i in range(len(new_table)):
    if new_table['Class'][i] == full_section_string:
      val = new_table['OpenSeats'][i]
      break

  return val

我想将此脚本连接到要求用户输入这 4 个变量的数据的移动应用程序。因此,与其手动标记它们,我如何为变量分配来自触发器的数据?

起初,我以为数据会以 JSON 格式发送:

{
"college":"ENG",
"department":"EC"
"course":"414",
"section":"A1"
}

所以我将代码更新为如下所示:

def open_seats(request):
  college = request["college"]
  department = request["department"]
  course = request["course"]
  section = request["section"]

我对 http 触发器的功能方式以及如何通过 http 触发器将输入传递给云函数缺乏一些基本知识。

【问题讨论】:

标签: python google-cloud-functions


【解决方案1】:

使用下面的代码,我得到了 12 的响应

import pandas as pd
college = "ENG"
department = "EC"
course = "414"
section = "A1"

def open_seats(a,b,c,d):

    url_base = 'https://www.bu.edu/link/bin/uiscgi_studentlink.pl/1630695081?ModuleName=univschr.pl&SearchOptionDesc=Class+Number&SearchOptionCd=S&KeySem=20223&ViewSem=Fall+2021&College='

    url = url_base + a + "&Dept=" + b + "&Course=" + c + "&Section=" + d
    table = pd.read_html(url)[4]
    class_table = table['Class']
    open_seats_table = table['OpenSeats']
    new_table = pd.concat([class_table, open_seats_table], axis=1)


    full_section_string = a + '\u00A0' + b + c + '\u00A0' + d

    for i in range(len(new_table)):
        if new_table['Class'][i] == full_section_string:
            val = new_table['OpenSeats'][i]
            return print(f'{val}')
# I call the function with the predefined variables being passed
open_seats(college,department,course,section)

你想将什么数据传递给open_seats(request)

def open_seats(request):
  college = request["college"]
  department = request["department"]
  course = request["course"]
  section = request["section"]

您没有将来自应用的原始数据发布到脚本中。如果你能发布它,我将能够给出完整的答案。

还有你用来接受用户输入的代码吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 2021-10-31
    • 2020-10-14
    • 2020-02-04
    • 2021-07-04
    相关资源
    最近更新 更多