AttributeError:模块“ tensorflow”在jupyter笔记本中没有属性“ get_default_graph”

时间:2019-04-06 22:33:43

标签: tensorflow keras


import React, { Component } from 'react';
import NavBar from '../../components/navbar/Navbar';
import ProductSidebar from '../../components/productSidebar/ProductSidebar';
import ReportSidebar from '../../components/reportSidebar/ReportSidebar';
import NewReportSidebar from '../../components/newReportSidebar/newReportSidebar';


import 'uikit/dist/css/uikit.min.css';
import 'uikit/dist/js/uikit.min';
import './App.css';
import UIkit from "uikit";

// get all data in form and return object

class App extends Component {

    constructor(props){
        super(props);
        this.state = {currentProduct: "", currentReport: "", loaded_reports: ""}
    }

    componentDidMount() {
        //load reports
        /*
        , {
            method: 'GET',
            mode: "no-cors",
            headers: {
                'Access-Control-Allow-Origin':'*'
              }
        }
        */
       this.handleLoad();

        let product_list_url = `${main_url}product/`
        fetch(product_list_url)
        .then((response) => {
            let json = response.json(); // there's always a body
            console.log(json)
            if (response.status >= 200 && response.status < 300) {
                return json;
            } else {
                return json.then(Promise.reject.bind(Promise));
            }
        }).then(res => this.setState({product_list: res}))
    }

    searchClick = (product_id) => {
        let product_url = `${main_url}product/${product_id}`
        fetch(product_url)
            .then((response) => {
                let json = response.json(); // there's always a body
                if (response.status >= 200 && response.status < 300) {
                    return json;
                } else {
                    return json.then(Promise.reject.bind(Promise));
                }
            }).then(res => this.setState({currentProduct: res}))
    }

    getFormData = (event) => {
        let form = event.target;
        let elements = form.elements; // all form elements
        // eslint-disable-next-line
        let fields = Object.keys(elements).map(function (k) {
            if (elements[k].name !== undefined) {
                return elements[k].name;
                // special case for Edge's html collection
            } else if (elements[k].length > 0) {
                return elements[k].item(0).name;
            }
        }).filter(function (item, pos, self) {
            return self.indexOf(item) === pos && item;
        });
        let data = {};
        fields.forEach(function(k){
            data[k] = elements[k].value;
            let str = ""; // declare empty string outside of loop to allow
                          // it to be appended to for each item in the loop
            if(elements[k].type === "checkbox"){ // special case for Edge's html collection
                str = str + elements[k].checked + ", "; // take the string and append
                                                        // the current checked value to
                                                        // the end of it, along with
                                                        // a comma and a space
                data[k] = str.slice(0, -2); // remove the last comma and space
                                            // from the  string to make the output
                                            // prettier in the spreadsheet
            }else if(elements[k].length){
                for(let i = 0; i < elements[k].length; i++){
                    if(elements[k].item(i).checked){
                        str = str + elements[k].item(i).value + ", "; // same as above
                        data[k] = str.slice(0, -2);
                    }
                }
            }
        });

        // add form-specific values into the data
        data.formDataNameOrder = JSON.stringify(fields);
        data.formGoogleSheetName = form.dataset.sheet || "responses"; // default sheet name
        data.formGoogleSendEmail = form.dataset.email || ""; // no email by default

        return data;
    };

    handleAdd = () => {
        let report = this.state.currentReport;
        let question_url = `${main_url}product/${this.state.currentProduct.product_id}/questions`
        fetch(question_url)
            .then((response) => {
                let json = response.json(); // there's always a body
                if (response.status >= 200 && response.status < 300) {
                    return json;
                } else {
                    return json.then(Promise.reject.bind(Promise));
                }
            }).then(res => {
                report.products.push({
                    product_name: this.state.currentProduct.product_name,
                    product_id: this.state.currentProduct.product_id,
                    questions: res
                });
                this.setState({
                    currentReport: report,
                    edited: true
                });
                UIkit.notification({
                    message: `Successfully added ${this.state.currentProduct.product_name} to your report!`,
                    status: 'success',
                    timeout: 3000
                });
            })
    };

    handleAnswerChange = (note) => {
        let reportData = this.state.currentReport;
        let tuple = note.target.name.split(" ");
        reportData.products[tuple[0]].questions[tuple[1]].answer = note.target.value;
        this.setState({currentReport: reportData, edited: true});
        console.log(`Notes have changed to "${reportData.products[tuple[0]].questions[tuple[1]].answer}"`)
    };


