Skip to content

Instantly share code, notes, and snippets.

View perpouh's full-sized avatar
🍺
drinking!

perpouh perpouh

🍺
drinking!
View GitHub Profile
@perpouh
perpouh / main.py
Last active October 4, 2018 09:30
APIてきなものから値を取得してラベル付き散布図を表示する
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import datetime as dt
def onclick(event):
print(event)
if __name__ == '__main__':
@perpouh
perpouh / api.py
Created October 4, 2018 09:31
APIてきなもの
# -*- coding: utf-8 -*-
import mysql.connector
from flask import Flask, jsonify, request, send_file, make_response, send_from_directory
app = Flask(__name__)
@app.route('/csv', methods=['GET'])
def csv():
try:
cnn = mysql.connector.connect(host='localhost',
port=8889,
@perpouh
perpouh / likebutton.md
Last active January 20, 2019 10:58
ちょっと動くLIKEボタン
@perpouh
perpouh / nest.md
Created January 22, 2019 09:08
Rails5でネストしたリソースのform

前提:blog has_many :articles

routes.rb

resources :blogs do
  resources :articles
end

アクセス先:/blog/:blog_id/article/new

@perpouh
perpouh / validate.md
Created January 23, 2019 10:02
登録されるとつらいユーザー名リストについて
@perpouh
perpouh / hepler.md
Created January 24, 2019 08:05
検索画面で検索語をハイライトするヘルパー

たぶん探せば便利なgemがあるんだろうけど、適当にggった感じでは出てこなかったので。

  def focus(str, words)
    length = 100 # 他の画面でも使うならどっかに出したほうがいいかもしれない

    if !words.kind_of?(Array)
      words = [words]
    end
@perpouh
perpouh / search.md
Created January 29, 2019 01:44
ransackでキーワード検索を実装する
def index
  if params[:q].present?
    @articles = Article.ransack(search_query).result.page(params[:page]).per(10)
    render action: :search 
  else
    @articles = Article.all
  end
end
@perpouh
perpouh / _map.html.erb
Created February 6, 2019 05:26
Vueに毒されたのでパーシャルがコンポーネントみたいになったの図
<div id="map"></div>
<% if form.present? %>
<%= form.hidden_field :latitude, {id: "shop_latitude"} %>
<%= form.hidden_field :longitude, {id: "shop_longitude"} %>
<%= form.hidden_field :name, {id: "shop_name"} %>
<%= form.hidden_field :address, {id: "shop_address"} %>
<script type="application/javascript">
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
@perpouh
perpouh / article_controller.rb
Created February 21, 2019 02:19
検索ログをファイルに書き出しつつバッチ処理で集計してDBに吐くためのモデルクラス
class ArticlesController < ApplicationController
def index
if params[:q].present?
@articles = Article.ransack(search_query).result.page(params[:page]).per(10)
logger = Log.new
logger.write(params[:q][:title_or_body_matches_any].split(/\s/)) # せっかくMeCab入れたので修正する
render action: :search
elsif params[:tag].present?
@articles = Article.ransack({body_matches_all: "%##{params[:name]}%"}).result.page(params[:page]).per(10)
logger = Log.new
@perpouh
perpouh / article_controller.rb
Last active February 22, 2019 06:46
ユーザーが入力した検索ワードをMeCabでアレする
private
def search_query
return {"title_or_body_matches_any" => Mecab.parse(params[:q][:title_or_body_matches_any]).map{|t| "%#{t}%"}}
end