Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cs205/workbook
  • yixino3/workbook
2 results
Show changes
Commits on Source (4)
e283353ca97e4346b7ebb5e9aa3f312f
\ No newline at end of file
{
"title": "{{TITLE}}",
"index": {{INDEX}},
"type": "{{TYPE}}"
}
import csv
import json
{% extends "templates/projectBase.html" %}
{% block projectContent %}
<h2></h2>
<div id="chart" style="margin: 0px auto; text-align: center;"></div>
<script src="web/vis.js"></script>
{% endblock %}
// Asks JavaScript to show more errors.
"use strict";
/*
* # Boilerplate jQuery
* This code loads the file `res/scores.json` and calls the `visualize` function
* as soon as the JSON file is loaded.
*/
$(function() {
$.getJSON("res/data.json")
.done(function (data) { visualize(data); })
.fail(function() { alert("Failed to load the JSON file!\n(Did your Python run?)"); });
});
/*
* # d3.js visualization
* All of the code to create our visualization will be contained in the `visualize` function,
* which is called once the data for the visualization has been loaded by the boilerplate
* jQuery code.
*/
var visualize = function(data) {
/*
* # Boilerplate Code for d3.js
*/
var margin = { top: 20, right: 20, bottom: 20, left: 20 },
width = 800 - margin.left - margin.right,
height = 970 - margin.top - margin.bottom;
var svg = d3.select("#chart")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("width", width + margin.left + margin.right)
.style("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
/*
* #
*/
};
{% extends "static/templates/base.html" %}
{% block content %}
<script>
var updateNewProjectForm = function() {
// Read project name, return if empty
var projectName = $("#inputProjectName").val();
if (projectName == "") {
$("#dirName").html("&mdash;");
$("#hiddenDirName").val("");
return;
}
// Read project type, fix it for directory type
var projectType = $("#selectProjectType option:selected").val();
if (projectType == "Personal") { projectType = "per"; }
else if (projectType == "Demo") { projectType = "demo"; }
else if (projectType == "Experience") { projectType = "exp"; }
else if (projectType == "Project") { projectType = "proj"; }
// Create the directory name
var dirName = projectType + "_" + projectName;
// Remove spaces
dirName = dirName.replace(/\s+/g, '');
// Remove any non-words
dirName = dirName.replace(/[^\w]/gi, '')
// Update HTML
$("#dirName").html(dirName);
$("#hiddenDirName").val(dirName);
};
</script>
<div class="row">
<div class="col-sm-4">
<h3>Add New Project</h3>
<form class="form-horizontal">
<h3>Add New Discovery</h3>
<p>
<i>
This form simply populates a new directory with a base set of files for
a CS 205 &quot;Discovery&quot;. You can use it to quickly set up a
new directory for working on a new discovery.
</i>
</p>
<hr>
<form class="form-horizontal" action="/" method="post">
{% if error %}
<div class="alert alert-danger" role="alert">
{{error | safe}}
</div>
{% endif %}
{% if success %}
<div class="alert alert-success" role="alert">
{{success | safe}}
</div>
{% endif %}
<div class="form-group">
<label for="inputProjectName" class="col-sm-2 control-label">Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputProjectName" placeholder="Project Name">
<input oninput="updateNewProjectForm()" type="text" class="form-control" name="projectName" id="inputProjectName" placeholder="Project Name">
</div>
</div>
<div class="form-group">
<label for="inputProjectName" class="col-sm-2 control-label">Type:</label>
<label for="selectProjectType" class="col-sm-2 control-label">Type:</label>
<div class="col-sm-10">
<select class="form-control">
<option>Personal</option>
<option>Demo</option>
<option>Experience</option>
<option>Project</option>
<select class="form-control" id="selectProjectType" name="projectType" onchange="updateNewProjectForm()">
<option value="Personal">Personal</option>
<option value="Demo">Demo</option>
<option value="Experience">Experience</option>
<option value="Project">Project</option>
</select>
</div>
</div>
......@@ -27,24 +79,28 @@
<b>Dir.:</b>
</div>
<div class="col-sm-10">
<tt class="dirName">
#dirName
</tt>
<tt id="dirName">&mdash;</tt>
<input type="hidden" id="hiddenDirName" name="dirName" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-6 col-sm-push-6">
<button type="submit" class="btn btn-primary form-control">
<button type="submit" class="btn btn-primary form-control" id="buttonSubmit">
<span class="glyphicon glyphicon-plus-sign"></span>&nbsp;
Create Project
</button>
</div>
</div>
</form>
<hr />
This form does the same thing as creating a directory and creating <tt>project.json</tt>,
<tt>web</tt>, <tt>py</tt>, and <tt>res</tt> directories.
</div>
<div class="col-sm-4">
<h3>Personal</h3>
<ul>
{% for item in navigation.personal|sort(attribute="index") %}
<li><a href="/{{item.dir}}/">{{item.title}}</a></li>
{% endfor %}
</ul>
<hr>
<h3>Demos</h3>
<ul>
{% for item in navigation.demos|sort(attribute="index") %}
......@@ -59,8 +115,7 @@
<li><a href="/{{item.dir}}/">{{item.title}}</a></li>
{% endfor %}
</ul>
</div>
<div class="col-sm-4">
<hr>
<h3>Projects</h3>
<ul>
{% for item in navigation.projects|sort(attribute="index") %}
......
......@@ -4,7 +4,7 @@ import logging
logger = logging.getLogger("workbook")
# helper functions
from workbook_utilities import run_compute_py, read_project_json, construct_navigation, read_metadata_json
from workbook_utilities import run_compute_py, read_project_json, construct_navigation, read_metadata_json, create_new_dir
# flask
from flask import Flask, render_template, jsonify, send_file, send_from_directory, request
......@@ -30,6 +30,22 @@ app.logger.setLevel(logging.ERROR)
#
# Route the base URL to the main page
#
@app.route('/', methods=["POST"])
def home_createDir():
logger.info("Processing index POST...")
error = None
success = None
newDirName = request.form["dirName"]
if os.path.isdir(newDirName):
error = "Directory <b>" + newDirName + "</b> already exists!"
else:
create_new_dir(newDirName, request.form["projectName"], request.form["projectType"])
success = "New directory <b>" + newDirName + "</b> created!"
navigation = construct_navigation()
return render_template('static/templates/mainPage.html', navigation=navigation, error=error, success=success)
@app.route('/')
def home():
navigation = construct_navigation()
......@@ -81,6 +97,8 @@ def fetchExercise(projectDir):
# Get the project info
projectInfo = read_project_json(projectDir)
if projectInfo == None:
return "No project.json found."
metadata = read_metadata_json(projectDir)
# Figure out what to do based on the value of `show`
......
......@@ -7,6 +7,29 @@ logger = logging.getLogger("workbook")
# save the starting cwd
basecwd = os.getcwd()
def create_new_dir(newDirName, projectName, projectType):
import datetime
import shutil
# copy files
os.chdir(basecwd)
shutil.copytree("static/newDirFiles", newDirName)
# replace project.json with accurate info
with open( os.path.join(basecwd, newDirName, "project.json") ) as f:
file_str = f.read()
file_str = file_str.replace("{{TITLE}}", projectName)
file_str = file_str.replace("{{TYPE}}", projectType)
d = datetime.date.today()
file_str = file_str.replace("{{INDEX}}", d.strftime("%Y%m%d"))
with open(os.path.join(basecwd, newDirName, "project.json"), "w") as f:
f.write(file_str)
def load_src(name, fpath):
logger.info("-:Loading Python script:%s", fpath)
......@@ -33,6 +56,10 @@ def read_project_json(dir, project_data = {}):
project_data["dir"] = dir
fileName = os.path.join(basecwd, dir, "project.json")
if not os.path.isfile(fileName):
logger.debug("%s:No project.json", dir)
return None
logger.debug("%s:Reading project.json", dir)
with open(fileName) as json_file:
json_data = json.load(json_file)
......@@ -75,6 +102,7 @@ def construct_navigation():
navigation["experiences"] = []
navigation["projects"] = []
navigation["demos"] = []
navigation["personal"] = []
# Scan all of the directories
for index, rpath in enumerate(os.listdir(basecwd)):
......@@ -84,12 +112,14 @@ def construct_navigation():
project_data["dir"] = rpath
# Attempt to infer the type of project:
if rpath.startswith("project_"):
if rpath.startswith("proj_"):
project_data["type"] = "Project"
elif rpath.startswith("exp_"):
project_data["type"] = "Experience"
elif rpath.startswith("demo_"):
project_data["type"] = "Demo"
elif rpath.startswith("per_"):
project_data["type"] = "Personal"
else:
project_data["type"] = "Unknown"
......@@ -116,6 +146,8 @@ def construct_navigation():
navigation["projects"].append(project_data)
elif project_data["type"] == "Demo":
navigation["demos"].append(project_data)
elif project_data["type"] == "Personal":
navigation["personal"].append(project_data)
else:
logger.warning("%s:Unknown project type: " + project_data["type"] + ". Not added to navigation.", rpath)
continue
......