Javascript 使用Flask和Angular,如何使$scope变量可用于python函数

Javascript 使用Flask和Angular,如何使$scope变量可用于python函数,javascript,python,angularjs,flask,Javascript,Python,Angularjs,Flask,我是一个初学者,所以我甚至不知道做这件事的最佳方式,也不知道该怎么称呼我正在尝试做的事情,但我正在制作一个事件发布应用程序,它的标题将是月份和年份,就像在中一样 Python的重要部分如下所示: import datetime now = datetime.datetime.now() current_month=now.month current_year=now.year def get_days_for_dates(year): dates = calendar.Calendar

我是一个初学者,所以我甚至不知道做这件事的最佳方式,也不知道该怎么称呼我正在尝试做的事情,但我正在制作一个事件发布应用程序,它的标题将是月份和年份,就像在中一样

Python的重要部分如下所示:

import datetime
now = datetime.datetime.now()
current_month=now.month
current_year=now.year

def get_days_for_dates(year):
    dates = calendar.Calendar().yeardayscalendar(year)
    days_of_week=   ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
    dates_list=[]
    days_list=[]
    for quarter in dates:
        for month in quarter:
            for week in month:
                for i in range(len(week)):
                    dates_list.append(week[i])
                    days_list.append(days_of_week[i])
    return days_list, dates_list

calendar_tuple = get_days_for_dates(current_year)

所以我的问题是,我想在Python中使用$scope.year,其中当前的_year是使用日历模块,并计算出每个日期的星期几。将此信息发送到后端的最佳方法是什么?

您需要使用AJAX请求将数据发送回服务器。这有两件:

第一个是在后端创建一个端点,它允许您发送请求并检索数据负载

@app.route('/day-lookup', method=['GET'])
def day_lookup():
    year = request.args.get('year', None)
    # error handle here

    calendar_tuple = get_days_for_dates(year)
    # do something with it, return the list, etc.    
第二部分是使用Angular发送数据并处理响应

var calendar = angular.module('calendar', []);
calendar.controller('month_and_year', ['$scope', '$http', function ($scope, $http) {
$scope.month = {{current_month}};
$scope.year = {{current_year}};
$scope.month_names = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
$scope.add_one = function () {
    if ($scope.month == 12) {
        $scope.year++;
        $scope.month = 1;
    } else {
        $scope.month++
    }
};
$scope.sub_one = function () {
    if ($scope.month == 1) {
        $scope.year--;
        $scope.month = 12;
    } else {
        $scope.month--
    }
};
$scope.send_year = function () {
    // Add the year as a parameter to GET request to your URL
    var url = "http://foo.com/?year=" + $scope.year;
    // Send info to the server, then handle the result
    $http.get(url).then(function (result) {
        // Do something with the result
    });
};
}]);

您需要使用AJAX请求将数据返回到服务器。这有两件:

第一个是在后端创建一个端点,它允许您发送请求并检索数据负载

@app.route('/day-lookup', method=['GET'])
def day_lookup():
    year = request.args.get('year', None)
    # error handle here

    calendar_tuple = get_days_for_dates(year)
    # do something with it, return the list, etc.    
第二部分是使用Angular发送数据并处理响应

var calendar = angular.module('calendar', []);
calendar.controller('month_and_year', ['$scope', '$http', function ($scope, $http) {
$scope.month = {{current_month}};
$scope.year = {{current_year}};
$scope.month_names = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
$scope.add_one = function () {
    if ($scope.month == 12) {
        $scope.year++;
        $scope.month = 1;
    } else {
        $scope.month++
    }
};
$scope.sub_one = function () {
    if ($scope.month == 1) {
        $scope.year--;
        $scope.month = 12;
    } else {
        $scope.month--
    }
};
$scope.send_year = function () {
    // Add the year as a parameter to GET request to your URL
    var url = "http://foo.com/?year=" + $scope.year;
    // Send info to the server, then handle the result
    $http.get(url).then(function (result) {
        // Do something with the result
    });
};
}]);