    handleLoad = () => {
        console.log("recall to server for reports");
        let report_url = `${main_url}report/`
        fetch(report_url)
            .then((response) => {
                let json = response.json(); // there's always a body
                if (response.status >= 200 && response.status < 300) {
                    return json;
                } else {
                    return json.then(Promise.reject.bind(Promise));
                }
            }).then(res => this.setState({loaded_reports: res}))
    };
    handleSave = () => {
        console.log("save current version to server");
        UIkit.notification({
            message: 'Successfully saved!',
            status: 'success',
            timeout: 3000
        });
        let report_url = `${main_url}report/${this.state.currentReport.meta.product_id}`
        fetch(report_url)
            .then((response) => {
                let json = response.json(); // there's always a body
                if (response.status >= 200 && response.status < 300) {
                    return json;
                } else {
                    return json.then(Promise.reject.bind(Promise));
                }
            }).then(res => this.setState({edited: false}))
    };

    changeReport = (id) => {
        console.log(`report changed to ${id}!`);
        UIkit.notification({
            message: 'Successfully loaded!',
            status: 'success',
            timeout: 3000
        });
        let report_url = `${main_url}report/${id}`
        fetch(report_url)
            .then((response) => {
                let json = response.json(); // there's always a body
                if (response.status >= 200 && response.status < 300) {
                    return json;
                } else {
                    return json.then(Promise.reject.bind(Promise));
                }
            }).then(res => this.setState({currentReport: res}))
    };

    handleNew = (event) => {
        event.preventDefault();
        let data = this.getFormData(event);
        event.target.reset();
        let report_url = `${main_url}report/new`
        fetch(report_url, {
            method: "POST",
            body: {
                report_name: data.report_name,
                company_name: data.company_name
            }
        })
            .then((response) => {
                let json = response.json(); // there's always a body
                if (response.status >= 200 && response.status < 300) {
                    return json;
                } else {
                    return json.then(Promise.reject.bind(Promise));
                }
            }).then(res => this.setState({currentReport: res, edited: false}))
            UIkit.notification({
                message: 'Successfully created new report!',
                status: 'success'
            })
    };

    handleMeta = (event) => {
        event.preventDefault();
        let data = this.getFormData(event);
        let reportData = this.state.currentReport;
        reportData.meta.report_name = data.report_name;
        reportData.meta.company_name = data.company_name;
        this.setState({currentReport: reportData, edited: false});
    };

    handleMetaChange = (event) => {
        let reportData = this.state.currentReport;
        reportData.meta[event.target.name] = event.target.value;
        this.setState({currentReport: reportData, edited: true})
    };

  render() {

      let {currentProduct, currentReport, loaded_reports} = this.state;
        console.log(loaded_reports)
    return (
      <div>
        <NavBar product_list={this.state.product_list} searchClick={this.searchClick}/>
          <div className='uk-container'>
            <div className='uk-child-width-expand@m' data-uk-grid="">
                <div className="uk-section-default uk-panel-scrollable uk-height-viewport" style={{resize:"none", border:"none"}} uk-height-viewport="offset-top: true">
                    {
                        typeof(currentProduct) === "object" &&
                        <ProductSidebar
                            productData={currentProduct}
                            handleAdd={this.handleAdd}
                        />
                    }
                    { 
                        typeof(currentProduct) !== "object" &&
                        <div>
                            Start your search above!
                        </div>
                    }
                </div>
                <div className="uk-section-default uk-panel-scrollable uk-height-viewport" style={{resize:"none", border: "none"}} uk-height-viewport="offset-top: true">
                {
                    typeof(currentReport) === "object" &&
                    <ReportSidebar
                        reportData={currentReport}
                        handleAnswerChange={this.handleAnswerChange}
                        handleLoad={this.handleLoad}
                        handleSave={this.handleSave}
                        handleNew={this.handleNew}
                        handleMetaChange={this.handleMetaChange}
                        handleMeta={this.handleMeta}
                        loaded_reports={loaded_reports}
                        changeReport={this.changeReport}
                        edited={this.state.edited}
                    />
                }
                {
                    currentReport === "" && 
                    this.state.loaded_reports &&
                    <NewReportSidebar
                        handleLoad={this.handleLoad}
                        handleNew={this.handleNew}
                        changeReport={this.changeReport}
                        loaded_reports={loaded_reports}
                    />
                }
                </div>
            </div>
        </div>
      </div>
    );
  }
}

export default App;

此代码运行jupyter noteebook python3.6 anaconda envs。

AttributeError: module 'tensorflow' has no attribute
'get_default_graph'



 [1]: https://i.stack.imgur.com/Ikoe7.png

0 个答案:

没有答案